Created
October 31, 2014 20:25
-
-
Save epidemian/cf97222ee3336f9d6655 to your computer and use it in GitHub Desktop.
Code snippets for a Suma lightning talk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Greeting | |
def greet(who) | |
"Hello #{who}" | |
end | |
end | |
class Hacker | |
include Greeting | |
def greet(who) | |
if who.downcase == who | |
"hi @#{who}" | |
else | |
super | |
end | |
end | |
end | |
hacker = Hacker.new | |
hacker.greet('Damián S') # => "Hello Damián S" | |
hacker.greet('nox') # => "hi @nox" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example totally stolen from RubyMonk. | |
class Monk | |
%w(life the_universe everything).each do |concept| | |
define_method("meditate_on_#{concept}") do | |
"I know the meaning of #{concept.tr '_', ' '}" | |
end | |
end | |
end | |
monk = Monk.new | |
monk.meditate_on_life |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Meditation | |
def can_meditate_on(*concepts) | |
concepts.each do |concept| | |
define_method("meditate_on_#{concept}") do | |
"I know the meaning of #{concept.to_s.tr '_', ' '}" | |
end | |
end | |
end | |
end | |
class Newbie | |
extend Meditation | |
can_meditate_on :variables, :blocks | |
def meditate_on_blocks | |
"#{super}, i think..." | |
end | |
end | |
newbie = Newbie.new | |
newbie.meditate_on_blocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Meditation | |
def can_meditate_on(*concepts) | |
if const_defined?(:MeditationMethods, _search_ancestors = false) | |
mod = const_get(:MeditationMethods) | |
else | |
mod = const_set(:MeditationMethods, Module.new) | |
include mod | |
end | |
mod.module_eval do | |
concepts.each do |concept| | |
define_method("meditate_on_#{concept}") do | |
"I know the meaning of #{concept.to_s.tr '_', ' '}" | |
end | |
end | |
end | |
end | |
end | |
class Newbie | |
extend Meditation | |
can_meditate_on :variables, :blocks | |
def meditate_on_blocks | |
"#{super}, i think..." | |
end | |
end | |
newbie = Newbie.new | |
newbie.meditate_on_blocks # => "I know the meaning of blocks, i think..." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Meditation | |
def self.on(*concepts) | |
Module.new do | |
concepts.each do |concept| | |
define_method("meditate_on_#{concept}") do | |
"I know the meaning of #{concept.to_s.tr '_', ' '}" | |
end | |
end | |
end | |
end | |
end | |
class Guru | |
include Meditation.on(:modules) | |
def meditate_on_modules | |
"#{super} and more!" | |
end | |
end | |
guru = Guru.new | |
guru.meditate_on_modules # => "I know the meaning of modules and more!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment