[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

inspecting the current definition of a method

Frank Mittelbach

2/2/2008 11:01:00 AM

Hi,

I'm fairly new to Ruby and this may be a RTFM but I couldn't find anything
in books or through a web search:

can one display the current definition of a method and as a step further can
one modify it? And if so how?

any pointers?

thanks
frank
4 Answers

Phlip

2/2/2008 12:34:00 PM

0

Frank Mittelbach wrote:

> can one display the current definition of a method

Unfortunately yes. You can use a library called 'rubynode' to reflect the
method's opcodes into a big array of hashes, describing each one.

> and as a step further
> can one modify it? And if so how?

I'm currently writing a package which disassembles those opcodes back
into Ruby code that resembles your source. One could conceivably perform
string-surgery on that, then eval() it _back_ into running Ruby.

However, Ruby is an extremely dynamic language, and it permits many
tricks using both basic OO programing, and meta-programming techniques
such as define_method. If you call define_method twice, with different
values in the variables around it each time, you can use those variables
inside the new method, and it will vary its behavior each time.

There's so many techniques to chose from you should report your original
problem, because it must have a better solution than disassembling a
method and performing string surgery on it!

--
Phlip

Frank Mittelbach

2/2/2008 12:51:00 PM

0

Phlip wrote:

>> can one display the current definition of a method
>
> Unfortunately yes. You can use a library called 'rubynode' to reflect the
> method's opcodes into a big array of hashes, describing each one.

why unfortunately? because it is not straight forward?

> However, Ruby is an extremely dynamic language, and it permits many
> tricks using both basic OO programing, and meta-programming techniques
> such as define_method. If you call define_method twice, with different
> values in the variables around it each time, you can use those variables
> inside the new method, and it will vary its behavior each time.

understood. it is precisely one of the reasons that given this flexibility I
would really like to be able "inspect" what a method in a given context
does. The modification of this behavior would then be an additional plus,
but just the plain seeing what the current definition is would be really
helpful in some cases.

but it seems that this is less straight forward than I have hoped

thanks
frank

Phlip

2/2/2008 1:14:00 PM

0

Frank Mittelbach wrote:

> Phlip wrote:
>
>>> can one display the current definition of a method
>>
>> Unfortunately yes. You can use a library called 'rubynode' to reflect
>> the method's opcodes into a big array of hashes, describing each one.
>
> why unfortunately? because it is not straight forward?

Because learning a sick but awesome technique invites abuse, if a simpler
mundane technique would have worked!

> understood. it is precisely one of the reasons that given this
> flexibility I would really like to be able "inspect" what a method in a
> given context does. The modification of this behavior would then be an
> additional plus, but just the plain seeing what the current definition
> is would be really helpful in some cases.

Here's a unit test from my project (to be viewed with a proportional
font):

def test_violate_law_of_demeter
assert_equal 'n[:b]', reflect_source{ n[:b] }
assert_equal '( n[:b] ).last()', reflect_source{ n[:b].last }

assert_equal '( n[:b] ).last().map(&:first)',
reflect_source{ n[:b].last.map(&:first) }
end

Notice the reassembler added extra () marks. Some disassemblies require
worse hacks!

I have not yet packaged this up (or finished all the opcodes!), but
here's the relevant files:

http://phlip.svnrepository.com/svn/yar_wiki/lib/ruby_re...
http://phlip.svnrepository.com/svn/yar_wiki/test/ruby_reflect...

They are for a new kind of assertion I'm inventing for developer tests:

http://phlip.svnrepository.com/svn/yar_wiki/lib/ass...

--
Phlip

The Peeler

6/6/2013 10:08:00 PM

0

On Thu, 6 Jun 2013 23:46:26 +0200, The Peeler
<finishingoff@themoronicRevd.invalid> wrote:

>On Thu, 06 Jun 2013 14:34:36 -0700, The Rectum, the resident psychopath of
>sci and scj, FAKING his time zone again and IMPERSONATING his master, The
>Peeler, wrote:
>
>
>>>
>>>LIberation D-Day, Canadian Army Newsreel - YouTube
>>
>> Army??? LOL!!!
>
>Barmy, The Rectum? Of course, I am! <BG>

Of, COURSE Grik anus! <G>