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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:49:42+00:00 2026-05-27T20:49:42+00:00

inspired by some lisp magic of runtime edit sourcecode, I want to do it

  • 0

inspired by some lisp magic of runtime edit sourcecode,

I want to do it in ruby. looks like I cannot get sourcecode from method/class,
is there a way to do it?

I write a sample sourcecode here:

def helloworld n
  "hello #{n}"
end

o = Kernel.method :helloword

Kernel.define_singleton_method o.name do |n|
  eval o.source_code.sub('hello', 'hello world')
end

helloworld 'halida' #=> 'hello world halida'
  • 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-27T20:49:43+00:00Added an answer on May 27, 2026 at 8:49 pm

    You can’t get the string representation of a part of the code, edit it and expect Ruby to reevaluate your changes. The only way to do something near to what you want is using ParseTree to get s-expressions of the source, edit and use Ruby2Ruby to generate a string of ruby code. Them add def ... and end to the string and call eval with it.

    It’s too hard and error-prone to be useful in a real-world situation. But I don’t know any other way.

    Note: ParseTree only works on Ruby 1.8.

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

Sidebar

Related Questions

Okay, I've been inspired to do some head punching . Seems like overloading operator&
This is more a conceptual question. It's inspired from using some extremely large table
After making some comments , I've been inspired to get some feedback on the
Inspired by this discussion , after some googling I wasn't able to find an
Inspired by the question What’s the simplest way to call Http GET url using
Inspired by Eric Sink's interview on the stackoverflow podcast I would like to build
Inspired from the game GunTactyx , where you write programs controlling fighting robots. Guntactyx
recently this post inspired me, I want to track my own life, too. I
I'm writing a game using Swing and want to achieve a distinctive steampunk inspired
Possible Duplicate: Relational UML Diagram inspired by SO I have developed my homework from

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.