Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =begin
- kata:
- http://www.codewars.com/kata/calculating-with-functions/
- Description:
- This time we want to write calculations using functions and get the results. Let's have a look at some examples:
- seven(times(five)) # must return 35
- four(plus(nine)) # must return 13
- eight(minus(three)) # must return 5
- six(divided_by(two)) # must return 3
- =end
- #Solution
- def match number, string=nil
- unless string.nil?
- return eval( number.to_s + string.to_s )
- end
- number
- end
- def zero function=nil
- match(0, function)
- end
- def one function=nil
- match(1, function)
- end
- def two function=nil
- match(2, function)
- end
- def three function=nil
- match(3, function)
- end
- def four function=nil
- match(4, function)
- end
- def five function=nil
- match(5, function)
- end
- def six function=nil
- match(6, function)
- end
- def seven function=nil
- match(7, function)
- end
- def eight function=nil
- match(8, function)
- end
- def nine function=nil
- match(9, function)
- end
- def plus number=nil
- "+ "+number.to_s
- end
- def minus number=nil
- "- "+number.to_s
- end
- def times number=nil
- "* "+number.to_s
- end
- def divided_by number=nil
- return " / "+number.to_s+".0"
- end
Advertisement
Add Comment
Please, Sign In to add comment