Ruby can be difficult for people of Java and PHP. :)
In Ruby, not everything looks as it seems. Take this for example:
before_save :make_rotting
This is a method call, of course. But this is not the make_rotting method that is called. This is before_save ( :make_rotting is its parameter). This is the so-called “hook” in ActiveRecord. before_save will take the method name as a parameter and will dynamically call it when the time comes.
if age > 20
Here age is a method call, not a character. It can be written as:
if age() > 20
but brackets are optional. And finally:
add_column :zombies, :rotting, :boolean, default: false
This method takes four parameters, the last of which is a hash. The hash uses the new Ruby 1.9 syntax. Previously, it would be written like this (with a colon in the right place and that's it):
add_column :zombies, :rotting, :boolean, :default => false
You should read a good Ruby programming book, instead of scraping pieces of knowledge from Stack Overflow posts. :)
source share