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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:24:55+00:00 2026-05-12T11:24:55+00:00

In this example from The Ruby Programming Language (p.270), I’m confused why the instance_eval

  • 0

In this example from The Ruby Programming Language (p.270), I’m confused why the instance_eval method on the last line of the sample code defines a class method called String.empty.

Don’t you use class_eval to define a class method and instance_eval when you want to define an instance method?

o.instance_eval("@x") # Return the value of o's instance variable @x

# Define an instance method len of String to return string length
String.class_eval("def len; size; end")

# Here's another way to do that
# The quoted code behaves just as if it was inside "class String" and "end"
String.class_eval("alias len size")

# Use instance_eval to define class method String.empty
# Note that quotes within quotes get a little tricky...
String.instance_eval("def empty; ''; end")
  • 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-12T11:24:55+00:00Added an answer on May 12, 2026 at 11:24 am

    Don’t you use class_eval to define a
    class method and instance_eval when
    you want to define an instance method?

    Unfortunately, it is not as straightforward as that.

    First take a closer look at what the examples of class_eval are doing. class_eval is a method which comes from Ruby’s module class so can be called on any class or module. When you use String.class_eval you are evaluating the given code in the context of the class. i.e. when you write String.class_eval("def len; size; end") it’s exactly like you reopened the class and typed the code passed to class_eval e.g.

    class String
      def len
        size
      end
    end
    

    Thus to add a class method using class_eval you would write String.class_eval("def self.empty; ''; end") which has the same effect as:

    class String
      def self.empty
        ''
      end
    end
    

    instance_eval is defined in Ruby’s Object class so is available on any Ruby object. In the general case it can be used to add a method to a specific instance. e.g. if we have a String str and say:

    str.instance_eval("def special; size; end")
    

    Then this will alias special to size just for str but not for any other String object:

    irb(main):019:0> "other".special
    NoMethodError: undefined method `special' for "other":String
            from (irb):19
    

    To understand what is going on with String.instance_eval remember that the class String is itself an object (an instance of the class Class) and that there is such a singleton instance object of every class defined. When you use String.instance_eval you are evaluating the given code in the context of the String instance object. i.e. it is equivalent to reopening String’s metaclass and typing the code passed e.g.

    class String
      class << self
        def empty
          ''
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 163k
  • Answers 163k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After a lot of playing around with gettext() and xgettext… May 12, 2026 at 12:16 pm
  • Editorial Team
    Editorial Team added an answer Here's a blog post that talks about this: ASP.NET does… May 12, 2026 at 12:16 pm
  • Editorial Team
    Editorial Team added an answer You can implement the pattern yourself if you need to… May 12, 2026 at 12:16 pm

Related Questions

I've been a web developer for some time now, and have recently started learning
This was something originally discussed during a presentation given by Charles Brian Quinn of
Recently I was talking to a co-worker about C++ and lamented that there was
I have this massive array of ints from 0-4 in this triangle. I am
Dependency Injection frameworks in Ruby have been pretty much declared unnecessary. Jamis Buck wrote

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.