I was trying to do an open source contribution, and the line of code in the star_rating method below with options.merge() (this is the change I tried to submit) was kicked back with the following message “This is invalid Ruby syntax (in both 1.8.7 and 1.9.3)…” My question is why? I checked the Hash#merge method on the ruby interactive shell and it seems to work.
def star_rating(options = {})
##original line of code
has_many :rates_without_dimension, :as => :rateable, :class_name => 'RateMe', :dependent => :destroy, :conditions => {:dimension => nil}
##line of code I tried to submit
has_many :rates_without_dimension, :as => :rateable, options.merge(:class_name => 'RateMe'), :dependent => :destroy, :conditions => {:dimension => nil}
end
This is indeed invalid syntax. The last arguments of a method call can be a Hash, in which case Ruby allows you to drop the enclosing
{}:In your case, you have
:as => :rateablebefore a simple argument (options.merge), that’s not allowed.If you inverted them, you still wouldn’t get the effect you want:
What you can do here is insure you are passing two parameters. One way to do this:
Note that in Ruby 2.0, it will be possible to do what you wanted using the equivalent of the splat operators for arrays (
*) which will be**for hashes: