>>>>> "S" == Stephen White <spwhite / chariot.net.au> writes:


S>   class Fred
S>     attr_accessor :var
S>     def exec
S>       @var
S>     end
S>   end

S>   a = Fred.new.var 10       -> 10
S>   b = a + 20                -> 30
S>   c = 30 + a                -> 40

S>   a = Fred.new.var "12345"  -> "12345"
S>   b = a + "hello"           -> "12345hello"
S>   c = "world" + a           -> "world12345"

S> because the interpreter reads in the source, finds "a" by itself, and calls
S> the "exec" method automatically. I can hook in and define how I want this
S> variable to behave - in this case, acting exactly like @val.

S> This is something I can't do in Ruby at the moment, therefore my classes
S> will never be able to do as much as the built-in's.

pigeon% cat b.rb
#!/usr/bin/ruby
class Fred
   class ProcFred < Proc
      def inspect
         call.inspect
      end
      def method_missing(a, *t)
         call.send(a, *t)
      end
   end
   def var(a) 
      @a = a
      ProcFred.new { @a }
   end
end
 
p a = Fred.new.var(10)
p b = a + 20           
p c = 30 + a           
 
p a = Fred.new.var("12345")
p b = a + "hello"         
p c = "world" + a         
pigeon% 

pigeon% b.rb
10
30
40
"12345"
"12345hello"
"world12345"
pigeon% 



Guy Decoux