3

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?

Community
  • 1
  • 1
Abe
  • 12,956
  • 12
  • 51
  • 72
  • 1
    |f| is variable that takes each instance from the group comma, it does not make any change until unless you use it in your do..end loop. For example, if you need to access an attribute value of a particular instance of comma, you can have f.attributename, other wise |f| makes no difference. I too am new to rails and I think there is something more than this though.. – user1455116 Oct 02 '12 at 16:04
  • 1
    One can use SymbolHound instead of Google for this kind of search: http://symbolhound.com/?q=Ruby+%7Cf%7C – philant Oct 02 '12 at 16:39

2 Answers2

5

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.

Josh
  • 5,631
  • 1
  • 28
  • 54
2

The |f| is a parameter of your block. With this few lines you call the method called comma with two parameters. The first is a symbol :show_mytable, the second is your block between the do and the end.

With a list of variable names enclosed between pipes you can specify the parameter list of your block. A block is like an anonymous function, it can be called with any number of parameters, ruby will try to best match them.

Matzi
  • 13,770
  • 4
  • 33
  • 50