In Ruby, why does inspect() print out some kind of object id which is different from what object_id() gives?

The default implementation of inspect calls the default implementation of to_s, which just shows the hexadecimal value of the object directly, as seen in the Object#to_s docs (click on the method description to reveal the source). Meanwhile the comments in the C source underlying the implementation of object_id shows that there are different “namespaces” for … Read more

ActiveRecord::NoEnvironmentInSchemaError

Two Fixes to ActiveRecord::NoEnvironmentInSchemaError The other answers here describe the problem very well, but lack proper solutions. Hoping this answer helps someone experiencing this issue. Why this error is happening This incorrect error message is a result of this pull request designed to prevent destructive actions on production databases. As u/pixelearth correctly points out, Rails … Read more

Ruby dynamic variable name

You can do it with instance variables like i = 0 file.lines do |l| l.split do |p| if p[1] == “InitGame” instance_variable_set(“@Game_#{i += 1}”, Hash.new) end end end but you should use an array as viraptor says. Since you seem to have just a new hash as the value, it can be simply i = … Read more

What evaluates to false in Ruby?

false and nil are the only ones: http://www.ruby-doc.org/core-2.1.1/FalseClass.html Rails provides present? which also includes empty strings and empty arrays: http://api.rubyonrails.org/classes/Object.html#method-i-present-3F

Is Ruby a scripting language or an interpreted language?

Things aren’t just black and white. At the very least, they’re also big and small, loud and quiet, blue and orange, grey and gray, long and short, right and wrong, etc. Interpreted/compiled is just one way to categorize languages, and it’s completely independent from (among countless other things) whether you call the same language a … Read more

Dynamically set local variables in Ruby [duplicate]

As an additional information for future readers, starting from ruby 2.1.0 you can using binding.local_variable_get and binding.local_variable_set: def foo a = 1 b = binding b.local_variable_set(:a, 2) # set existing local variable `a’ b.local_variable_set(:c, 3) # create new local variable `c’ # `c’ exists only in binding. b.local_variable_get(:a) #=> 2 b.local_variable_get(:c) #=> 3 p a … Read more

attr_reader with question mark in a name

This question is old but with alias_method you can achieve that: class Foo attr_reader :server_error alias_method :server_error?, :server_error # […] end Basically the method server_error? will be an alias for the server_error method.

Does Ruby support unicode and how does it work?

What you heard is outdated and applies (only partially) to Ruby 1.8 or before. The latest stable version of Ruby (1.9), supports no less than 95 different character encodings (counted on my system just now). This includes pretty much all known Unicode Transformation Formats, including UTF-8. The previous stable version of Ruby (1.8) has partial … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)