What is the meaning of "do | |" in Ruby?

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:

  • About Ruby
  • Ruby in 20 Minutes

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

Tags:

Ruby