Ruby/Ror: Calling Original Method via Super()

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.

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

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.

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.

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

difference between calling super and calling super()

When you call super with no arguments, Ruby sends a message to the parent of the current object, asking it to invoke a method with the same name as where you called super from, along with the arguments that were passed to that method.

On the other hand, when called with super(), it sends no arguments to the parent.

If the arguments you have don't match what the parent is expecting, then I would say you would want to use super(), or explicitly list parameters in the functional call to match a valid parent constructor.

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 another method in super class in ruby

class B < A

alias :super_a :a

def a
b()
end
def b
super_a()
end
end

Calling super from module method

Calling super looks for the next method in the method lookup chain. The error is telling you exactly what you are doing here: there is foobar method in the method lookup chain for Foo, since it is not inheriting from anything. The code you show in your example is just a redefinition of the Foo module, so having the first Foo does nothing.



Related Topics



Leave a reply



Submit