While reading some Ruby code I saw this:
create_table :talks do |t|
What is this notation |variable|
? What does that do?
And also, where do I find help for these specific subjects like | |
, #{}
, and so on?
While reading some Ruby code I saw this:
create_table :talks do |t|
What is this notation |variable|
? What does that do?
And also, where do I find help for these specific subjects like | |
, #{}
, and so on?
It's a way of defining arguments for a block, in a similar way to def methodname(arg1, arg2)
A nice explanation of blocks is available from Robert Sosinski
You may also hear them referred to as goal posts. They are essentially named arguments that one can use to iterate over the data within a collection. For example, with an array:
# Print 1 2 3 4
[1,2,3,4].each do |e|
print "#{e} "
end
Or with a key, value map, you would have multiple arguments between the goal posts
m = {"ruby" => "rails", "groovy" => "grails", "scala" => "lift", "java" => "spring"}
m.each do |lang, framework|
# print the keys first - "ruby groovy scala java"
print "#{lang} "
# print the values second - "rails grails lift spring"
print "#{framework} "
end
Your question sounds more specific to the Ruby language than Ruby on Rails. I would check out some of these links: