Method to view a class or a method?

V

Vincent Foley

Hello everyone,

I was writing some code in irb this morning, and after a while, I
realized that I wanted to put that into a script. I had to go and copy
& paste every method by hand. I was wondering if there was a way to
get the source string of a class (or possibly, just a method)? Kind of
like the reverse of eval(). I know that some languages support this
feature (Forth and Factor come to mind).

Thanks
 
R

Robert Klemme

Vincent said:
Hello everyone,

I was writing some code in irb this morning, and after a while, I
realized that I wanted to put that into a script. I had to go and copy
& paste every method by hand. I was wondering if there was a way to
get the source string of a class (or possibly, just a method)? Kind of
like the reverse of eval(). I know that some languages support this
feature (Forth and Factor come to mind).

No. The closest you get is methods with signature length. If you want
the full source code you need to do additional tricks, i.e. parse the
code yourself (probably using any of the ruby parsers around) before
ruby sees it.

That's the reason why I usually do it the other way round: I write the
code in an editor and paste it to IRB. :)

Kind regards

robert
 
C

ChrisH

Vincent said:
Hello everyone,

I was writing some code in irb this morning, and after a while, I
realized that I wanted to put that into a script. I had to go and copy
& paste every method by hand. I was wondering if there was a way to
get the source string of a class (or possibly, just a method)? Kind of
like the reverse of eval(). I know that some languages support this
feature (Forth and Factor come to mind).

Thanks

Maybe you should look at using irb-history
(http://irb-history.rubyforge.org/rdoc/)
Not familiar with it, but looks like it will save and allow recall of
code entered in an IRB session

Cheers
 
S

S.Volkov

Robert said:
That's the reason why I usually do it the other way round: I write the
code in an editor and paste it to IRB. :)
Do you mean copy/paste? Why!!!
There are Emacs, Eclipse, Crimson Editor... lot of others, which allow you
to execute script from editor buffer while you are editing it, and if you
need to test few lines of code, just add them in the editor buffer and put
__END__ line after, or use =begin/=end to prevent execution of unfinished
code; Emacs even allows you to send text to irb session from emacs buffer;
you should try - you will like it!
regards
Sergey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,351
Latest member
RoxiePulli

Latest Threads

Top