Forum: Ruby Bound macro in ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
40700f4fc57ab1ed04e235c7b7ae4d50?d=identicon&s=25 Me Me (melmoth)
on 2008-10-24 11:37
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-24 13:01
(Received via mailing list)
2008/10/24 Me Me <emanuelef@tiscali.it>:
> 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
40700f4fc57ab1ed04e235c7b7ae4d50?d=identicon&s=25 Me Me (melmoth)
on 2008-10-24 13:18
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 Klemme wrote:
> 2008/10/24 Me Me <emanuelef@tiscali.it>:
>> 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
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jano Svitok (Guest)
on 2008-10-24 14:07
(Received via mailing list)
On Fri, Oct 24, 2008 at 13:19, Me Me <emanuelef@tiscali.it> 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-24 14:12
(Received via mailing list)
2008/10/24 Me Me <emanuelef@tiscali.it>:
>
>  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
40700f4fc57ab1ed04e235c7b7ae4d50?d=identicon&s=25 Me Me (melmoth)
on 2008-10-24 14:20
thanks all

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

bye
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-24 15:12
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
50b2daf0e7666574579b9edaf8f2b69a?d=identicon&s=25 Pit Capitain (Guest)
on 2008-10-24 17:21
(Received via mailing list)
2008/10/24 Me Me <emanuelef@tiscali.it>:
> 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
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 Michael Fellinger (Guest)
on 2008-10-24 18:05
(Received via mailing list)
On Sat, Oct 25, 2008 at 12:20 AM, Pit Capitain <pit.capitain@gmail.com>
wrote:
> 2008/10/24 Me Me <emanuelef@tiscali.it>:
>> 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]
This topic is locked and can not be replied to.