Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does |f| do in Ruby? [duplicate]

Possible Duplicate:
In Ruby, what are the vertical lines?

This question seems Google-proof, and I do not know Ruby.

Comparing different presence of |f| at the end of a line in a model description causes content not to be shown. I am just trying to fix a bug in a page that does not provide access to some information in a table.

"What does ||= do in Ruby" about the || does not seem to help.

Here is the suspect code from the broken .rb file:

comma :show_mytable do |f|
    table2 :field2
    table3 :field3
end

but this seems to work, showing the desired fields when activated:

comma :show_mytable do
    table2 :field2
    table3 :field3
end

Could |f| prevent output from showing?

like image 303
Abe Avatar asked Mar 16 '26 00:03

Abe


1 Answers

In your code, you are passing two variables to the comma method. The first is a symbol called :show_mytable and the second is a block. It is unrelated to the ||= syntax which is conditional assignment.

Here is an example of how blocks are used in ruby:

array = [1, 2, 3, 4]
array.each do |element|
  element + 1
end 
  #=> 2 3 4 5

When you use a loop(each in this case), you can pass it a variable(element) to give you a way to reference the current element in the loop.

You can also use curly braces instead of do and end like this:

array = [1, 2, 3, 4]
array.each { |e| e + 1 } 
  #=> 2 3 4 5

Since you aren't looping through anything here I don't see any reason you could need the |f| in your example.

like image 200
Josh Avatar answered Mar 18 '26 06:03

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!