For (auto-)educational purposes, I'm trying to mimic the super behavior to learn how it works.
I could mimic super for instance methods, but I couldn't do it for class methods.
Here is my code:
class A
def aa
#msg ||= 'Original...: '
puts "#{#msg}#{self}.aa: #{self.class} < #{self.class.superclass}"
end
def self.ab
#msg ||= 'Original...: '
puts "#{#msg}#{self}.ab: #{self} < #{self.superclass}"
end
end
class B < A
def aa
#msg = "Real super.: "
super
end
def self.ab
#msg = "Real super.: "
super
end
def mimic_aa
#msg = "Mimic super: "
self.class.superclass.instance_method(:aa).bind(self).call
end
def self.mimic_ab
#msg = "Mimic super: "
#superclass.method(:ab).unbind.bind(self).call
#=> Error: singleton method only works in original object
#superclass.ab
#=> self is A; I want self to be B
proc = superclass.method(:ab).to_proc
#self.instance_eval(&proc)
#=> ArgumentError: instance_eval seems to call aa(some_unwanted_param)
# Note: Ruby 1.8.7
#eval('proc.call', binding)
#=> self is A; I want self to be B
end
end
a = A.new
b = B.new
a.aa #=> Original...: #<A:0xb77c66ec>.aa: A < Object
b.aa #=> Real super.: #<B:0xb77c6624>.aa: B < A
b.mimic_aa #=> Mimic super: #<B:0xb77c6624>.aa: B < A
puts ''
A.ab #=> Original...: A.ab: A < Object
B.ab #=> Real super.: B.ab: B < A
B.mimic_ab #=> (expected the same as above)
Any ideas?
Well, the problem is probably the version of Ruby that you are using. I'm running 1.9.2, and the program runs as expected. I think (from the comment in your code) that the problem is that you are running Ruby v1.8.7. It also wouldn't hurt to try running your code again.
Related
I want to execute a method of instance ofA on instance of B, where A and B are not related, independent classes.
a = A.new
b = B.new
b.<method_of_a>
Insane, senseless way:
class A
def a_method
'I am instance of A'
end
end
class B
def method_missing(method_name)
if method_name.to_s =~ /a_method/
A.instance_method(method_name).bind(self).call
else
super
end
end
end
B.new.a_method
#=> "I am instance of A"
Sane, idiomatic way:
module CommonMethods
def common_method
'I am available for all includers'
end
end
class A
include CommonMethods
end
class B
include CommonMethods
end
a = A.new
b = B.new
a.common_method
#=> "I am available for all includers"
b.common_method
#=> "I am available for all includers"
class A
def yay!
puts "¡YAY!"
end
end
b = A.new
A.instance_method(:yay!).bind(b).()
#⇒ "¡YAY!"
Some background: I have an external library that uses explicit type checking instead of duck-typing in one of its methods. Something like:
def a_method(value)
case value
when Array then 'an Array'
when Hash then 'a Hash'
when Foo then 'a Foo'
end
end
Foo is defined in the library. I would like to pass another object to this method that should be treated like a Foo. Therefore, I'm subclassing Foo:
class Bar < Foo
end
which works just fine:
bar = Bar.new
a_method(bar)
#=> 'a Foo'
Unfortunately, Foo implements several methods that will break the way Bar is supposed to work, including method_missing and respond_to?. For example:
class Foo
def respond_to?(method_name)
false
end
end
Because Foo is Bar's superclass, Foo#respond_to? is invoked when calling Bar#respond_to?:
class Bar < Foo
def hello
end
end
bar = Bar.new
bar.respond_to?(:hello) #=> false
bar.method(:respond_to?) #=> #<Method: Bar(Foo)#respond_to?>
I would like to remove or bypass Foo's method in this case (i.e. from within Bar) so that:
bar.respond_to?(:hello) #=> true
bar.method(:respond_to?) #=> #<Method: Bar(Kernel)#respond_to?>
just as if Foo#respond_to? did not exist.
Any suggestions?
Suppose you had this class structure:
class A
def respond_to?(meth)
"A"
end
def cat
end
def tap
puts "tap in A"
end
end
class B < A
def respond_to?(meth)
"B"
end
end
class C < B
def respond_to?(meth)
"C"
end
end
class D < C
end
We have:
D.ancestors
#=> [D, C, B, A, Object, Kernel, BasicObject]
Further, you want:
D.new.respond_to?(:cat)
#=> true
If we write:
class D < C
def respond_to?(meth)
method(__method__).super_method.call(meth)
end
end
then:
D.new.respond_to?(:cat)
#=> C
This is not surprising, since it's the same as:
class D < C
def respond_to?(meth)
super
end
end
D.new.respond_to?(:cat)
#=> C
Now try:
class D < C
def respond_to?(meth)
method(__method__).super_method.super_method.call(meth)
end
end
then:
D.new.respond_to?(:cat)
#=> B
so we have skipped over C. Now redefine D#respond_to? as follows:
class D < C
def respond_to?(meth)
method(__method__).super_method.super_method.super_method.call(meth)
end
end
D.new.respond_to?(:cat)
#=> A
Once more:
class D < C
def respond_to?(meth)
method(__method__).super_method.super_method.super_method.super_method.call(meth)
end
end
D.new.respond_to?(:cat)
#=> True (invokes Kernel#respond_to?)
Therefore, you could do the following:
module JumpOver
def jump_over(over_mod, meth)
m = instance_method(meth)
loop do
m = m.super_method
break if m.owner > over_mod
end
define_method(meth, m)
end
end
class D
extend JumpOver
jump_over(A, :respond_to?)
jump_over(A, :tap)
end
D.methods.include?(:jump)
#=> true
D.instance_methods(false)
#=> [:respond_to?, :tap]
d = D.new
#=> #<D:0x007ff263161b58>
d.respond_to? :cat
#=> true
d.respond_to? :dog
#=> false
d.tap { |o| puts 'hi' }
# 'hi'
#=> #<D:0x007ff263161b58>
I don't understand the problem. Just implement a respond_to? in Bar and don't call super.
class Foo
def respond_to?(method)
puts 'in foo'
false
end
end
class Bar < Foo
def respond_to?(method)
puts 'in bar'
true
end
end
bar = Bar.new
bar.respond_to?(:quux) # => true
# >> in bar
This is, of course, a violation of LSP, but you specifically asked for it, so... :)
Ruby's method lookup seems to be "hard-coded". I couldn't find a way to alter it from within Ruby.
Based on Sergio Tulentsev's suggestion to re-implement the methods, however, I came up with a helper method to replace / overwrite every superclass (instance) method with its "super" method (or undefine it if there is none):
def self.revert_superclass_methods
superclass.instance_methods(false).each do |method|
super_method = instance_method(method).super_method
if super_method
puts "reverting #{self}##{method} to #{super_method}"
define_method(method, super_method)
else
puts "undefining #{self}##{method}"
undef_method(method)
end
end
end
This has basically the same effect for my purposes. Example usage:
module M
def foo ; 'M#foo' ; end
end
class A
include M
def to_s ; 'A#to_s' ; end
def foo ; 'A#foo' ; end
def bar ; 'A#bar' ; end
end
class B < A
def self.revert_superclass_methods
# ...
end
end
b = B.new
b.to_s #=> "A#to_s"
b.foo #=> "A#foo"
b.bar #=> "A#bar"
B.revert_superclass_methods
# reverting B#to_s to #<UnboundMethod: Object(Kernel)#to_s>
# reverting B#foo to #<UnboundMethod: Object(M)#foo>
# undefining B#bar
b.to_s #=> "#<B:0x007fb389987490>"
b.foo #=> "M#foo"
b.bar #=> undefined method `bar' for #<B:0x007fb389987490> (NoMethodError)
I have two classes (I can't combine them), each in their own file.
File number 1:
class A
def say_hi
puts "Hi"
end
end
File number 2:
class B
def say_bye
puts "bye"
end
end
I can do:
apple = A.new
apple.say_hi
Or:
baby = B.new
apple.say_bye
But what if I want to do:
apple = A.new
apple.say_bye
Or:
baby = B.new
baby.say_hi
Is there a simple way to do that without restructuring my classes?
If it's a case of "which class I should instantiate?" and all you know is you want method "say_hi" then you could do...
method = :say_hi
if A.new.respond_to?(method)
runner_object = A.new
else
runner_object = B.new
end
active support in Rails lets you do...
result = A.new.try(:say_hi) || B.new.try(:say_hi)
If I understand correctly, you need to call these different methods from a flow which gets either one of these classes.
The easiest way to do this is by using alias_method:
class A
def say_hi
puts "Hi"
end
alias_method :say, :say_hi
end
class B
def say_bye
puts "bye"
end
alias_method :say, :say_bye
end
apple = A.new
apple.say
# => "Hi"
baby = B.new
baby.say
# => "bye"
You can even do it if you can't actually change the class code, by using class_eval:
A.class_eval { alias_method :say, :say_hi }
B.class_eval { alias_method :say, :say_bye }
Just for fun, again, but is it possible to take a block that contains method definitions and add those to an object, somehow? The following doesn't work (I never expected it to), but just so you get the idea of what I'm playing around with.
I do know that I can reopen a class with class << existing_object and add methods that way, but is there a way for code to pass that information in a block?
I guess I'm trying to borrow a little Java thinking here.
def new(cls)
obj = cls.new
class << obj
yield
end
obj
end
class Cat
def meow
puts "Meow"
end
end
cat = new(Cat) {
def purr
puts "Prrrr..."
end
}
cat.meow
# => Meow
# Not working
cat.purr
# => Prrrr...
EDIT | Here's the working version of the above, based on edgerunner's answer:
def new(cls, &block)
obj = cls.new
obj.instance_eval(&block)
obj
end
class Cat
def meow
puts "Meow"
end
end
cat = new(Cat) {
def purr
puts "Prrrr..."
end
}
cat.meow
# => Meow
cat.purr
# => Prrrr...
You can use class_eval(also aliased as module_eval) or instance_eval to evaluate a block in the context of a class/module or an object instance respectively.
class Cat
def meow
puts "Meow"
end
end
Cat.module_eval do
def purr
puts "Purr"
end
end
kitty = Cat.new
kitty.meow #=> Meow
kitty.purr #=> Purr
kitty.instance_eval do
def purr
puts "Purrrrrrrrrr!"
end
end
kitty.purr #=> Purrrrrrrrrr!
Yes
I suspect you thought of this and were looking for some other way, but just in case...
class A
def initialize
yield self
end
end
o = A.new do |o|
class << o
def purr
puts 'purr...'
end
end
end
o.purr
=> purr...
For the record, this isn't the usual way to dynamically add a method. Typically, a dynamic method starts life as a block itself, see, for example, *Module#define_method*.
Sorry for the poor title, I don't really know what to call this.
I have something like this in Ruby:
class Test
def initialize
#my_array = []
end
attr_accessor :my_array
end
test = Test.new
test.my_array << "Hello, World!"
For the #my_array instance variable, I want to override the << operator so that I can first process whatever is being inserted to it. I've tried #my_array.<<(value) as a method in the class, but it didn't work.
I think you're looking for this:
class Test
def initialize
#myarray = []
class << #myarray
def <<(val)
puts "adding #{val}" # or whatever it is you want to do first
super(val)
end
end
end
attr_accessor :myarray
end
There's a good article about this and related topics at Understanding Ruby Singleton Classes.
I'm not sure that's actually something you can do directly.
You can try creating a derived class from Array, implementing your functionality, like:
class MyCustomArray < Array
def initialize &process_append
#process_append = &process_append
end
def << value
raise MyCustomArrayError unless #process_append.call value
super.<< value
end
end
class Test
def initialize
#my_array = MyCustomArray.new
end
attr_accessor :my_array
end
Here you go...
$ cat ra1.rb
class Aa < Array
def << a
puts 'I HAVE THE CONTROL!!'
super a
end
end
class Test
def initialize
#my_array = Aa.new
end
attr_accessor :my_array
end
test = Test.new
test.my_array << "Hello, World!"
puts test.my_array.inspect
$ ruby ra1.rb
I HAVE THE CONTROL!!
["Hello, World!"]
$
a = []
a.instance_eval("alias old_add <<; def << value; puts value; old_add(value); end")
Very hackish, and off the top of my head ...
Just change 'puts value' with whatever preprocessing you want to do.
You can extend the metaclass of any individual object, without having to create a whole new class:
>> i = []
=> []
>> class << i
>> def <<(obj)
>> puts "Adding "+obj.to_s
>> super
>> end
>> end
=> nil
>> i << "foo"
Adding foo
=> ["foo"]
i extend the class, creating a method which provides access to the instance variable.
class KeywordBid
def override_ignore_price(ignore_price)
#ignorePrice = ignore_price
end
end