I’m being dense today, but can anyone think of a real-world application
of SingleForwardable? I keep trying to come up with examples, but I
haven’t yet discovered any that aren’t better written as methods inside
the class itself.
Cheers
Dave
I’m being dense today, but can anyone think of a real-world application
of SingleForwardable? I keep trying to come up with examples, but I
haven’t yet discovered any that aren’t better written as methods inside
the class itself.
Cheers
Dave
I’m being dense today, but can anyone think of a real-world application
of SingleForwardable? I keep trying to come up with examples, but I
haven’t yet discovered any that aren’t better written as methods inside
the class itself.
The only usage I can think of is unittesting. Imagine that the Values
class is some class that we didn’t wrote and that we somehow needs to
populate it with data.
ruby a.rb
#<Values:0x810a5e4 @values=[1, 2]>
expand -t2 a.rb
class Values
def initialize
@values =
end
attr_reader :values
end
require ‘forwardable’
v = Values.new
v.extend SingleForwardable
v.def_delegator(“@values”, :<<, “append”)
v.append(1)
v.append(2)
p v
I don’t know if this can be useful… some kind of mockobject is probably
better to use in this case.
On Tue, 23 Mar 2004 02:15:18 +0900, Dave Thomas wrote:
–
Simon Strandgaard
I’m being dense today, but can anyone think of a real-world application
of SingleForwardable? I keep trying to come up with examples, but I
haven’t yet discovered any that aren’t better written as methods inside
the class itself.The only usage I can think of is unittesting. Imagine that the Values
class is some class that we didn’t wrote and that we somehow needs to
populate it with data.ruby a.rb
#<Values:0x810a5e4 @values=[1, 2]>
expand -t2 a.rb
class Values
def initialize
@values =
end
attr_reader :values
endimagine that this is a testcase, located in a
different file.
require ‘forwardable’
v = Values.new
v.extend SingleForwardable
v.def_delegator(“@values”, :<<, “append”)
v.append(1)
v.append(2)
p vI don’t know if this can be useful… some kind of mockobject is probably
better to use in this case.
Here is the opposite example: Trying to reopen a class inside a method
isn’t possible.
ruby a.rb
a.rb:10: class definition in method body
expand -t2 a.rb
class Values
def initialize
@values =
end
attr_reader :values
end
class Test
def test_values
class Values
def append(element)
@values << element
end
end
v = Values.new
v.append(1)
v.append(2)
p v
end
end
Test.new.test_values
On Mon, 22 Mar 2004 20:26:18 +0100, Simon Strandgaard wrote:
On Tue, 23 Mar 2004 02:15:18 +0900, Dave Thomas wrote:
–
Simon Strandgaard