Skip to content

Instantly share code, notes, and snippets.

@metaskills
Created May 17, 2010 20:10
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 metaskills/404173 to your computer and use it in GitHub Desktop.
Save metaskills/404173 to your computer and use it in GitHub Desktop.
class Numeric
def negative
if self <= 0
self
else
self * -1
end
end
end
-123.negative # => -123
123.negative # => -123
0.negative # => 0
123.5.negative # => -123.5
BigDecimal('123.45').negative.to_s # => "-123.45"
@rmm5t
Copy link

rmm5t commented May 17, 2010

class Numeric
  def negative_abs
    self.abs * -1
  end
end

-123.negative_abs                         # => -123
123.negative_abs                          # => -123
0.negative_abs                            # => 0
123.5.negative_abs                        # => -123.5
BigDecimal('123.45').negative_abs.to_s    # => "-0.12345E3"

@metaskills
Copy link
Author

Yup... just went with that one! Great minds... odd I never solved this before :)
Took me forever to come up with .

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