I am learning rails and going back to ruby to understand how methods in rails (and ruby really work). When I see method calls like:
validates :first_name, :presence => true
I get confused. How do you write methods in ruby that accept symbols or hashes. The source code for the validates method is confusing too. Could someone please simplify this topic of using symbols as arguments in ruby class and instance methods for me?
UPDATE:
Good one @Dave! But What I was trying out was something like:
def full_name (:first_name, :last_name)
@first_name = :first_name
@last_name = :last_name
p "#{@first_name} #{last_name}"
end
full_name("Breta", "Von Sustern")
Which obviously raises errors. I am trying to understand: Why is passing symbols like this as arguments wrong if symbols are just like any other value?
Symbols and hashes are values like any other, and can be passed like any other value type.
Recall that ActiveRecord models accept a hash as an argument; it ends up being similar to this (it’s not this simple, but it’s the same idea in the end):
This takes advantage of not having to put the hash in curly-brackets
{}unless you need to disambiguate parameters (and there’s a block parsing issue as well when you skip the parens).Similarly:
This outputs:
From there you can start to see how things like this work.