Weird behaviour of division in rails console - ruby-on-rails

2 servers (same gems, exactly same ruby version 2.2.2p95 installed)
Development
Loading development environment (Rails 4.0.13)
[1] pry(main)> (1/3).class
=> Fixnum
[2] pry(main)> (1/3)
=> 0
[3] pry(main)>
Production
Loading production environment (Rails 4.0.13)
2.2.2 :001 > (1/3).class
=> Rational
2.2.2 :002 > (1/3)
=> (1/3)
2.2.2 :003 >
wth ? The only difference between the two environments is that one uses rvm (production) the other not

Ok the issue is due to a dependency gem using mathn module, trying now to figure out which gem is :)

Related

Redis cache write return false

After upgrading the redis version in order to upgrade to sidekiq version 7. Whenever I try to write to the cache it returns false. But sidekiq works correctly.
Sidekiq 7.0.0
redis gem 5.0.5
redis 7.0.5
3.1.1 :003 > Rails.cache
=> #<ActiveSupport::Cache::RedisCacheStore options={:namespace=>nil, :compress=>true, :compress_threshold=>1024, :expires_in=>nil, :race_condition_ttl=>nil} redis=#<Redis client v5.0.5 for redis://127.0.0.1:6379/0>>
3.1.1 :005 > Rails.cache.write 'test', 'test'
=> false
EDIT:
Solved.The problem was in the new redis.conf file that was generated when upgrading.

Rails RGeo::Geos.supported? false on rails c, but true on irb

I'm using the rgeo gem which gives me troubles on the production server, although it works on my local machine.
This outputs are same on both local MacOS and Ubuntu 16.04 production machine:
psql --version -> (PostgreSQL) 9.6.15
geos-config --version -> 3.7.1
SELECT postgis_full_version(); returns:
POSTGIS="2.3.3 r15473" GEOS="3.7.1-CAPI-1.11.1 27a5e771" PROJ="Rel. 4.9.2, 08 September 2015" GDAL="GDAL 1.11.3, released 2015/09/16" LIBXML="2.9.3" LIBJSON="0.11.99" TOPOLOGY RASTER
But in rails c the production machine returns:
RGeo::Geos.supported? => false
although irb returns RGeo::Geos.supported? => true
Any advice is very much appreciated.
For others with this problem, spring stop and restart rails console could be enough if you installed everything correctly

Generate a random token in ruby >= 2.5.1 / >= rails 5

I can see from here that has_secure_token can generate a random token for a new record in a (User) model. How can I simply generate a random token without needing to do it for a model? (i.e. suppose an app has no model(s) or to put it really simply, to run some (preferably very simple) method, and it should return a random string like pX27zsMN2ViQKta1bGfLmVJE
I have tried gems SecureRandom and digest as recommended in other answers, but neither seems to bundle install successfully (the SO answers were oldish so perhaps those gems are deprecated?) - I'm using ruby 2.5.1 and rails 5.2.3
My goal is simply to generate tokens to use as keys for an API, so I essentially just need randomly generated strings that are safe to use in urls, perhaps a method that I can specify the length of the token (but I'll take what I can get for now!)
Just use SecureRandom. It's not a gem that you have to install. It's built into Ruby:
⇒ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
⇒ irb
2.5.1 :001 > require 'securerandom'
=> true
2.5.1 :002 > SecureRandom.hex(32)
=> "89d45edb28859a905672b707c8f7599f766d12074584ef48a997230dfc0e0998"
2.5.1 :003 > SecureRandom.base64(12)
=> "KaZbhQ7o7U/f9pMs"
2.5.1 :004 > SecureRandom.uuid
=> "ade17ef5-0943-4c70-b417-df7c96c198cd"

Ruby 2 Upgrade Breaks Nokogiri and/or open-uri Encoding?

I have a mystery to solve when upgrading our Rails3.2 Ruby 1.9 app to a Rails3.2 Ruby 2.1.2 one. Nokogiri seems to break, in that it changes its behavior using open-uri. No gem versions are changed, just the ruby version (this is all on OSX Mavericks, using brew, gcc4 etc).
Steps to reproduce:
$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-darwin13.1.0]
$ rails console
Connecting to database specified by database.yml
Loading development environment (Rails 3.2.18)
> feed = Nokogiri::XML(open(URI.encode("http://anyblog.wordpress.org/feed/")))
=> #(Document:0x3fcb82f08448 {
name = "document",
children = [
..
> feed.xpath("//item").count
=> 10
So all good! Next, after a rvm change to Ruby 2.1.2 and a bundle install..
$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
$ rails console
Connecting to database specified by database.yml
Loading development environment (Rails 3.2.18)
> feed = Nokogiri::XML(open(URI.encode("http://anyblog.wordpress.org/feed/")))
=>
> feed.inspect
=> "#<Nokogiri::XML::Document:0x86a1f21c name=\"document\">"
> feed.xpath("//item").count
=> 0
So it looks like the 'open' encoding has changed, in that a gzip http stream isn't being fed correctly to nokogiri? I checked with a nokogiri -v and it is using the packaged xml libs rather than system ones. Is this a open-uri Ruby 2.1.2 issue?
Another theory is that one of the gems has monkey patched open-uri to fix something in 1.9 and that is breaking 2.1? Help or ideas please!
EDIT: Here's more info not using Nokogiri, i.e. thinking this is more a open-uri issue on Ruby 2.1.2:
> open(url) {|f|
* f.each_line {|line| p line}
* p f.content_type
* p f.charset
* p f.content_encoding
* }
"\u001F\x8B\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003\xED\x9D\xDBr\eW\xB2\xA6\xAF\xED\xA7\xA8\xCD\u001E\xB7/$\u0010..
(snip)
3\xF3\xA79\xA7\xFAɗ\xFF\u000F\xEAo\x9C\u0014k\xE8\u0000\u0000"
"text/xml"
"utf-8"
["gzip"]
=> ["gzip"]
..the 1.9 version was readable, i.e. gzip was applied already.
If I go into a clean ruby irb it works ok, so it must be something in my rails gems that is changing the behavior of open-uri open to not deflate/gzip. I have a lot of gems referenced.. :(
Ok, here's an answer, and maybe the answer. Ruby 2 changed how it uses headers in HTTP requests and zipping/deflating, but at some point they changed their minds back and put it to be how 1.9 worked. In the interim some Rails gem maintainers monkey patched HTTP:Net to make their gems work on both 1.9 and 2.0. Those monkey patches still linger in older versions of gems and cause issues like I saw upgrading from 1.9 to 2.1
A summary of the issue and solution here:
http://avi.io/blog/2013/12/17/do-not-upgrade-your-rails-project-to-ruby-2-before-you-read-this/
We use the gem right_aws, and the details of that issue with ruby versions is here:
https://github.com/sferik/twitter/issues/473
The solution was to undo the monkey patch using this as a gem reference in our Gemfile:
gem 'right_http_connection', git: 'git://github.com/rightscale/right_http_connection.git', ref: '3359524d81'
Background reading and more info:
https://github.com/rightscale/right_aws/issues/167

Using Crypt Gem (Ruby On Rails)

I am attempting to use the crypt gem. While I am able to get it installed via bundler, when I do:
require 'crypt/rijndael'
I got an error:
no such file to load -- crypt/rijndael
Any ideas?
Which version of Crypt? Which version of Ruby? Where are you trying to require it from? I tried Crypt 1.1.4 on Ruby 1.8.7 and it worked for me in the rails console:
ruby-1.8.7-p334 :001 > require 'crypt/rijndael'
=> ["Crypt"]
ruby-1.8.7-p334 :002 >
If you're using 1.9, try crypt 1.2.1 instead:
https://github.com/titanous/crypt
Haven't tested it myself, but give it a shot if you're on 1.9

Resources