Apparently, Ruby can make a code block returning an instance variable's value with a symbol. Consider:
class Person
attr_accessor :fn
end
def give(n,&b)
i=0
while(i<n)
aa = Person.new
aa.fn = "name #{i}"
i=i+1
puts b.call(aa)
end
end
Now, both give(5, &:fn)
and give(5) {|x| x.fn}
give
name 0
name 1
name 2
name 3
name 4
=> nil
But what does &:fn
really mean? I know the ampersand can convert a Proc to a block such as
bb = Proc.new {|x| x.fn}
give(5, &bb)
So what does the symbol :fn
mean? Where can I see a documentation of its use like this? Can we use a symbol to access the instance variable, like say person:new
or person[:new]
?