Trying to work some attr_accessor - style magic so I can refactor a
bunch of menial methods into a single declaration. Can’t subclass, so
I’m trying to do it with a module.
In the code below, I expect both of the “puts” lines to work on the
"abc" string, but I can’t figure out what I’m missing. Can anyone
enlighten me here?
module FooModule
def delegate(*methods)
methods.each {|method|
module_eval %{
def #{method}
@new_receiver.#{method}
end
}
}
end
module_function :delegate
end
class TryIt
include FooModule
FooModule.delegate :intern
delegate :capitalize
def initialize(obj)
@new_receiver = obj
end
end
t = TryIt.new(“abc”)
puts t.intern
puts t.capitalize
···
=====
$ ruby demo.rb
demo.rb:17: undefined method `delegate’ for TryIt:Class (NoMethodError)
(now comment out the line “delegate :capitalize”)
$ ruby demo.rb
abc
demo.rb:25: undefined method `capitalize’ for #<TryIt:0x283c8
@new_receiver=“abc”> (NoMethodError)
–
Ryan “John” Platte
Custom services, NIKA Consulting
http://nikaconsulting.com/
batsman@tux-chan:/tmp$ cat fdgdf.rb
module FooModule
def delegate(*methods)
methods.each do |method|
define_method(method){|*a| @new_receiver.send(method, *a) }
end
end
end
class TryIt
extend FooModule
delegate :intern
delegate :capitalize
def initialize(obj)
@new_receiver = obj
end
end
t = TryIt.new(“abc”)
puts t.intern
puts t.capitalize
batsman@tux-chan:/tmp$ ruby fdgdf.rb
abc
Abc
···
On Sat, Apr 17, 2004 at 07:32:07AM +0900, John Platte wrote:
Trying to work some attr_accessor - style magic so I can refactor a
bunch of menial methods into a single declaration. Can’t subclass, so
I’m trying to do it with a module.
In the code below, I expect both of the “puts” lines to work on the
“abc” string, but I can’t figure out what I’m missing. Can anyone
enlighten me here?
–
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
The documentation is in Japanese. Good luck.
– Rich $alz
Thank you, Mauricio. This was indeed the fix I needed.
I guess I don’t understand the distinction between “include” and
“extend” from the descriptions in the Pickaxe. There’s apparently a
conceptual difference?
···
On 2004 Apr 16, at 17:42, Mauricio Fernández wrote:
Trying to work some attr_accessor - style magic so I can refactor a
bunch of menial methods into a single declaration. Can’t subclass, so
I’m trying to do it with a module…I can’t figure out what I’m
missing…
extend FooModule
–
Ryan “John” Platte
Custom services, NIKA Consulting
http://nikaconsulting.com/
obj.extend Foo
works like
class << obj; include Foo end
···
On Tue, Apr 20, 2004 at 06:21:27AM +0900, John Platte wrote:
On 2004 Apr 16, at 17:42, Mauricio Fernández wrote:
Trying to work some attr_accessor - style magic so I can refactor a
bunch of menial methods into a single declaration. Can’t subclass, so
I’m trying to do it with a module…I can’t figure out what I’m
missing…
extend FooModule
Thank you, Mauricio. This was indeed the fix I needed.
I guess I don’t understand the distinction between “include” and
“extend” from the descriptions in the Pickaxe. There’s apparently a
conceptual difference?
–
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Linux is obsolete
– Andrew Tanenbaum