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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:56:53+00:00 2026-06-11T15:56:53+00:00

This snippet of code creates method as a private method, why? a = %q{def

  • 0

This snippet of code creates method as a private method, why?

a = %q{def hello() "Hello there!" end}
class A; end
A.class.send(:eval, a)
A.new.hello #=> NoMethodError: private method `hello' called for A

Platform: ruby 1.9.3p125, tested in pry

  • 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-11T15:56:55+00:00Added an answer on June 11, 2026 at 3:56 pm

    Methods which are defined outside of any module (so called global methods) are actually defined as private instance methods of Object. That way, the can be called everywhere (since everything inherits from Object), and they can only be called without an explicit receiver.

    This includes methods like require, load, puts, print, p, gets, and eval. (Note: most of those are actually defined in Kernel and mixed into Object but the effect and the objective is the same.)

    In your case, you are defining a method outside of any module: there is no mention of a module in your a string. The fact that you are calling eval on A.class is completely irrelevant. Like I said above: eval is a global method defined on Object for convenience reasons (so that it may be called everywhere). Your A.class.send(:eval) is just a very convoluted way of calling the global private eval method. It does not somehow magically set the context of the evaluated string to A.class.

    You could do 42.send(:eval) instead and the result would still be the same, just like puts('Hello') and 42.send(:puts, 'Hello') are exactly the same, because they end up calling the exact same method.

    And by the way: even if it did, it still wouldn’t do what you want. A.class is just Class (the class of any class is always Class), so if it did work as you expect, the method would be defined in Class, not in A.

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

Sidebar

Related Questions

Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
I have this snippet of code private Templates retrieveFromCache(String name) { TemplatesWrapper t =
if you run this snippet of code(put it in form1) in a fresh new
I have a class whose new method has been made private because I want
This snippet of code always parses the date into the current timezone, and not
I have this snippet of code: var ShopLogicOptions = {}; ShopLogicOptions.params = {orderId: '
Edit: I put this snippet of code in jsbin: http://jsbin.com/eneru I am trying to
I've found this snippet of code on the net. It sets up an NSMutableArray
So i got this snippet of code: $(document).ready(function () { var replacementDoneIn = $(body).html();
I found this snippet of code I am having trouble trying to find 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.