Printing contents of a method

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

"Nate Smith" <nsmith5@nist.gov> schrieb im Newsbeitrag
news:1089732884.9305.5.camel@circuit.eeel.nist.gov...

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

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

···

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.716 / Virus Database: 472 - Release Date: 7/5/2004

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 :slight_smile:
Thanks anyway

Nate

···

On Tue, 2004-07-13 at 12:12, Robert Klemme wrote:

"Nate Smith" <nsmith5@nist.gov> schrieb im Newsbeitrag
news:1089732884.9305.5.camel@circuit.eeel.nist.gov...
> 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?

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

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

Cheers

Dave

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

···

On Tue, 2004-07-13 at 14:24, Dave Thomas wrote:

>> 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

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

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

···

On Jul 13, 2004, at 13:43, Nate Smith wrote:

On Tue, 2004-07-13 at 14:24, Dave Thomas wrote:

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

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!

got an eta on that? i can't wait! :wink:

-a

···

On Wed, 14 Jul 2004, Dave Thomas wrote:

Here's some stuff from the new pickaxe:

--

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

===============================================================================

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

"Dave Thomas" <dave@pragprog.com> schrieb im Newsbeitrag
news:2F308146-D4FE-11D8-B4D9-000A95676A62@pragprog.com...

>>>> 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
>>
>> 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!
>

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

···

On Jul 13, 2004, at 13:43, Nate Smith wrote:
> On Tue, 2004-07-13 at 14:24, Dave Thomas wrote:

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

···

On Wed, Jul 14, 2004 at 04:22:21AM +0900, Ara.T.Howard wrote:

On Wed, 14 Jul 2004, Dave Thomas wrote:

>Here's some stuff from the new pickaxe:

got an eta on that? i can't wait! :wink:

--
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

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).

···

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

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

···

On Jul 13, 2004, at 14:22, Ara.T.Howard wrote:

On Wed, 14 Jul 2004, Dave Thomas wrote:

Here's some stuff from the new pickaxe:

got an eta on that? i can't wait! :wink:

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.)

no, the node tree is a blackbox.

Guy Decoux

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.
:wink:

···

On Wed, 14 Jul 2004 05:25:56 +0900, Dave Thomas <dave@pragprog.com> wrote:

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.

Dave,

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 :slight_smile:

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 :wink:

-- shanko

···

--- Lyle Johnson <lyle.johnson@gmail.com> wrote:

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