# Bound macro in ruby

**URL:** <https://www.ruby-forum.com/t/bound-macro-in-ruby/151683>\
**Category:** Ruby\
**Created:** [October 24, 2008, 11:37am UTC](https://www.ruby-forum.com/t/bound-macro-in-ruby/151683 "2008-10-24T11:37:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 24, 2008, 11:37am UTC](https://www.ruby-forum.com/t/bound-macro-in-ruby/151683/1 "2008-10-24T11:37:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 24, 2008, 1:01pm UTC](https://www.ruby-forum.com/t/bound-macro-in-ruby/151683/2 "2008-10-24T13:01:24Z")

</div>

2008/10/24 Me Me [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 24, 2008, 2:07pm UTC](https://www.ruby-forum.com/t/bound-macro-in-ruby/151683/3 "2008-10-24T14:07:08Z")

</div>

On Fri, Oct 24, 2008 at 13:19, Me Me [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 24, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/bound-macro-in-ruby/151683/4 "2008-10-24T14:12:59Z")

</div>

2008/10/24 Me Me [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 24, 2008, 2:20pm UTC](https://www.ruby-forum.com/t/bound-macro-in-ruby/151683/5 "2008-10-24T14:20:23Z")

</div>

thanks all

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

bye

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 24, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/bound-macro-in-ruby/151683/6 "2008-10-24T13:18:58Z")

</div>

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](mailto: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

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 24, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/bound-macro-in-ruby/151683/7 "2008-10-24T17:21:29Z")

</div>

2008/10/24 Me Me [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 24, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/bound-macro-in-ruby/151683/8 "2008-10-24T15:12:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [October 24, 2008, 6:05pm UTC](https://www.ruby-forum.com/t/bound-macro-in-ruby/151683/9 "2008-10-24T18:05:34Z")

</div>

On Sat, Oct 25, 2008 at 12:20 AM, Pit C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 2008/10/24 Me Me [removed\_email\_address@domain.invalid](mailto: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]
