Bound macro in ruby


#1

Hi all,
I think it’s a simple question but I couldn’t find in docs.
Is there a macro already defined in ruby for returning the “bound” of a
value:

ex:

BOUND(1, 5, 10) => 5
BOUND(10, 5, 100) => 5
BOUND(1, 50, 10) => 10


#2

2008/10/24 Me Me removed_email_address@domain.invalid:

I think it’s a simple question but I couldn’t find in docs.
Is there a macro already defined in ruby for returning the “bound” of a
value:

ex:

BOUND(1, 5, 10) => 5
BOUND(10, 5, 100) => 5
BOUND(1, 50, 10) => 10

First of all, there are no macros in Ruby. If anything you would be
looking for a method (or function). How is your “bound” defined?

Cheers

robert


#3

On Fri, Oct 24, 2008 at 13:19, Me Me removed_email_address@domain.invalid wrote:

thanks for answering:

in my case it’s something I can easy implement with if-clause, but I
wanted to know if this could be done in one single line

def bound(min, value, max)
(value > max) ? max : ((value < min) ? min : value
end

I propose replacing > with >=, because in case when value==max it will
be evaluated faster

def bound(min, value, max)
(value >= max) ? max : ((value < min) ? min : value
end


#4

2008/10/24 Me Me removed_email_address@domain.invalid:

if(value < min)
return min
end

return value

end

irb(main):001:0> def BOUND(min,val,max) val >=max ? max : val < min ?
min : val end
=> nil
irb(main):002:0> BOUND(10,0,100)
=> 10
irb(main):003:0>
irb(main):004:0* BOUND(10,200,100)
=> 100
irb(main):005:0> BOUND(10,150,100)
=> 100
irb(main):006:0> BOUND(10,50,100)
=> 50

Cheers

robert


#5

thanks all

I though it was something already implemented inside ruby, I guess the
same for MIN, MAX

bye


#6

thanks for answering:

in my case it’s something I can easy implement with if-clause, but I
wanted to know if this could be done in one single line

def bound(min, value, max)

if(value > max)
return max
end

if(value < min)
return min
end

return value

end

Robert K. wrote:

2008/10/24 Me Me removed_email_address@domain.invalid:

I think it’s a simple question but I couldn’t find in docs.
Is there a macro already defined in ruby for returning the “bound” of a
value:

ex:

BOUND(1, 5, 10) => 5
BOUND(10, 5, 100) => 5
BOUND(1, 50, 10) => 10

First of all, there are no macros in Ruby. If anything you would be
looking for a method (or function). How is your “bound” defined?

Cheers

robert


#7

2008/10/24 Me Me removed_email_address@domain.invalid:

I though it was something already implemented inside ruby, I guess the
same for MIN, MAX

If you want to use #min and #max then you can

def bound(min, val, max) [[val, max].min, min].max end

Regards,
Pit


#8

Me Me wrote:

I though it was something already implemented inside ruby

If it existed, I would expect to find it as Range#clip or something like
that. But I don’t think it does.

class Range
def clip(v)
v < first ? first : v > last ? last : v
end
end

p (1…10).clip(5) # 5
p (10…100).clip(5) # 10 (different to your original post??)
p (1…10).clip(50) # 10

It wouldn’t surprise me if something like this existed in the facets
library though.

I guess the same for MIN, MAX

irb(main):001:0> [20,40].min
=> 20
irb(main):002:0> [20,40,30].min
=> 20
irb(main):003:0> [20,40,30].max
=> 40


#9

On Sat, Oct 25, 2008 at 12:20 AM, Pit C. removed_email_address@domain.invalid
wrote:

2008/10/24 Me Me removed_email_address@domain.invalid:

I though it was something already implemented inside ruby, I guess the
same for MIN, MAX

Another way is to use sorting:

[min, val, max].sort[1]