DBI Row / delegate behavior between rubies 1.8.7 and 2.1

I am executing the following code in ruby ​​1.8.7 to read lines from my database:

require 'dbi'
db_conn_handle = DBI.connect("DBI:Mysql:host=localhost;database=mydb;port=3306", "root")
sth = db_conn_handle.prepare("select accounts.id, accounts.name from accounts;")
sth.execute
info = sth.to_a
puts "Info: #{info[0].class}"
info.each do |x, y|
  puts "#{x} ... #{y}"
end

      

The output shows that the information [0] .class is DBI :: Row. This code works fine when executed with ruby ​​1.8.7 (rails 3.2.17)

When I try to execute it in ruby ​​2.1.5 / rails 3.2.17 it gives the following error:

/home/rjain/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/delegate.rb:392:in `__getobj__': not delegated (ArgumentError)
    from /home/rjain/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/delegate.rb:341:in `block in delegating_block'
    from /home/rjain/mac/query.rb:7:in `each'
    from /home/rjain/mac/query.rb:7:in `<top (required)>'
    from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands/runner.rb:52:in `eval'
    from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands/runner.rb:52:in `<top (required)>'
    from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands.rb:64:in `require'
    from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands.rb:64:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

      

The file / home / rjain / mac / query.rb is pasted above. I want to understand what is the difference between ruby ​​2.1 and 1.8 that is causing this problem. What is the cause of this problem?

+3


source to share


2 answers


The same issue scanned the issue.

Find lib / dbi / row.rb in the gem directory. Line 212 or so should read

        if RUBY_VERSION =~ /^1\.9/

      



Edit it like

        if RUBY_VERSION =~ /^1\.9/ || RUBY_VERSION =~ /^2/

      

+5


source


This was also a problem with code written with Ruby 1.8x, when running in 2.1.0 it coughed / usr / lib / ruby ​​/ 2.1.0 / delegate.rb: 392: in __getobj__': not delegated (ArgumentError) from /usr/lib/ruby/2.1.0/delegate.rb:341:in

block in delegating_block '

Once we found the jsc comment above, we changed the row.rb file in the dbi gem folder. Again, looking for the part:

 if RUBY_VERSION =~ /^1\.9/

      



and update it to

if RUBY_VERSION =~ /^1\.9/ || RUBY_VERSION =~ /^2/

After that, the application started without problems.

+2


source







All Articles