Skip to content

Instantly share code, notes, and snippets.

@gacha
Created February 25, 2019 10:20
Show Gist options
  • Save gacha/cdac549f7242de2081f785c9acc13704 to your computer and use it in GitHub Desktop.
Save gacha/cdac549f7242de2081f785c9acc13704 to your computer and use it in GitHub Desktop.
ruby prepend
class Car
def info
# some long method
puts "Only some info"
end
end
module FullInfo
def enable
@show_full_info = true
self
end
def info
if @show_full_info
puts "Full info"
else
super
end
end
end
Car.prepend FullInfo
Car.new.enable.info
Car.new.info
puts Car.ancestors.inspect
## Output:
#
# Full info
# Only some info
# [FullInfo, Car, Object, Kernel, BasicObject]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment