[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[ANN] pervasives-1.1.0

ara.t.howard

9/25/2007 4:43:00 AM



NAME

pervasives.rb

SYNOPSIS

access to pristine object state. if you don't metaprogram or write
debuggers you probably don't need it.

INSTALL

gem install pervasives

URIS

http://rubyforge.org/projects/code...
http://codeforpeople.co...

HISTORY

1.1.0

added Pervasives.call method, refactored a bit, updated samples,
updated
tests for new api

1.0.0

is NOT backward compatible with any other pervasives version.
the new
library is __greatly__ simplified

SAMPLES

<========< samples/a.rb >========>

~ > cat samples/a.rb

#
# Pervasives allows an object's methods to be accessed in a
pristine state,
# even when some effort has been made to derride them
#
require 'pervasives'

class BlankSlate
instance_methods.each{|m| undef_method m unless m[%r/__/]}
end

bs = BlankSlate.new

p Pervasives(bs).methods #=> ["__object_pervasive__",
"__id__", "__send__", "__pervasive__"]

p Pervasives(bs).is_a?(BlankSlate) #=> true

p Pervasives(bs).instance_eval{ @a = 42 } #=> 42

p Pervasives(bs).instance_variables #=> ["@a"]

~ > ruby samples/a.rb

["__pervasive__", "__id__", "__send__"]
true
42
["@a"]


<========< samples/b.rb >========>

~ > cat samples/b.rb

#
# the special method __pervasive__ is added to all objects - it
can be used to
# access the pristine state
#
require 'pervasives'

class C
def instance_eval(*a, &b) raise end
end

c = C.new

c.__pervasive__(:instance_eval){ @a = 42 }

p c.instance_variables #=> ["@a"]

~ > ruby samples/b.rb

["@a"]


<========< samples/c.rb >========>

~ > cat samples/c.rb

#
# even if hackery removes the __pervasive__ method you can still
call a
# pristine method on an object
#
require 'pervasives'

class VeryBlankSlate
instance_methods.each{ |m| undef_method m }
end

vbs = VeryBlankSlate.new

begin
vbs.__pervasive__
rescue
'nope not even that'
end

Pervasives.call(vbs, :instance_eval){ @a = 42 }

p Pervasives.call(vbs, :instance_variables) #=> ["@a"]

~ > ruby samples/c.rb

samples/c.rb:8: warning: undefining `__id__' may cause serious
problem
samples/c.rb:8: warning: undefining `__send__' may cause serious
problem
["@a"]


<========< samples/d.rb >========>

~ > cat samples/d.rb

#
# of course it works for classes and modules too
#
require 'pervasives'

class C
def self.new() raise end

def inspect() 42.inspect end
end

c = C.__pervasive__ :new

p c #=> 42

~ > ruby samples/d.rb

42



enjoy.

a @ http://draw...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




1 Answer

Robert Dober

9/25/2007 9:35:00 AM

0

On 9/25/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
Ara,
does this fix the bug in prototype? It was in attributes IIRC, so I am not sure.
I tried last about six weeks ago and it did not work yet.

Thx in advance.
Robert