Last active
August 29, 2015 14:15
-
-
Save anithri/29a75f68669a130e3e26 to your computer and use it in GitHub Desktop.
are there any gotcha's for this refinement scheme?
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
# Small module intended for enhancing a string object. | |
module Joy | |
def joy | |
self + ' Joy!' | |
end | |
end |
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
# setup the Refinement using include | |
require_relative 'joy' | |
module RefinedJoy | |
refine String do | |
include Joy | |
end | |
end |
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
require_relative 'refined_joy' | |
module TestPositive | |
using RefinedJoy | |
puts "refined? ".joy #> refined? Joy! | |
end | |
module TestNegative | |
puts "refined? ".joy #> NoMethodError "undefined method `joy' for "Refined?":String" | |
end |
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
require_relative 'joy' | |
String.include(Joy) | |
puts "monkey patched?".joy #> monkey patched? Joy! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment