0

Is it possible to do something like:

class A
  def a(var)
    puts "do something with #{var}"
  end
end

class B < A
  def a(var)
    var = var + "some modification"
    #this is what I want to do:
    super.a(var)
  end
end

Thanks!

mfcabrera
  • 781
  • 10
  • 26
  • In your example is B class not a subclass of A and object doesn'T have a methode a, so what do you whant to do? – hotfix Apr 05 '13 at 22:14
  • http://stackoverflow.com/questions/3689736/rails-3-alias-method-chain-still-used may be related – rogerdpack Apr 05 '13 at 22:16

3 Answers3

4

Unless I'm misreading your question, you should be able to call super by itself; e.g.:

class A
  def a(var)
    puts "do something with #{var}"
  end
end

class B < A
  def a(var)
    var = var + "some modification"
    #this is what I want to do:
    #super.a(var)
    super
  end
end

v = B.new
v.a("hey")

produces

$ ruby test.rb
do something with heysome modification
Mark Westling
  • 5,904
  • 4
  • 26
  • 30
  • 2
    If you call `super` like this, it'll pass its arguments along to the parent function (usually what you want). You can also call `super()` (empty arg list) to invoke the parent function with no args, or `super("whatever")` to explicitly call the parent method with whatever args you want. – Jim Stewart Apr 05 '13 at 22:22
2

You cannot use a method name with the super call

If you use super on it's own then it will call the super class' implementation passing along the same arguments

class A
  def a(var)
    puts var
  end
end

class B < A
  def a(var)
    super
  end
end

B.new.a(1) #=> 1

If you want to modify the variable then you can either reassign the var arg or use a super call passing in arguments

class A
  def a(var)
    puts var
  end
end

class B < A
  def a(var)
    var = 2
    super
  end
end

B.new.a(1) #=> 2

or

class A
  def a(var)
    puts var
  end
end

class B < A
  def a(var)
    super(2)
  end
end

B.new.a(1) #=> 2
Paul.s
  • 38,494
  • 5
  • 70
  • 88
0

try the below:

class A
  def a(var)
    puts "do something with #{var}"
  end
end

class B < A
  def a(var)
    var = var + "some modification"
    #this is what I want to do:
    #super.a(var)
    self.class.superclass.instance_method(:a).bind(self).call 12
  end
end

v = B.new
v.a("hello")

Output:

do something with 12

Another one:

class A
  def a(var)
    puts "do something with #{var}"
  end
end

class B < A
    alias :old_a :a
  def a(var)
    var = var + "some modification"
    #this is what I want to do:
    #super.a(var)
    old_a 12
  end
end

v = B.new
v.a("hello")

Output:

do something with 12
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • I think this is solving a problem in an overly complicated way - see the other answers – Paul.s Apr 05 '13 at 23:21
  • @Paul.s `alias` is complicated way? Bummer I just shown OP how many ways we can do it. – Arup Rakshit Apr 05 '13 at 23:30
  • But why go through the extra effort and complexity when it's built into the language? If the OP doesn't know the core language that well then meta programming is completely out of reach. – Paul.s Apr 05 '13 at 23:39
  • **Down-voter:** why down vote? put reasons what wrong in my code. If you don't understand then it doesn't mean code is wrong.You are not capable. So read more more before judge other's codes. – Arup Rakshit Apr 06 '13 at 06:16
  • @Paul.s does `alias` is not ruby's one or `bind` is not. What do you mean? – Arup Rakshit Apr 06 '13 at 06:18
  • 1
    I mean the `super` keyword is built in for this purpose. So reimplementing what it does is just unhelpful and unclear – Paul.s Apr 06 '13 at 08:14