[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Can I see method source code?

femto

9/1/2006 6:45:00 AM

hello all, Can I see method source code,
like in javascript, when you generate a function,
you can call to_string on it to see its source code.
It's especailly useful when you examine other's code
or dynamic generating func/method.

29 Answers

Jeremy Tregunna

9/1/2006 7:23:00 AM

0


On 06-09-01, at 02:45, femto gary wrote:

> hello all, Can I see method source code,
> like in javascript, when you generate a function,
> you can call to_string on it to see its source code.
> It's especailly useful when you examine other's code
> or dynamic generating func/method.

No, there are only a few languages I can think of (okay, 2) that
allow this: the aforementioned javascript, and Io; Ruby does not.

--
Jeremy Tregunna
jtregunna@blurgle.ca



Ilmari Heikkinen

9/1/2006 8:01:00 AM

0

hi,

On 9/1/06, femto gary <femtowin@gmail.com> wrote:
> hello all, Can I see method source code,
> like in javascript, when you generate a function,
> you can call to_string on it to see its source code.
> It's especailly useful when you examine other's code
> or dynamic generating func/method.
>
>

http://blog.zenspider.com/archives/2005/02/rubyt...
may be of some use

Robert Klemme

9/1/2006 8:08:00 AM

0

On 01.09.2006 09:23, Jeremy Tregunna wrote:
>
> On 06-09-01, at 02:45, femto gary wrote:
>
>> hello all, Can I see method source code,
>> like in javascript, when you generate a function,
>> you can call to_string on it to see its source code.
>> It's especailly useful when you examine other's code
>> or dynamic generating func/method.
>
> No, there are only a few languages I can think of (okay, 2) that allow
> this: the aforementioned javascript, and Io; Ruby does not.

Just out of curiosity: what about Lisp?

robert

Jeremy Tregunna

9/1/2006 9:26:00 AM

0


On 06-09-01, at 04:10, Robert Klemme wrote:

> On 01.09.2006 09:23, Jeremy Tregunna wrote:
>> On 06-09-01, at 02:45, femto gary wrote:
>>> hello all, Can I see method source code,
>>> like in javascript, when you generate a function,
>>> you can call to_string on it to see its source code.
>>> It's especailly useful when you examine other's code
>>> or dynamic generating func/method.
>> No, there are only a few languages I can think of (okay, 2) that
>> allow this: the aforementioned javascript, and Io; Ruby does not.
>
> Just out of curiosity: what about Lisp?

I don't know of a single lisp implementation that lets you do this,
perhaps you could enlighten me?

--
Jeremy Tregunna
jtregunna@blurgle.ca



Devin Mullins

9/1/2006 12:48:00 PM

0

>> hello all, Can I see method source code,
> http://blog.zenspider.com/archives/2005/02/rubyt...
Yup. I made another, much simpler one, using method_added:
http://svn.twifkak.com/jonx/site_ruby/...

Only works for methods defined normally, in actual files. Not
meta-programming friendly or anything. Just a cheap hack, but great (as
the URL implies) for IRB use.

Devin

Robert Klemme

9/1/2006 12:53:00 PM

0

On 01.09.2006 11:26, Jeremy Tregunna wrote:
>
> On 06-09-01, at 04:10, Robert Klemme wrote:
>
>> On 01.09.2006 09:23, Jeremy Tregunna wrote:
>>> On 06-09-01, at 02:45, femto gary wrote:
>>>> hello all, Can I see method source code,
>>>> like in javascript, when you generate a function,
>>>> you can call to_string on it to see its source code.
>>>> It's especailly useful when you examine other's code
>>>> or dynamic generating func/method.
>>> No, there are only a few languages I can think of (okay, 2) that
>>> allow this: the aforementioned javascript, and Io; Ruby does not.
>>
>> Just out of curiosity: what about Lisp?
>
> I don't know of a single lisp implementation that lets you do this,
> perhaps you could enlighten me?

<disclaimer>No LISP guru here</disclaimer>

I probably confused lambdas with normal functions:

[1]> (setq f1 (lambda (x) (+ x x)))
#<FUNCTION :LAMBDA (X) (+ X X)>
[2]> (funcall f1 10)
20
[3]> f1
#<FUNCTION :LAMBDA (X) (+ X X)>

So, for a lambda you can - but apparently for functions you can't. Or
can you?

Kind regards

robert

Gene Tani

9/1/2006 4:52:00 PM

0


femto gary wrote:
> hello all, Can I see method source code,
> like in javascript, when you generate a function,
> you can call to_string on it to see its source code.
> It's especailly useful when you examine other's code
> or dynamic generating func/method.

you can search c.l.ruby for all the SCRIPT_LINES__ tricks that people
ahve done

Eric Hodel

9/2/2006 1:48:00 AM

0

On Sep 1, 2006, at 12:23 AM, Jeremy Tregunna wrote:

> On 06-09-01, at 02:45, femto gary wrote:
>
>> hello all, Can I see method source code,
>> like in javascript, when you generate a function,
>> you can call to_string on it to see its source code.
>> It's especailly useful when you examine other's code
>> or dynamic generating func/method.
>
> No, there are only a few languages I can think of (okay, 2) that
> allow this: the aforementioned javascript, and Io; Ruby does not.

Lies (provided the method is written in Ruby).

$ ruby -rlib/ruby2ruby.rb -I ../../ruby_to_c/dev/lib/ -e 'puts
Object.source(:source)'
def source(method_name = nil)
RubyToRuby.new.process(parse_tree(method_name))
end

You can find ruby2ruby.rb in the ZenHacks gem.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Rick DeNatale

9/2/2006 9:29:00 PM

0

On 9/1/06, Robert Klemme <shortcutter@googlemail.com> wrote:

> <disclaimer>No LISP guru here</disclaimer>
>
> I probably confused lambdas with normal functions:
>
> [1]> (setq f1 (lambda (x) (+ x x)))
> #<FUNCTION :LAMBDA (X) (+ X X)>
> [2]> (funcall f1 10)
> 20
> [3]> f1
> #<FUNCTION :LAMBDA (X) (+ X X)>
>
> So, for a lambda you can - but apparently for functions you can't. Or
> can you?

I think that it just looks like the source here. The print-name of a
lambda can look pretty much like source code because the internal
representation looks a lot like the source code. But it's not really
the source code.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Logan Capaldo

9/2/2006 10:55:00 PM

0


On Sep 2, 2006, at 5:28 PM, Rick DeNatale wrote:

> On 9/1/06, Robert Klemme <shortcutter@googlemail.com> wrote:
>
>> <disclaimer>No LISP guru here</disclaimer>
>>
>> I probably confused lambdas with normal functions:
>>
>> [1]> (setq f1 (lambda (x) (+ x x)))
>> #<FUNCTION :LAMBDA (X) (+ X X)>
>> [2]> (funcall f1 10)
>> 20
>> [3]> f1
>> #<FUNCTION :LAMBDA (X) (+ X X)>
>>
>> So, for a lambda you can - but apparently for functions you
>> can't. Or
>> can you?
>
> I think that it just looks like the source here. The print-name of a
> lambda can look pretty much like source code because the internal
> representation looks a lot like the source code. But it's not really
> the source code.
>
Note that this is the same reason Ruby2Ruby doesn't give you the
"source code". Of course the OP probably doesn't really care if he
gets the source code "character for character".

> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...
>
> IPMS/USA Region 12 Coordinator
> http://ipmsr12.denh...
>
> Visit the Project Mercury Wiki Site
> http://www.mercuryspace...
>