Skip to content

Instantly share code, notes, and snippets.

@urokuta
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urokuta/5a6470a1ae9c4274b855 to your computer and use it in GitHub Desktop.
Save urokuta/5a6470a1ae9c4274b855 to your computer and use it in GitHub Desktop.
simple_calc
def expr(e) e.to_s.include?("=") ? e.split[0..-2].inject([]){|arr,e| (!arr.empty? && arr.last =~ /[^\d]/) ? [eval([*arr,e].join)] : arr << e } : ->(_e){expr("#{e} #{_e}")} end
puts expr(4)["+"][3]["*"][2]["-"][1]["="]
@urokuta
Copy link
Author

urokuta commented May 10, 2014

ruby -e 'def f(e)e.to_s.include?("=")?e.split[0..-2].inject([]){|a,e|(!a.empty?&&a[-1]=~/[^\d]/)?[eval([*a,e].join)]:a<<e}:->(b){f("#{e} #{b}")}end;puts f(4)["+"][3]["*"][2]["-"][1]["="]'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment