1

I have a piece of code

def data
  data_type == 'option' ? options[super.to_i] : super
end

I cannot figure out from where the super keyword is getting the value.

sawa
  • 165,429
  • 45
  • 277
  • 381
MKB
  • 777
  • 1
  • 9
  • 27

2 Answers2

2

Here super keyword calls the same data method of its parent class if data_type == 'option' is false. Check this link for a detailed explanation. Super keyword in Ruby

Update:

The above code can be re-written as

if data_type == 'option'
  options[super.to_i]#options is probably a hash/array here.
else
  super
end

When we call super it returns a value after executing it's parent class's data method, lets assume it returned "5", we're converting that to an integer and getting data out of options array. i.e., options[5].

In the else block we're simply returning the the value parent's data method got us.

Community
  • 1
  • 1
Kumar
  • 3,116
  • 2
  • 16
  • 24
  • Thanks for your quick reply Abinesh. I got your point about parent class. But thing I am confused with is what does it do within the specified array? – MKB Jun 14 '16 at 05:26
2

If both superclass and subclass have methods of the same name, the implementation of the subclass will prevail (inside the subclass). However, instead of overriding the implementation of the superclass, we might need to add extra functionality. Using the super keyword within the subclass allows us to do that; super calls the superclass implementation of the corresponding method. In other words, it allows the overriding method to call the overridden method.

class Zazen
  def meditate
    puts "Practicing Zazen…"
  end
end

class Sitting < Zazen
  def meditate
    puts "Sitting…"
    super # Calls the meditate method implemented in the parent class
    puts "Getting up…"
  end
end

s = Sitting.new
s.meditate
Output:
Sitting…
Practicing Zazen…
Getting up…

Notice how, in the example above, the statements from both meditate methods (implemented in both classes) were executed.

How super handles arguments

Regarding argument handling, the super keyword can behave in three ways:

When called with no arguments, super automatically passes any arguments received by the method from which it's called (at the subclass) to the corresponding method in the superclass.

class A
  def some_method(*args)
    puts "Received arguments: #{args}"
  end
end

class B < A
  def some_method(*args)
    super
  end
end

b = B.new
b.some_method("foo", "bar")     # Output: Received arguments: ["foo", "bar"]

If called with empty parentheses (empty argument list), no arguments are passed to the corresponding method in the superclass, regardless of whether the method from which super was called (on the subclass) has received any arguments.

class A
  def some_method(*args)
    puts "Received arguments: #{args}"
  end
end

class B < A
  def some_method(*args)
    super()  # Notice the empty parentheses here
  end
end

b = B.new
b.some_method("foo", "bar")     # Output: Received arguments: [ ]

When called with an explicit argument list, it sends those arguments to the corresponding method in the superclass, regardless of whether the method from which super was called (on the subclass) has received any arguments.

class A
  def some_method(*args)
    puts "Received arguments: #{args}"
  end
end

class B < A
  def some_method(*args)
    super("baz", "qux")  # Notice that specific arguments were passed here
  end
end

b = B.new
b.some_method("foo", "bar")     # Output: Received arguments: ["baz", "qux"]
BrunoF
  • 3,239
  • 26
  • 39