Printing contents of a method

N

Nate Smith

Hello all,

Does anyone know how to actually print (as a string) the contents of a
given method? I've googled and searched books, to no avail. Thanks much
for any help

Nate
 
R

Robert Klemme

Nate Smith said:
Hello all,

Does anyone know how to actually print (as a string) the contents of a
given method? I've googled and searched books, to no avail. Thanks much
for any help

AFAIK there is no easy solution. You could write a script that parses the
source file and prints it but this would not ensure you get the current
definition of that method.

Why do you need that?

robert
 
Z

Zach Dennis

I wrote a MiniSpreadsheet application in ruby using wxRuby, again thanks to
Curt Hibbs for coming forth with wxRuby as an option (i was first thinking
fxRuby or Ruby/Tk which at some point I still intend to do).

I submitted the application to Andrea's Practical Language comparision,
www.kochandreas.com/home/language/lang.htm

I submitted it about 5 minutes ago so it probably isn't up yet, but it
should be up within the week (i dont know the timeframe in which Andrea
works).

The spreadsheet application only follows the requirements set forth by the
language comparison and it is not a full fledged spreadsheet application,
but a "mini" one.

I'm excited so I thought I'd share the news on the list.

Zach Dennis
 
N

Nate Smith

AFAIK there is no easy solution. You could write a script that parses the
source file and prints it but this would not ensure you get the current
definition of that method.

Why do you need that?

Well basically I am writing a class editor like squeak or visual age for
ruby. I guess I'll have to look into writing my own class in C for it :)
Thanks anyway

Nate
 
D

Dave Thomas

Might SCRIPT_LINES help? (if the method is loaded into your program
after it starts)


Cheers

Dave
 
N

Nate Smith

Might SCRIPT_LINES help? (if the method is loaded into your program
after it starts)

Forgive me if I sound stupid, but what is SCRIPT_LINES? I search google
for it and it only came up with Perl references, and a couple of emails
that you wrote which had the word in it. Thanks!

Nate
 
D

Dave Thomas

Forgive me if I sound stupid, but what is SCRIPT_LINES? I search google
for it and it only came up with Perl references, and a couple of emails
that you wrote which had the word in it. Thanks!

Here's some stuff from the new pickaxe:

SCRIPT_LINES__ (Hash)

If a constant SCRIPT_LINES__ is defined and references a Hash at the
time Ruby is compiling code, Ruby will store an entry containing the
contents of each file it parses, with the file’s name as the key and an
array of strings as the value. See Kernel.require on page ?? for an
example.

and then over in Kernel.require:

The SCRIPT_LINES__ constant can be used to capture the source of code
read using require.

SCRIPT_LINES__ = {}
require 'code/scriptlines'
puts "Files: #{SCRIPT_LINES__.keys.join(', ')}"
SCRIPT_LINES__['./code/scriptlines.rb'].each do |line|
puts "Source: #{line}"
end

produces:

3/8 Files: ./code/scriptlines.rb,
/Users/dave/ruby1.8/lib/ruby/1.8/rational.rb
Source: require 'rational'
Source:
Source: puts Rational(1,2)*Rational(3,4)

(where code/scriptlines.rb does indeed contain the three lines of code
listed above).


Cheers

Dave
 
A

Ara.T.Howard

Here's some stuff from the new pickaxe:

got an eta on that? i can't wait! ;-)

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
M

Mauricio Fernández

got an eta on that? i can't wait! ;-)

Look at
http://raa.ruby-lang.org/project/coverage2/
or rcov (if you can find it haha }:) for some examples of what you can
do with that...

--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Q: What's the big deal about rm, I have been deleting stuff for years? And
never lost anything.. oops!
A: ...
-- From the Frequently Unasked Questions
 
E

Eirikur Hallgrimsson

Dave's example looks really cool, but my recent "Pragmatic" Windows
install doesn't like the require:
require 'code/scriptlines'
It doesn't find the file.

Eirikur
 
J

Jim Weirich

Eirikur Hallgrimsson said:
Dave's example looks really cool, but my recent "Pragmatic" Windows
install doesn't like the require:
require 'code/scriptlines'
It doesn't find the file.

code/scriptlines.rb is the file Dave is using as an example. You should
require a file of your own (or type in the three lines if scriptlines.rb).
 
D

Dave Thomas

got an eta on that? i can't wait! ;-)

It's going through its first review right now. Then it gets copyedits,
final layout, and printing. I'm hoping to have it available by the end
of September.


Cheers

Dave
 
L

Lyle Johnson

It's going through its first review right now. Then it gets copyedits,
final layout, and printing. I'm hoping to have it available by the end
of September.

Do yourself a favor and just ship a boxful to the Ruby Conference the
first weekend of October. It will be like shooting fish in a barrel.
;)
 
R

Robert Klemme

Dave Thomas said:
Forgive me if I sound stupid, but what is SCRIPT_LINES? I search google
for it and it only came up with Perl references, and a couple of emails
that you wrote which had the word in it. Thanks!

Here's some stuff from the new pickaxe:

SCRIPT_LINES__ (Hash)

If a constant SCRIPT_LINES__ is defined and references a Hash at the
time Ruby is compiling code, Ruby will store an entry containing the
contents of each file it parses, with the file’s name as the key and an
array of strings as the value. See Kernel.require on page ?? for an
example.

and then over in Kernel.require:

The SCRIPT_LINES__ constant can be used to capture the source of code
read using require.

SCRIPT_LINES__ = {}
require 'code/scriptlines'
puts "Files: #{SCRIPT_LINES__.keys.join(', ')}"
SCRIPT_LINES__['./code/scriptlines.rb'].each do |line|
puts "Source: #{line}"
end

produces:

3/8 Files: ./code/scriptlines.rb,
/Users/dave/ruby1.8/lib/ruby/1.8/rational.rb
Source: require 'rational'
Source:
Source: puts Rational(1,2)*Rational(3,4)

(where code/scriptlines.rb does indeed contain the three lines of code
listed above).

But still at best you get the original code of methods when they were
defined. If someone overrides it, you're lost. A C extension that can
recreate Ruby from a given method would be a safer approach IMHO. (If
that's possible, that is.)

Kind regards

robert
 
T

ts

R> defined. If someone overrides it, you're lost. A C extension that can
R> recreate Ruby from a given method would be a safer approach IMHO. (If
R> that's possible, that is.)

no, the node tree is a blackbox.


Guy Decoux
 
S

Shashank Date

Dave,

--- Lyle Johnson said:
Do yourself a favor and just ship a boxful to the
Ruby Conference the first weekend of October.

If you really get around to do this, then please do us
a favor... sign them too :)

No seriously, I have been wanting to have a signed
copy of the Pickaxe for a long time now. In fact, it
is the only Ruby book I have which is unsigned (and I
have all other books on Ruby).

And as you all know having only unsigned types is no
fun ;-)

-- shanko





__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail
 

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,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top