Skip to content

Instantly share code, notes, and snippets.

@mocoso
Forked from jamesshipton/my_class_keywords.rb
Last active December 11, 2015 07:49
Show Gist options
  • Save mocoso/4568915 to your computer and use it in GitHub Desktop.
Save mocoso/4568915 to your computer and use it in GitHub Desktop.
# Please rewrite the MyString definition, without using ruby keywords
# http://www.ruby-doc.org/docs/keywords/1.9/
MyString = Class.new(String) {
define_singleton_method(:alphabet) {
'abc'
}
define_method(:exclaim) { |*args|
count = args.first || 1
"#{self}#{exclamation_mark count}"
}
define_method(:!) { |*args|
exclaim *args
}
define_method(:exclamation_mark) { |count|
'!' * count
}
self.send :private, :exclamation_mark
}
j = MyString.new('james')
j.exclaim # "james!"
j.exclaim(4) # "james!!!!"
j.! # "james!"
MyString.alphabet # "abc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment