Memoirs Of A Jr Dev


I was so grateful this week to learn a brand new concept (to me) via my awesome coworkers. I should just call them mentors cos JEEZ.
Image result for mentor gif funny
I was reviewing a PR and wanted to know about the super call I saw in a method. So, I reached out to the author and asked. Here is what happened.
The method looked like this
def initialize(access_key:, formatter: default_user_defined_fields_formatter, customer_id: nil)
super(access_key, formatter)
@customer_id = customer_id || Site::THE_SITE_ID
end

So what is super and how is it used here?
The explanation is authored by our Technical Lead Fernando. Fernando actually said “let me look for a good resource”, and when he couldn’t find one, I realised HE is the best resource, so rather than link you to a crazy Stack O I am simply copy/pasting our Slack convo, using general naming conventions.

Fernando: Each time you invoke a method inside an object, a mechanism called method lookup is triggered
this means that ruby will try to find the implementation of that method, starting with the object itself and then moving up in the hierarchy of inherited/included methods.
Me:So far I am following you!
Image result for bubbling up gif funny
F: So let’s say we have:
class A
def method_1
return 'hi'
end
end

class B < A
def method_1
return 'hola'
end
end

A.new.method_1 => 'hi'
B.new.method_1 => 'hola'

F: both classes implement method_1 so, no big deal, but, what if…
Image result for what if gif funny

class A
def method_1
return 'hi'
end
end

class B < A
end
A.new.method_1 => 'hi'
B.new.method_1 => 'hi'

F: method_1 doesn't exist directly on B but, as B inherits from A, it has a method_1 method available.

Me: STILL following.
Image result for I'm following gif funny

F: now, the super part. What if… you need to reuse method_1 defined in A, but you also need to add some custom code to it? In other words, what if B needs to implement method_1 but at the same time reuse A‘s implementation?
introduce super
class A
def method_1
return 'hi'
end
end

class B < A
def method_1
super + 'Patricia'
end
end

A.new.method_1 => 'hi'
B.new.method_1 => 'hi Patricia'

F: super lets you access a method’s parent implementation within the same method from a child class, in other words, super gives you access to a method you inherited. I’m overriding the initialize method I inherited from my parent Parent::SomeClass::SITE::OTHER, in this case and reusing it in my own implementation of initialize in OTHER
Image result for I'm your father gif funny
Me: With the access_key, formatter in the initialize method, you are using these as parameters to super? or telling super these are children and find all the associated inheritances up the chain?

F: That’s a really good question
Me: Image result for happy dance gif funny
F: I use super in two different ways
super(access_key, formatter) and super, in the example you mention, they are just parameters, the parent’s implementation of initialize expects two parameters, then, I send two params.
Me: Image result for Mind blown gif funny

From this awesome exchange, I  NOW know of one way to use super, and look forward to implementing it in code I write. We didn’t get a chance to discuss the second way Fernando uses it, but I am certain we will!

Let me know in the comments if you have any questions, that way I can learn MOAR by explaining it.
Also, if you have an answer as to what Fernando is going to tell me about the second way he uses super send me a message or comment and we can chat about it!
Happy hacking!

Leave a comment

Your email address will not be published. Required fields are marked *