Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:39:07+00:00 2026-06-07T20:39:07+00:00

Apparently, Ruby can make a code block returning an instance variable’s value with a

  • 0

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]?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:39:10+00:00Added an answer on June 7, 2026 at 8:39 pm

    Simple. The statment

    attr_accessor :fn
    

    Doesn’t define some sort of special instance variable thing. It declares two methods of (approximately) this form:

    def fn
      @fn
    end
    def fn=(v)
      @fn=v
    end
    

    The Ruby “syntax” &:fn is a symbol that the operator & tries to convert to a proc. And, guess what? Symbol implements to_proc. Guess that that looks like:

    def to_proc
      Proc.new {|obj| obj.__send__(self)}
    end
    

    That captures self, which is the symbol :fn, which, on invocation, tells obj to execute the method fn, which returns the value of the instance variable.

    EDIT: Answering the second part of the question, no and yes. With the syntax you say, no. But you can call a method from a symbol with BasicObject#__send__ and you can do the same for not attr_accessor instance variables with Object#instance_variable_get.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class TestClass CONSTANT = 1 end TestClass::CONSTANT = 2 Apparently you can change value
There is apparently a ruby method called method and I can't find any docs
Apparently this used to work on ruby 1.8.7 but unfortunately not on 1.9.2 class
Hi I have a question about ruby on rails Apparently I have a statement
Apparently MapReduce queries are one of the slowest things one can do in MongoDB
Apparently, using AttachDbFilename and user instance in your connection string is a bad way
Apparently mutableCopy copies by reference, not value. Ie if I do this: NSMutableArray arrayA
Ruby has conditional initialization. Apparently, Java does not or does it? I try to
I've recently installed Ruby 1.9.1 on Windows 7, and apparently it doesn't come with
I'm running Ruby on Windows though I don't know if that should make a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.