Skip to content

Instantly share code, notes, and snippets.

@astux7
Last active February 3, 2023 15:54
Show Gist options
  • Save astux7/4387e6fb3c90250e36c0 to your computer and use it in GitHub Desktop.
Save astux7/4387e6fb3c90250e36c0 to your computer and use it in GitHub Desktop.
include, extend, super, prepend in ruby

Ruby Methods:

[include] - makes the modules methods available to the instance of a class. It takes all the methods from another module and includes them into the current module. This is a language-level thing. The include method is the primary way to "extend" classes with other modules (usually referred to as mix-ins).

module After
end

class Example
  include After
end

Example.ancestors
# => [Example, After, Object, Kernel, BasicObject]

[require] - runs another file. Also tracks what you have required in the past and won't require the same file twice. For import libraries mostly.


[require_relative] - allowing to load a file that is relative to the file containing the require_relative statement. For example if you have a rspec classes in /spec directory and data for them under the spec/data directory, then you might use the line this in test case:

require_relative "data/test"

[load] - for execute code.


[extend] - will add the extended class to the ancestors of the extending classes' singleton class. Other words extend mix the module's functionality into class and makes these methods available to the class itself.

module After
 def module_method
    puts "Module Method invoked"
  end
end

class Example
end

ex = Example.new
ex.extend After
ex.module_method
# => Module Method invoked

[super] - is used for overridden method call by the overriding method. using super will call the same method, but as defined in the superclass and give you the result.

class Animal
  def move 
    "I can move"
  end
end

class Bird
  def move
    super + "by flying"
  end
end

puts Animal.new.move
# => I can move
puts Bird.new.move
# => I can move by flying

[prepend] - invokes Module.prepend_features on each parameter in reverse order.

module Before
end

class Example
  prepend Before
end

Example.ancestors
# => [Before, Example, Object, Kernel, BasicObject]
@abdullahtariq1171
Copy link

abdullahtariq1171 commented May 1, 2017

In [super] example, apparently Bird class should inherit from Animal?
@astux7

@Lupeipei
Copy link

Bird should inherit from Animal ,if not, super will be raise an error. @abdullahtariq1171

@toomanyjoes
Copy link

But does this work in ruby/rails?

class Bird
  include Animal
  def move
    super + "by flying"
  end
end

@funder7
Copy link

funder7 commented Aug 17, 2021

@toomanyjoes I don't think so: you're including, not extending, I'm not sure of the result when including a class having the same method name - probably the included class will override existing methods.

class Animal
  def move 
    "I can move"
  end
end

class Bird < Animal
  def move
    super + "by flying"
  end
end

not a ruby expert, but I think that this version would let you use super

@astux7
Copy link
Author

astux7 commented Aug 18, 2021

In [super] example, apparently Bird class should inherit from Animal?
@astux7

Yep it should inherit, it was a while ago i wrote that dont think it changed a lot but also it is not up to date

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment