vladislavkopilov

ruby - Calculating with Functions

Mar 15th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.17 KB | None | 0 0
  1. =begin
  2. kata:
  3. http://www.codewars.com/kata/calculating-with-functions/
  4.  
  5. Description:
  6.  
  7. This time we want to write calculations using functions and get the results. Let's have a look at some examples:
  8.  
  9. seven(times(five)) # must return 35
  10. four(plus(nine)) # must return 13
  11. eight(minus(three)) # must return 5
  12. six(divided_by(two)) # must return 3
  13. =end
  14.  
  15. #Solution
  16.  
  17. def match number, string=nil
  18.   unless string.nil?
  19.     return eval( number.to_s + string.to_s )
  20.   end
  21.   number
  22. end
  23.  
  24. def zero function=nil
  25.   match(0, function)
  26. end
  27. def one function=nil
  28.   match(1, function)
  29. end
  30. def two function=nil
  31.   match(2, function)
  32. end
  33. def three function=nil
  34.   match(3, function)
  35. end
  36. def four function=nil
  37.   match(4, function)
  38. end
  39. def five function=nil
  40.   match(5, function)
  41. end
  42. def six function=nil
  43.   match(6, function)
  44. end
  45. def seven function=nil
  46.   match(7, function)
  47. end
  48. def eight function=nil
  49.   match(8, function)
  50. end
  51. def nine function=nil
  52.   match(9, function)
  53. end
  54.  
  55. def plus number=nil
  56.   "+ "+number.to_s
  57. end
  58. def minus number=nil
  59.   "- "+number.to_s
  60. end
  61. def times number=nil
  62.   "* "+number.to_s
  63. end
  64. def divided_by number=nil
  65.   return " / "+number.to_s+".0"
  66. end
Advertisement
Add Comment
Please, Sign In to add comment