Is the Current Ruby Method Called via Super

Is the current Ruby method called via super?

The ultimate mix between my other, @mudasobwa's and @sawa's answers plus recursion support:

module SuperDetector
def self.included(clazz)
unless clazz.instance_methods.include?(:via_super?)
clazz.send(:define_method, :via_super?) do
first_caller_location = caller_locations.first
calling_method = first_caller_location.base_label

same_origin = ->(other_location) do
first_caller_location.lineno == other_location.lineno and
first_caller_location.absolute_path == other_location.absolute_path
end

location_changed = false
same_name_stack = caller_locations.take_while do |location|
should_take = location.base_label == calling_method and !location_changed
location_changed = !same_origin.call(location)
should_take
end

self.kind_of?(clazz) and !same_origin.call(same_name_stack.last)
end
end
end
end

The only case that wont work (AFAIK) is if you have indirect recursion in the base class, but I don't have ideas how to handle it with anything short of parsing the code.

What is `super` in Ruby?

super method calls the parent class method.

for example:

class A
def a
# do stuff for A
end
end

class B < A
def a
# do some stuff specific to B
super
# or use super() if you don't want super to pass on any args that method a might have had
# super/super() can also be called first
# it should be noted that some design patterns call for avoiding this construct
# as it creates a tight coupling between the classes. If you control both
# classes, it's not as big a deal, but if the superclass is outside your control
# it could change, w/o you knowing. This is pretty much composition vs inheritance
end
end

If it is not enough then you can study further from here

Does calling super() cause further methods in the parent class to be used?

The proof of the pudding is in the eating.

class Parent
def foo; p self; bar; end # This calls bar on the current object
def bar; puts "parent bar"; end
end

class Child < Parent
def foo; super; end # Removing this line changes nothing
def bar; puts "child bar"; end
end

Child.new.foo
#=> #<Child:0x007f980b051f40>
#=> child bar # NOTE! Not "parent bar"

Calling super doesn't change the self, as seen above. As such, methods you call on self (explicitly or implicitly, by not providing a receiver) still act upon the original instance, and use it for method lookup.


Calling super() is equivalent to calling:

self.class.superclass.instance_method(__method__).bind(self).call

…which helps to illustrate that you are calling the implementation of the method as though it is on the current instance. Note also that super is not the same as super(), since the former will magically pass along whatever parameters were supplied to the current method.

Ruby/RoR: calling original method via super()?

You want:

super(attrs)

That will call the original method, passing attrs as an argument to it.

As it is now, you're trying to call update_attributes on the "true" value returned by the original update_attributes.

Super keyword in Ruby

no... super calls the method of the parent class, if it exists. Also, as @EnabrenTane pointed out, it passes all the arguments to the parent class method as well.

How do I call a super class method

In Ruby 2.2, you can use Method#super_method now

For example:

class B < A
def foo
super + " world"
end

def bar
method(:foo).super_method.call
end
end

Ref: https://bugs.ruby-lang.org/issues/9781#change-48164 and https://www.ruby-forum.com/topic/5356938

Calling super from a class method

Super works for child classes inherited from a parent class.

In your case alias is the only way. Or you can use alias_method:

alias_method :old_hi, :hi

def self.hi
old_hi
puts "Oh!"
end

What does the method super do in the ruby code?

I think I know super intends to override the same method inherited from the parent class.

Actually, it is the opposite. When you override some method in a child class, super is used to refer to the behaviour of the same method in the parent class (i.e., the original behaviour).

In the given code, the indent is to make it dynamically respondable to any method that starts with foo without changing the respondability of other methods. The first part is done by:

if method_name.to_s[0,3] == "foo"
true

but if it were only that, all other undefined methods would simply return nil even if they are defined in some parent class. If some parent class were to respond to such method, then returning nil to respondability would incorrectly block such methods. The other part:

else
super(method_name)

is to return the correct value for such case. It means that in other cases, do what the parent class did.

Why is 'super' a keyword rather than a method in Ruby?

It behaves a little differently, in that if you don't pass arguments, all of the current arguments (and block, if present) are passed along... I'm not sure how that would work as a method.

To give a rather contrived example:

class A
def example(a, b, c)
yield whatever(a, b) + c
end
end

class B < A
def example(a, b, c)
super * 2
end
end

I did not need to handle the yield, or pass the arguments to super. In the cases where you specifically want to pass different arguments, then it behaves more like a method call. If you want to pass no arguments at all, you must pass empty parentheses (super()).

It simply doesn't have quite the same behaviour as a method call.



Related Topics



Leave a reply



Submit