
Security News
Follow-up and Clarification on Recent Malicious Ruby Gems Campaign
A clarification on our recent research investigating 60 malicious Ruby gems.
The GreaterLess gem can be used to generate objects that represent halfopen intervals, but transparently behave as Ruby Floats.
To install, type
sudo gem install greater_less
If you are using bundler, add greater_less
to your gemfile
gem 'greater_less'
One easy way to integrate this gem into your project is by requiring the GreaterLess string extension as follows:
require 'greater_less/string_extension'
This extension redifines the #to_f
method of the String class:
class String
alias :to_f_without_greater_less :to_f
def to_f
if self =~ GreaterLess::GREATER_LESS
return GreaterLess.new(self)
end
self.to_f_without_greater_less
end
end
Now when a string starts with a greater or less sign (like for instance
"> 3.45"
), the #to_f
method converts it to a GreaterLess object
instead of the value 0.0
.
Alternatively you can opt to create GreaterLess objects using initialize
directly, like so:
value = GreaterLess.new("> 3.45")
A GreaterLess object can be compared to a Float (or other numeric) as if it were a Float itself. For instance one can do the following:
>> value = ">3.45".to_f
=> > 3.45
>> value > 2.45
=> true
>> value >= 2.45
=> true
>> 2.45 > value
=> false
>> 2.45 >= value
=> false
>> value == ">3.45".to_f
=> true
>> value != 2.45
=> true
It is also possible to compare GreaterLess objects with each other, so you do not have to worry about what kind of object you are dealing with in your code:
>> value1 = ">3.45".to_f
=> > 3.45
>> value2 = "< 2.45".to_f
=> < 2.45
>> value1 > value2
=> true
>> value2 > value1
=> false
Finally it is possible to apply simple arithmetics to GreaterLess objects like addition, subtraction, multiplication and division:
>> value = ">3.45".to_f
=> > 3.45
>> value + 2
=> > 5.45
>> value - 2
=> > 1.4500000000000002
>> value * 2
=> > 1.725
Inverting the object's sign when multiplying with a negative numerical or using a GreaterLess object in the denominator is nicely dealt with:
>> value = ">3.45".to_f
=> > 3.45
>> -1 * value
=> < -3.45
>> 1 / value
=> < 0.2898550724637681
>> -1 / value
=> > -0.2898550724637681
In many cases it makes no sense to apply the operators +, -, * or / on a pair of GreaterLess objects, so when this happens an exception is raised for now.
All other methods are delegated to the Float value the GreaterLess
object contains, so that it transparently acts like a Float. This means you should be careful when using methods like
abs
or round
since they return the result from the underlying Float and bypass the GreaterLess behavior.
Copyright (c) 2016 Samuel Esposito, Jorn van de Beek. See LICENSE.txt for further details.
FAQs
Unknown package
We found that greater_less demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.
Research
/Security News
A malicious Go module posing as an SSH brute forcer exfiltrates stolen credentials to a Telegram bot controlled by a Russian-speaking threat actor.