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 6860233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:23:00+00:00 2026-05-27T02:23:00+00:00

the ruby book I’m reading has confused me a bit. If I do the

  • 0

the ruby book I’m reading has confused me a bit. If I do the following, I understand completely why the code throws an error;

class Person
  def show_name
    puts @name
  end
end

person = Person.new
person.show_name
Person.show_name   #(note the capital P) this line falls over

It throws an error stating that the Person class does not have a method called show_name, because it is an instance method. I understand this completely. The book then throws in this example;

class Class
  def add_accessor(accessor_name)
    self.class_eval %Q{attr_accessor :#{accessor_name}}
  end
end

class Person
end

person = Person.new
Person.add_accessor :name    #note the capital P
Person.add_accessor :age     #capital P here too

person.name = "Mikey"
person.age = 30

puts person.name

and goes on to state how cool it is that you can add methods to classes dynamically. What I don’t understand is why I am suddenly allowed to call the “add_accessor” method as a class method (with a capital P) when the method itself isn’t defined as such? I thought all class methods had to be declared like this?

class Math
  def self.PI
    3.141
  end
end

puts Math.PI

Is anyone able to enlighten me?

  • 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-05-27T02:23:01+00:00Added an answer on May 27, 2026 at 2:23 am

    Look at this:

    person.instance_of?(Person) #=> true
    Person.instance_of?(Class)  #=> true
    

    You have defined an instance method add_accessor for all the instances of the class Class, so Person class can use that method because it is an instance of Class.

    I recommend yout to take a look at metaprogramming and Ruby Object Model.

    Hoep this helps

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

Sidebar

Related Questions

I'm working on learning ruby/rails at the moment - The book i'm reading has
I'm reading a Ruby book but it doesn't explain the following: What is this:
I'm reading the Eloquent Ruby book (awesome book so far) and don't understand one
Saw this piece of code in a Ruby on Rails book. This first one
Im modeling an online book with ruby on rails. The book has chapters, each
I'm reading a book called The Ruby Programming Language for Ruby 1.8 and 1.9.
I am following learn by example ruby book by michael hart, the book tells
I just started reading this book Eloquent Ruby and I have reached the chapter
Is there a book about Ruby On Rails for someone that has little or
I am reading a book about ruby, it learn me that I write my

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.