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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:58:30+00:00 2026-06-13T09:58:30+00:00

I am a little confused why the following piece of code actually works: String.instance_eval

  • 0

I am a little confused why the following piece of code actually works:

String.instance_eval do # self is set to String
  [:readlink, :symlink?, :expand_path].each do |method| # self is still String
    define_method(method) do # self is still String
      File.send(method, self) # what exactly is this self?
    end
  end
end
"asdf".expand_path # => "C:/users/some_user/asdf"

I don’t understand why the last line works as it does. When each method is defined isn’t the body of the method equivalent to File.send(method, String)? None of the above blocks actually change self. The only line that changes self is String.instance_eval and it changes self to String.

  • 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-13T09:58:30+00:00Added an answer on June 13, 2026 at 9:58 am
    File.send(method, self)
    

    This self will be evaluated when that dynamically generated method will be called. At that point it will be set to an instance of String. (“asdf” in your example).

    It’s actually equivalent to opening String class and writing all those methods manually.

    class String
      def readlink
        File.send :readlink, self
      end
    
      def expand_path
        File.send :expand_path, self
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a little confused by the following C code snippets: printf(Peter string is
I'm a little confused when I see the output of following code: $x =
I'm a little confused! I have the following bit of JS //Set datetimes for
I am a little confused with Enumerator#reject in ruby. Consider the following code: (1..10).select
I'm a little confused here. I've got the following code: class Users { function
I am little bit confused about following problem & their solutions: i have 2
i m little confused on memory management in following scenario -(NSData *)getData{ NSData *TempData
Possible Duplicate: What's the role of the parentheses in the following piece of code?
A little confused how to create a query and set a dynamic var in
I am a little confused as to how passing pointers works. Let's say I

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.