[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Bound macro in ruby

Ema Fuma

10/24/2008 9:36:00 AM

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
--
Posted via http://www.ruby-....

20 Answers

Robert Klemme

10/24/2008 10:59:00 AM

0

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

--
remember.guy do |as, often| as.you_can - without end

Ema Fuma

10/24/2008 11:19:00 AM

0

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

--
Posted via http://www.ruby-....

Jano Svitok

10/24/2008 12:07:00 PM

0

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

Robert Klemme

10/24/2008 12:12:00 PM

0

2008/10/24 Me Me <emanuelef@tiscali.it>:
> 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

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

--
remember.guy do |as, often| as.you_can - without end

Ema Fuma

10/24/2008 12:20:00 PM

0

thanks all

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

bye
--
Posted via http://www.ruby-....

Brian Candler

10/24/2008 1:13:00 PM

0

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
--
Posted via http://www.ruby-....

Pit Capitain

10/24/2008 3:21:00 PM

0

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

Michael Fellinger

10/24/2008 4:05:00 PM

0

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]

Douglas Newlands

2/19/2014 5:50:00 AM

0

On 19/02/2014 4:25 pm, Michelle Steiner wrote:
> In article <5304328a$0$2830$e4fe514c@news2.news.xs4all.nl>, Co Wiersma
> <Co.Wiersma@xs4all.nl> wrote:
>
>> Michelle Steiner schreef op 19-2-2014 5:17:
>>> As I've previously mentioned, I returned to the game in early September
>>> of last year after 30-40 years away from the game. By the end of that
>>> month, I had a whopping 0.30 master points. A month later, I had 3.58
>>> master points, of which 0.13 were gold.
>>>
>>> As of today, I have 47.19: 20.41 black, 15.12 silver, 8.1 red, and
>>> 3.56 gold.
>>>
>>> Thus far this month, I have 12.07, 6.10 of which are black, and 5.97
>>> are silver. 2.76 of the black points was from coming in second overall
>>> in a field of 48 pairs (two 12-table sections) this afternoon.
>>>
>>> Just bragging on myself a bit is all.
>>>
>>
>> lol
>>
>> And I have no clue what that all is!
>
> How are duplicate players rated in The Netherlands? Here in the USA,
> it's by master points.

Every country, I think, has masterpoints because they are such a good
fund-raising idea. The colors and volumes per play vary quite markedly
from country to country.
The cynics refer to them as attendance points since even those who can
barely tell an ace from a king will get an impressive number if they
play often enough altho the national level ones may elude them in their
quest for higher rankings.

doug

Michelle Steiner

2/19/2014 5:56:00 AM

0

In article <le1gm4$eq$1@douglasnewlands.eternal-september.org>, Douglas
Newlands <douglas.newlands@gmail.com> wrote:

> > How are duplicate players rated in The Netherlands? Here in the USA,
> > it's by master points.
>
> Every country, I think, has masterpoints because they are such a good
> fund-raising idea. The colors and volumes per play vary quite markedly
> from country to country.

I didn't consider that he might have been referring to the colors.

> The cynics refer to them as attendance points since even those who can
> barely tell an ace from a king will get an impressive number if they
> play often enough altho the national level ones may elude them in their
> quest for higher rankings.

Well, to a degree they are attendance points, because the more often
you play, the more opportunities you have for winning points. But you
still have to play well enough to earn those points.

And of course, the more often you play, the more you will improve.
Assuming, of course, that you play with partners and competition that
challenge you to play better by their play.