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

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?

2

2 Answers

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

1

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:

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like