[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to create random object to a particular ruby object ?

Pokkai Dokkai

4/11/2008 9:18:00 AM

how to create random object to a particular ruby object ?

for example i want like this

rand(Fixnum) --> 345 (randomly)
rand(Float) --> 3877.5545(randomly)
rand(String) --> "sfskgksf" (randomly)
rand(boolean) --> 0(randomly)

any idea ?
--
Posted via http://www.ruby-....

21 Answers

Robert Klemme

4/11/2008 11:17:00 AM

0

2008/4/11, Pokkai Dokkai <bad_good_lion@yahoo.com>:
> how to create random object to a particular ruby object ?
>
> for example i want like this

You need more input parameters:

> rand(Fixnum) --> 345 (randomly)
Number range?

> rand(Float) --> 3877.5545(randomly)
Number range?

> rand(String) --> "sfskgksf" (randomly)
Length of String? Chars allowed in String?

> rand(boolean) --> 0(randomly)
That's easy
rand(2) == 0

Cheers

robert


--
use.inject do |as, often| as.you_can - without end

Robert Dober

4/11/2008 11:57:00 AM

0

On Fri, Apr 11, 2008 at 1:16 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> 2008/4/11, Pokkai Dokkai <bad_good_lion@yahoo.com>:
>
> > how to create random object to a particular ruby object ?
> >
> > for example i want like this
>
> You need more input parameters:
>
> > rand(Fixnum) --> 345 (randomly)
> Number range?
>
>
> > rand(Float) --> 3877.5545(randomly)
> Number range?
>
> > rand(String) --> "sfskgksf" (randomly)
> Length of String? Chars allowed in String?
>
> > rand(boolean) --> 0(randomly)
> That's easy
> rand(2) == 0
I am surprised that you are so permissive Robert ;).
I would say

[true,false][rand(2)]

or even, just to have some fun

[false,nil][rand(2)].send([:&&,:||][rand(2)],
method_returning_a_completeley_random_object)

to reflect Ruby's what's "true" and what's "false" semantics.

Cheers
Robert
> --
> use.inject do |as, often| as.you_can - without end

--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Robert Dober

4/11/2008 12:00:00 PM

0

> [false,nil][rand(2)].send([:&&,:||][rand(2)],
> method_returning_a_completeley_random_object)
oops, whats going wrong in my brain I was 100% sure that && was a
method, but :&& is not even a symbol, what is the reason for that?
I am obviously missing the obvious....
R.

David A. Black

4/11/2008 12:39:00 PM

0

Hi --

On Fri, 11 Apr 2008, Robert Dober wrote:

>> [false,nil][rand(2)].send([:&&,:||][rand(2)],
>> method_returning_a_completeley_random_object)
> oops, whats going wrong in my brain I was 100% sure that && was a
> method, but :&& is not even a symbol, what is the reason for that?
> I am obviously missing the obvious....

I'm not sure what the exact rule is, but for operators I think you
always have to quote them to get their symbol:

:"&&"


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!

Robert Dober

4/11/2008 1:16:00 PM

0

On Fri, Apr 11, 2008 at 2:39 PM, David A. Black <dblack@rubypal.com> wrote:
> Hi --
>
>
>
> On Fri, 11 Apr 2008, Robert Dober wrote:
>
>
> >
> > > [false,nil][rand(2)].send([:&&,:||][rand(2)],
> > > method_returning_a_completeley_random_object)
> > >
> > oops, whats going wrong in my brain I was 100% sure that && was a
> > method, but :&& is not even a symbol, what is the reason for that?
> > I am obviously missing the obvious....
> >
>
> I'm not sure what the exact rule is, but for operators I think you
> always have to quote them to get their symbol:
>
> :"&&"
No David
irb(main):017:0* x=:&
irb(main):018:0* 15.send x, 8
=> 8
irb(main):019:0>

The simple "problem" is that I believed for 3 years that &&, ||, :and
and :or where methods (of Object), which they are not :(
irb(main):020:0* 15.send("&&", 42)
NoMethodError: undefined method `&&' for 15:Fixnum
from (irb):20:in `send'
from (irb):20
from :0

and unless somebody can point out a good reason why that is like that
I am really tempted to make a RCR for 1.9.
Opinions?

Thx in advance
Robert

--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Michael Fellinger

4/11/2008 1:28:00 PM

0

On Fri, Apr 11, 2008 at 3:16 PM, Robert Dober <robert.dober@gmail.com> wrote:
> On Fri, Apr 11, 2008 at 2:39 PM, David A. Black <dblack@rubypal.com> wrote:
> > Hi --
> >
> >
> >
> > On Fri, 11 Apr 2008, Robert Dober wrote:
> >
> >
> > >
> > > > [false,nil][rand(2)].send([:&&,:||][rand(2)],
> > > > method_returning_a_completeley_random_object)
> > > >
> > > oops, whats going wrong in my brain I was 100% sure that && was a
> > > method, but :&& is not even a symbol, what is the reason for that?
> > > I am obviously missing the obvious....
> > >
> >
> > I'm not sure what the exact rule is, but for operators I think you
> > always have to quote them to get their symbol:
> >
> > :"&&"
> No David
> irb(main):017:0* x=:&
> irb(main):018:0* 15.send x, 8
> => 8
> irb(main):019:0>
>
> The simple "problem" is that I believed for 3 years that &&, ||, :and
> and :or where methods (of Object), which they are not :(
> irb(main):020:0* 15.send("&&", 42)
> NoMethodError: undefined method `&&' for 15:Fixnum
> from (irb):20:in `send'
> from (irb):20
> from :0
>
> and unless somebody can point out a good reason why that is like that
> I am really tempted to make a RCR for 1.9.
> Opinions?
>
> Thx in advance
> Robert

Because the use of these operators is that in some cases you don't
want the right hand to evaluate.
result = long_operation or other_long_operation

stops evaluating after the first one returns non-nil/false

^ manveru

>
>
>
> --
> http://ruby-smalltalk.blo...
>
> ---
> Whereof one cannot speak, thereof one must be silent.
> Ludwig Wittgenstein
>
>

Robert Dober

4/11/2008 2:04:00 PM

0

On Fri, Apr 11, 2008 at 3:28 PM, Michael Fellinger
<m.fellinger@gmail.com> wrote:
>
> > > > I am obviously missing the obvious....
> Because the use of these operators is that in some cases you don't
> want the right hand to evaluate.
> result = long_operation or other_long_operation
>
> stops evaluating after the first one returns non-nil/false
>
Missing the obvious, I was right after all, thank you Michael.

Robert


--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

David A. Black

4/11/2008 2:26:00 PM

0

Hi --

On Fri, 11 Apr 2008, Robert Dober wrote:

> On Fri, Apr 11, 2008 at 2:39 PM, David A. Black <dblack@rubypal.com> wrote:
>> Hi --
>>
>>
>>
>> On Fri, 11 Apr 2008, Robert Dober wrote:
>>
>>
>>>
>>>> [false,nil][rand(2)].send([:&&,:||][rand(2)],
>>>> method_returning_a_completeley_random_object)
>>>>
>>> oops, whats going wrong in my brain I was 100% sure that && was a
>>> method, but :&& is not even a symbol, what is the reason for that?
>>> I am obviously missing the obvious....
>>>
>>
>> I'm not sure what the exact rule is, but for operators I think you
>> always have to quote them to get their symbol:
>>
>> :"&&"
> No David
> irb(main):017:0* x=:&

& isn't an operator, though; it's a method. I don't know whether
that's actually why there's the difference with regard to symbol-izing
them, but I think it does at least mostly fall along those lines.


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!

Robert Klemme

4/11/2008 2:41:00 PM

0

2008/4/11, Robert Dober <robert.dober@gmail.com>:
> On Fri, Apr 11, 2008 at 1:16 PM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
> > 2008/4/11, Pokkai Dokkai <bad_good_lion@yahoo.com>:
> >
> > > how to create random object to a particular ruby object ?
> > >
> > > for example i want like this
> >
> > You need more input parameters:
> >
> > > rand(Fixnum) --> 345 (randomly)
> > Number range?
> >
> >
> > > rand(Float) --> 3877.5545(randomly)
> > Number range?
> >
> > > rand(String) --> "sfskgksf" (randomly)
> > Length of String? Chars allowed in String?
> >
> > > rand(boolean) --> 0(randomly)
> > That's easy
> > rand(2) == 0
>
> I am surprised that you are so permissive Robert ;).

I'm not getting the joke here, sorry. Is it somehow related to the
original posting mentioning "0" as the sole return value from
rand(boolean)?

Cheers

robert

--
use.inject do |as, often| as.you_can - without end

Robert Dober

4/11/2008 3:09:00 PM

0

On Fri, Apr 11, 2008 at 4:41 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> 2008/4/11, Robert Dober <robert.dober@gmail.com>:

> I'm not getting the joke here, sorry. Is it somehow related to the
> original posting mentioning "0" as the sole return value from
> rand(boolean)?
well I would not consider 0 a boolean, but maybe that was what OP wanted.

R.