Simple Ruby reference
It's a really simple reference implementation, used in failirc.
>> require 'refr'
true
>> lol = 2
2
>> ref = Reference{:lol}
2
>> ref =~ "what"
"what"
>> lol
"what"
This is useful especially in an event dispatcher, you can use a reference object as
it was the real object, the only stolen methods are: ___get_referenced___
, ___set_referenced___
, ~
and =~
,
everything else is sent to the referenced object.
You can also create a reference to an object directly with Reference.[]
Or you can use the Kernel#Reference
helper to achieve both.
Cases where the Reference object doesn't work as expected
There are some situations where the Reference object doesn't work exactly as the object it's referencing,
this cases are most likely related to C methods, when this happens just remember to use the ~
method
on the reference to get the referenced object.
-
In the
case
statement when comparing for the object's class it doesn't work due toModule#===
being implemented in C. -
IO#==
andIO#===
don't work as expected, this happens to be a problem when usingArray#delete
with a referenced IO object.
If you want to compare an IO object to a Reference:IO you have to doref == io
and notio == ref
or it won't work correctly.
Keep in mind that if the thing you compare with is a Reference:IO too you're not going to get what you're expecting, the comparing value has to be a real IO object. -
Same of
IO#==
happens forSymbol
-
Some weird problems seem to happen when returning a Reference from a to_s method.
If you find any other situations where it's not working as expected open an issue or send me an email and I'll add it here.