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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:09:24+00:00 2026-05-11T20:09:24+00:00

I would like to do something like the following: class String def fancy_thing appendix

  • 0

I would like to do something like the following:

class String
  def fancy_thing appendix
   # Just a trivial example to ensure self and params work.
   # Pretend this is something complex.
   self.reverse + appendix
  end
end

# print_method on instance or class should spit out a string
#  containing the actual code for that method
ft_code = "cob".print_method :fancy_thing
ft_code = String.print_instance_method :fancy_thing
  # => "{|appendix| self.reverse + appendix }"  *

# ft_code gets passed around a bit...

# exec on an object should run code (w/ parameters) as if that code is 
#  an instance method on that object (or class method if it's a class)
"cob".exec(ft_code, '!') #=> "boc!"

How might one code print_method and foo.exec? Preferably, they should work for any arbitrary methods, without knowing a priori where they might happen to have been defined or sourced from.

  • Yes, I know methods and blocks aren’t completely the same. But this is closer to what yield and call would normally take; I don’t know of a better solution.
  • 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-11T20:09:24+00:00Added an answer on May 11, 2026 at 8:09 pm

    parse_tree will give you the key step that you’ll need:

    http://github.com/seattlerb/parsetree/tree/master

    I think this does it, in the quickest/hackiest/most insecure manner possible:

    require 'parse_tree'
    require 'parse_tree_extensions'
    require 'ruby2ruby'
    
    class Object
      def method_source(name)
        (class << self; self; end).instance_method(name).to_ruby
      end
    
      def exec(ruby, *args)
        code = eval(ruby, binding)
        code.call(*args)
      end
    end
    

    I’ll add that I’m having difficulty seeing how this is a good idea… But there you have it. 🙂

    [edit]

    Also note that your example is busted: your ‘fancy_thing’ method requires an argument (appendix).

    [edit 2]

    going over the top, here’s your test code with bugs fixed (the way I think you wanted it):

    class String
      def fancy_thing(appendix)
        reverse << appendix || nil
      end
    end
    
    code = "cob".method_source :fancy_thing
    # => "proc {|appendix| reverse << appendix }"  *
    "cob".exec code, '!'
    # => "boc!"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 141k
  • Answers 141k
  • 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 Sparc64 - alone amongst modern CPUs - implements neither double… May 12, 2026 at 8:10 am
  • Editorial Team
    Editorial Team added an answer To circumvent your problem, you could use the JSEclipse plug-in… May 12, 2026 at 8:10 am
  • Editorial Team
    Editorial Team added an answer At initial glance, it looks like you're not closing the… May 12, 2026 at 8:10 am

Related Questions

I'm having some inheritance issues as I've got a group of inter-related abstract classes
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
I am in the process of refactoring our BI layers to make our code
I am using reflection to populate the properties of an object. These properties have

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.