Ruby Elegant Way to Return Min/Max If Value Outside Range

Ruby Elegant Way to Return Min/Max if Value Outside Range

If it is just for one occasion, I would recommend Shawn Balestracci's answer, which is the most beautiful.

Alternatively, here are some methods from my personal library:

module Comparable
def at_least other; self < other ? other : self end
def at_most other; self > other ? other : self end
end

I use it like this:

fee_amount = <whatever logic I need>.at_least(min).at_most(max)

How to: Ruby Range that doesn't include the first value

No, there is no built-in support for such a range. You might want to roll your own Range-like class if this behavior is necessary.

How to handle out-of-range values? Switch or if statements?

I would actually use an object containing your ranges, and then make a simple function to check against them. That way it is much easier to maintain, simpler to understand.

var ranges = [
{ min: 10, max: 250},
{ min: 255, max: 400},
{ min: 405, max: 600},
{ min: 605, max: 780},
{ min: 785, max: 900},
{ min: 905, max: 990}
];

function getScoreRange(score){
var output = 'out of range'; //set to either empty or null or default value for out of range scores.
ranges.forEach(function(range){
if(score >= range.min && score <= range.max){
output = range.min + ' - ' + range.max;
}
});
return output;
};
alert(getScoreRange(960));

Now you can change your logic with ease, and add new ranges or change them without changing too much, you can also reuse your scoring method with other ranges and for other purposes as well.

Working JSFiddle: https://jsfiddle.net/workingClassHacker/c06pq2w0/3/

Is there an elegant way to exclude the first value of a range?

No.

((0+1)..10)

rspec test model for minimum and maximum values

To test if all valid values are covered you can write something like:

it "should allow valid values" do
(1..24).to_a.each do |v|
should allow_value(v).for(:hours)
end

You can also implement boundary testing. For each boundary one can test at, above and below any boundary to ensure that the conditional logic works as expected as posted by David Chelimsky-2.

So you would have 6 tests in total for the 2 boundaries.

Is there a more elegant way to write my object hierarchical analyzer in Ruby?

def hiearchicalanalyzer(object)
puts klass = (Class === object) ? object : object.class
hiearchicalanalyzer(klass.superclass) if klass.superclass
end
  • It's not necessary to use object.respond_to?('superclass') just to make sure that object is class. Because class of class is always Class, you can use method Object#is_a? and write object.is_a?(Class) for the same purpose. Or equivalent Class === object using Module#===.

  • BasicObject.superclass returns nil which acts like false in conditional statements. So when you build a recursion function, you can use it for termination.

How to validate Array length using Ruby's Grape?

There's an example explaining how to do this in the Grape README:

class Length < Grape::Validations::Base
def validate_param!(attr_name, params)
unless params[attr_name].length <= @option
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "must be at the most #{@option} characters long"
end
end
end

You can make it work the way you expect with something like this for the max length of the array:

class MaxLength < Grape::Validations::Base
def validate_param!(attr_name, params)
unless params[attr_name].length <= @option
fail Grape::Exceptions::Validation,
params: [attr_name.to_s],
message: "must be at the most #{@option} elements long"
end
end
end

And this for the min length of the array:

class MinLength < Grape::Validations::Base
def validate_param!(attr_name, params)
unless params[attr_name].length >= @option
fail Grape::Exceptions::Validation,
params: [attr_name.to_s],
message: "must be at the least #{@option} elements long"
end
end
end

Then call it:

params do
requires :array, type: Array, min_length: 1, max_length: 2, desc: 'Array with defined length'
end


Related Topics



Leave a reply



Submit