Skip to content

Instantly share code, notes, and snippets.

@redconfetti
Last active March 18, 2016 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redconfetti/5d774da6056ae413771a to your computer and use it in GitHub Desktop.
Save redconfetti/5d774da6056ae413771a to your computer and use it in GitHub Desktop.
Using super with class and instance methods
# Testing to see how 'super' method works with class and instance methods
# when passing pre-declared variables into the methods where I am expecting them to be modified
# Results noted provided by jruby-1.7.19
class SuperClass
def self.add_something(somehash)
somehash['something'] += 1
end
def add_something(somehash)
somehash['something'] += 5
end
end
class ChildClass < SuperClass
def self.add_something(somehash)
super(somehash)
somehash['something'] += 2
end
def add_something(somehash)
super(somehash)
somehash['something'] += 20
end
end
somehash = {'something' => 1}
SuperClass.add_something(somehash)
# >> somehash
# => {"something"=>2}
somehash = {'something' => 1}
SuperClass.new.add_something(somehash)
# >> somehash
# => {"something"=>6}
somehash = {'something' => 1}
ChildClass.add_something(somehash)
# >> somehash
# => {"something"=>4}
somehash = {'something' => 1}
ChildClass.new.add_something(somehash)
# >> somehash
# => {"something"=>26}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment