Skip to content

Instantly share code, notes, and snippets.

@boxofrad
Last active August 29, 2015 14:02
Show Gist options
  • Save boxofrad/1a0b59a221b9433ba950 to your computer and use it in GitHub Desktop.
Save boxofrad/1a0b59a221b9433ba950 to your computer and use it in GitHub Desktop.
class Calc
NUMBERS = %w[one two three four five six seven eight nine ten]
def initialize(number = 0, meth = nil)
@number = number
@meth = meth
end
NUMBERS.each_with_index do |number, index|
define_method(number) do
value = index + 1
if @meth
@number.send(@meth, value)
else
value
end
end
end
end
class Fixnum
OPS = { plus: :+, minus: :-, times: :*, divided_by: :/ }
OPS.each do |op, meth|
define_method(op) { Calc.new(self, meth) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment