Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does absolute sign mean in Ruby?

For example

func(@param) do |f|
    some code here
end

and

@param.each do |sth|
    some code here
end

What does the absolute value sign do here? I don't understand these two pieces of code.

like image 706
OneZero Avatar asked Nov 29 '25 16:11

OneZero


1 Answers

It's the local variable within the block, so for the line:

@param.each do |sth|

you're iterating over @param right, well each item in @param is referred to singularly as sth.

So if @param refers to an array containing the numbers

[1,3,5,4]

During the first iteration sth will be 1, then 3, then 5, then 4.

Same goes for :

func(@param) do |f|

except now the local variable is called f! You could call it anything you want, even |ihavenoideawhatimdoing|

like image 58
siame Avatar answered Dec 02 '25 05:12

siame



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!