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

  • SEARCH
  • Home
  • 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 8057485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:02:59+00:00 2026-06-05T09:02:59+00:00

I have a code: class A attr_accessor :somevar def a somevar = ‘something’ puts

  • 0

I have a code:

class A
  attr_accessor :somevar

  def a
    somevar = 'something'
    puts @somevar
  end

  def b
    send :somevar=, 'something'
    puts @somevar
  end

end

A.new.a #=> nil
A.new.b #=> 'something'

Why is there a difference? Why can’t I just assign an instance variable through a writer? But why local variable has been created instead of method (setter) has been called?

  • 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-05T09:03:01+00:00Added an answer on June 5, 2026 at 9:03 am

    attr_accessor :somevar references the instance variable @somevar. Instance variables must be preceded by the @ sign. All other variables like ‘somevar’ without an @ sign in a method are just local variables to that method or scope, not instance variables for the object.

    Thus, changing the first line in method “a” to

    @somevar = 'something'
    

    will result in the answer you expect.

    Related note: you don’t have to declare instance variables in Ruby, you just create them with @somevar type notation. The attr_accessor method creates setters and getters for that instance variable.

    Methods in Ruby are attached to objects, so in order for class A to invoke its own somevar setter method, you would need to write self.somevar = 'something', otherwise the Ruby parser thinks you are just creating a local variable.

    This could be confusing because you could invoke method a from b, by just doing:

    def b
      send :somevar=,'something'
      puts @somevar
      a  # would invoke its own method 'a'
    end
    

    But the setter method somevar= is ambiguous with creating a local variable with the same notation:

    somevar='something'  # Ruby assumes you want to create a local variable
    

    So to call the somevar setter method you need to explicitly say that you are calling the method on self with:

    self.somevar = 'something'
    

    When you called send :somevar=,'something' you were invoking the somevar instance method also.

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

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
I have code: class Scene def initialize(number) @number = number end attr_reader :number end
I have the following code: class Engine attr_accessor :isRunning def initialize @isRunning = false
If I have this class: class A attr_accessor :b,:c,:d end and this code: a
I have the following code: class MyClass module MyModule class << self attr_accessor :first_name
I have this code: class DocumentIdentifier attr_reader :folder, :name def initialize( folder, name )
Lets say I have the code: class NoneDict(dict): def __getitem__(self, name): try: return super(NoneDict,
I have a class (ActorClue) that has three attr_accessor defined within it. There are
I have the following code with the corresponding test case: class XXX attr_accessor :source
Here is my ruby code class Demo attr_accessor :lines def initialize(lines) self.lines = lines

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.