[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

challenge

Ara.T.Howard

7/13/2006 9:24:00 PM

21 Answers

Ara.T.Howard

7/13/2006 9:28:00 PM

0

Pit Capitain

7/13/2006 10:13:00 PM

0

ara.t.howard@noaa.gov schrieb:
> On Fri, 14 Jul 2006 Ara.T.Howard@noaa.gov wrote:
>>
>> OPEN = File.method 'open'
>>
>> class File
>> def self.open(*a) 42 end
>> end
>> end
>>
>> ## now restore File.open using OPEN - remember that open takes a block...
>
> correction. start here
>
> open_m = File.method 'open'
>
> class File
> def self.open(*a) 42 end
> end
> end
>
> using a const makes it too easy and will accomplish what i need to do.

class << File; self; end.class_eval do
remove_method :open
end

Regards,
Pit

Ara.T.Howard

7/13/2006 10:33:00 PM

0

Marcin Mielzynski

7/13/2006 10:40:00 PM

0

ara.t.howard@noaa.gov wrote:

> wow. learn something every day!
>
> ~ > cat a.rb
> class File
> def File.open(*a) 42 end
> end
>
> p File.open(__FILE__)
>
> class << File; self; end.class_eval do
> remove_method :open
> end
>
> p File.open(__FILE__)
>
>
>
> ~ > ruby a.rb
> 42
> #<File:a.rb>
>
>
> i wasn't aware remove_method had this 'stacklike' quality.
>

The open method is in fact defined in IO class which is extended by File
class. In your case another method for File eigenclass class was defined

lopex

Pit Capitain

7/13/2006 10:47:00 PM

0

ara.t.howard@noaa.gov schrieb:
> wow. learn something every day!
>
> ~ > cat a.rb
> class File
> def File.open(*a) 42 end
> end
>
> p File.open(__FILE__)
>
> class << File; self; end.class_eval do
> remove_method :open
> end
>
> p File.open(__FILE__)
>
>
>
> ~ > ruby a.rb
> 42
> #<File:a.rb>
>
>
> i wasn't aware remove_method had this 'stacklike' quality.

Ara, I just called

p open_m

and noticed that "open" wasn't a method of class "File", but of class
"IO"! So to get back to the normal behaviour it was enough to remove the
new method of class "File".

This is even shorter:

class << File
remove_method :open
end

The "class_eval" part isn't necessary.

Regards,
Pit

Ara.T.Howard

7/13/2006 10:48:00 PM

0

Trans

7/14/2006 1:24:00 AM

0


ara.t.howard@noaa.gov wrote:
> On Fri, 14 Jul 2006 Ara.T.Howard@noaa.gov wrote:
>
> >
> > OPEN = File.method 'open'
> >
> > class File
> > def self.open(*a) 42 end
> > end
> > end
> >
> >
> > ## now restore File.open using OPEN - remember that open takes a block...
>
> correction. start here
>
> open_m = File.method 'open'
>
> class File
> def self.open(*a) 42 end
> end
> end
>

(class << File; self; end).instance_eval {
define_method(:open,&open_m)
}

?

T.


Ara.T.Howard

7/14/2006 1:59:00 AM

0

Sean O'Halpin

7/14/2006 2:44:00 AM

0

On 7/14/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> On Fri, 14 Jul 2006 transfire@gmail.com wrote:
>
> > (class << File; self; end).instance_eval {
> > define_method(:open,&open_m)
> and the block?
>
> -a
> --
> suffering increases your inner strength. also, the wishing for suffering
> makes the suffering disappear.
> - h.h. the 14th dali lama
>
I had to cheat ;)

File::OPEN_M = open_m
class File
eval "
def self.open(*args, &block)
OPEN_M.call(*args, &block)
end
"
end

The problem is - as I'm sure you know - that define_method doesn't
honour the block argument of the method reference. I'd be very
interested to see if you can do it cleanly in ruby 1.8.

Regards,
Sean

Sean O'Halpin

7/14/2006 12:08:00 PM

0

On 7/14/06, Robert Dober <robert.dober@gmail.com> wrote:
> On 7/14/06, Sean O'Halpin <sean.ohalpin@gmail.com> wrote:
> >
> > On 7/14/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> > > On Fri, 14 Jul 2006 transfire@gmail.com wrote:
> > >
> > > > (class << File; self; end).instance_eval {
> > > > define_method(:open,&open_m)
> > > and the block?
> > >
> > > -a
> > > --
> > > suffering increases your inner strength. also, the wishing for
> > suffering
> > > makes the suffering disappear.
> > > - h.h. the 14th dali lama
> > >
> > I had to cheat ;)
> >
> > File::OPEN_M = open_m
> > class File
> > eval "
> > def self.open(*args, &block)
> > OPEN_M.call(*args, &block)
> > end
> > "
> > end
> >
> > The problem is - as I'm sure you know - that define_method doesn't
> > honour the block argument of the method reference. I'd be very
> > interested to see if you can do it cleanly in ruby 1.8.
> >
> > Regards,
> > Sean
> >
> >
> Block behavior is preserved though when we store a reference to a method
> e.g.
> ----------------------- 8< ------------------------------
> class X
> def x(*args,&block)
> block.call(1764) if block
> end
> end
>
> OLD = X.instance_method :x
>
> class X
> def x; "rubbish"; end
> end
>
> class X
>
> define_method(:x, OLD )
>
> end
>
> X.new.x{ |a| puts a }
> ----------------------- >8 ----------------------------------
>
> That should do the trick, no?
> But I *cannot* make it work with IO :(, some internal magic, maybe?
>
> Cheers
> Robert
>
>
Excellent. Don't use the & to convert the Proc into a block - just use
the method reference directly.

Are you getting the "singleton method called for a different object
(TypeError)" error?

Thanks,
Sean