Skip to main content
object type trivia
Source Link
Darren Stone
  • 5.2k
  • 1
  • 19
  • 27

Ruby, 24

 r=eval s.gsub ?/,'**-1*'

Thank you to @ybeltukov for the Mathematica technique. I thought there might be a substitution that could be used to leverage eval and yours was much simpler than the crazy regexes I was trying!

This shortcut effectively changes the operator precedence in s to what the question requires. It has the side effect of sometimes resulting in a Rational object type (e.g. 2/1 == 2). The results are correct for the cases I've tried.

Ruby, 24

 r=eval s.gsub ?/,'**-1*'

Thank you to @ybeltukov for the Mathematica technique. I thought there might be a substitution that could be used to leverage eval and yours was much simpler than the crazy regexes I was trying!

Ruby, 24

 r=eval s.gsub ?/,'**-1*'

Thank you to @ybeltukov for the Mathematica technique. I thought there might be a substitution that could be used to leverage eval and yours was much simpler than the crazy regexes I was trying!

This shortcut effectively changes the operator precedence in s to what the question requires. It has the side effect of sometimes resulting in a Rational object type (e.g. 2/1 == 2). The results are correct for the cases I've tried.

being honest
Source Link
Darren Stone
  • 5.2k
  • 1
  • 19
  • 27

Ruby, 24

 r=eval s.gsub ?/,'**-1*'

Thank you to @ybeltukov for the Mathematica technique. I "knew"thought there wasmight be a substitution that could be used to leverage eval and yours was much simpler than the crazy regexes I was trying!

Ruby, 24

 r=eval s.gsub ?/,'**-1*'

Thank you to @ybeltukov for the Mathematica technique. I "knew" there was a substitution that could be used to leverage eval and yours was much simpler than the regexes I was trying!

Ruby, 24

 r=eval s.gsub ?/,'**-1*'

Thank you to @ybeltukov for the Mathematica technique. I thought there might be a substitution that could be used to leverage eval and yours was much simpler than the crazy regexes I was trying!

Source Link
Darren Stone
  • 5.2k
  • 1
  • 19
  • 27

Ruby, 24

 r=eval s.gsub ?/,'**-1*'

Thank you to @ybeltukov for the Mathematica technique. I "knew" there was a substitution that could be used to leverage eval and yours was much simpler than the regexes I was trying!