How to create random object to a particular ruby object?

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 ?

2008/4/11, Pokkai D. [email protected]:

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

On Fri, Apr 11, 2008 at 1:16 PM, Robert K.
[email protected] wrote:

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.blogspot.com/


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

[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.

On Fri, Apr 11, 2008 at 2:39 PM, David A. Black [email protected]
wrote:

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 :frowning:
irb(main):020:0* 15.send("&&", 42)
NoMethodError: undefined method &&' for 15:Fixnum from (irb):20:insend’
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.blogspot.com/


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

Hi –

On Fri, 11 Apr 2008, Robert D. 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

On Fri, Apr 11, 2008 at 3:16 PM, Robert D. [email protected]
wrote:

method_returning_a_completeley_random_object)
No David
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

On Fri, Apr 11, 2008 at 3:28 PM, Michael F.
[email protected] 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.blogspot.com/


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

Hi,
----- Original Message -----
From: “Michael F.” [email protected]
To: “ruby-talk ML” [email protected]
Sent: Friday, April 11, 2008 10:28 PM
Subject: Re: how to create random object to a particular ruby object ?

On Fri, Apr 11, 2008 at 3:16 PM, Robert D. [email protected] wrote:

On Fri, Apr 11, 2008 at 2:39 PM, David A. Black [email protected] wrote:

Hi –

NoMethodError: undefined method `&&’ for 15:Fixnum

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

I don’t think so.
If you’re correct, what’s your explanation of the following:

irb(main):001:0> 15.send("||",42)
NoMethodError: undefined method ||' for 15:Fixnum from (irb):1:insend’
from (irb):1

&& or || is not a method but an operator.
You cannot use send for the operator.

Regards,

Park H.

2008/4/11, Robert D. [email protected]:

rand(Fixnum) --> 345 (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

Hi –

On Fri, 11 Apr 2008, Robert D. wrote:

method_returning_a_completeley_random_object)
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

On Fri, Apr 11, 2008 at 4:41 PM, Robert K.
[email protected] wrote:

2008/4/11, Robert D. [email protected]:

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.

On Fri, Apr 11, 2008 at 4:25 PM, David A. Black [email protected]
wrote:

& 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.
I guess that this is the reason of which I fail to see the reason, but
that is not an important issue to me.
For me it was important to got explained why && was not a method,
which has been nicely accomplished in the meantime.

Cheers
Robert

2008/4/11, Robert D. [email protected]:

On Fri, Apr 11, 2008 at 4:41 PM, Robert K.
[email protected] wrote:

2008/4/11, Robert D. [email protected]:

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,

And a random sequence of 0’s would look funny. :slight_smile:

but maybe that was what OP wanted.

In which case I would have missed the mark because my bit does not
return 0 but true / false. :slight_smile:

Kind regards

robert

On Fri, Apr 11, 2008 at 2:17 AM, Pokkai D. [email protected]
wrote:

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)

The cleanest way is to add the class method make_random to all your
classes. For example

class String
def self.make_random(length = nil)
length ||= rand(15) # well, we have to pick something
(0…length).map { (rand(96) + 32).chr } .join("")
end
end

irb(main):006:0> String.make_random
=> “Y3+W%2(”
irb(main):007:0> String.make_random(6)
=> “[email protected]
irb(main):008:0> String.make_random(10)

martin

Martin DeMello wrote:

The cleanest way is to add the class method make_random to all your
classes. For example

Here is the dirtiest, wrongest, ugliest way to do it:

module ObjectSpace
def self.random_object(klass)
n = each_object(klass){}
i = rand(n)
each_object klass do |obj|
if i == 0
return obj
else
i -= 1
end
end
return nil
end
end

p ObjectSpace.random_object(String)
p ObjectSpace.random_object(Float)

On Fri, Apr 11, 2008 at 5:24 PM, Robert K.
[email protected] wrote:

well I would not consider 0 a boolean,

And a random sequence of 0’s would look funny. :slight_smile:

but maybe that was what OP wanted.

In which case I would have missed the mark because my bit does not
return 0 but true / false. :slight_smile:
It does indeed, I am in great form today, but at least you understand
my surprise now, that you would deliver rand(2) instead of rand(2) ==
0, which indeed is a nice way to do the job!
Sorry
R.


http://ruby-smalltalk.blogspot.com/


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

#freeze has side effects
Actually I think I would not say “side effects”, I would rather claim
that if one does .freeze an object, this object becomes rather unusable
(as you can not normally undo a .freeze again), I actually cannot recall
that I really needed .freeze in any code i wrote

On 12.04.2008 07:50, Joel VanderWerf wrote:

The cleanest way is to add the class method make_random to all your
return obj
else
i -= 1
end
end
return nil
end
end

p ObjectSpace.random_object(String)
p ObjectSpace.random_object(Float)

This does not work well for Strings because you get only instances that
do exist already when the method is called. That way you a) have no
clear definition of what you get (length, contained chars…) and b) you
either need to dup or freeze the return value. Both have their
problems: #freeze has side effects and #dup does not work for all
classes.

Kind regards

robert

On 12.04.2008 15:33, Marc H. wrote:

#freeze has side effects
Actually I think I would not say “side effects”,

Even if you would not use that term it is the proper CS term for what’s
happening here. :slight_smile:

I would rather claim
that if one does .freeze an object, this object becomes rather unusable

Usability is changed. Whether the object is usable or unusable depends
on what the code wants to do with it.

(as you can not normally undo a .freeze again), I actually cannot recall
that I really needed .freeze in any code i wrote

It comes in handy when you want to prevent a particular class of errors,
i.e. all those that are caused by unintentionally changing an object.
Once you find that a constant is not as constant as you thought you
might start using #freeze.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs