how to create random object to a particular ruby object ?

P

Pokkai Dokkai

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 ?
 
R

Robert Klemme

2008/4/11 said:
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
 
R

Robert Dober

You need more input parameters:

Number range?


Number range?

Length of String? Chars allowed in String?

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
 
R

Robert Dober

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

David A. Black

Hi --

[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.rubypal.com for details and updates!
 
R

Robert Dober

Hi --



[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 :eek:r 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
 
M

Michael Fellinger

Hi --



[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 :eek:r 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
 
R

Robert Dober

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
 
D

David A. Black

Hi --

Hi --



[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.rubypal.com for details and updates!
 
R

Robert Klemme

2008/4/11 said:
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
 
R

Robert Dober

& 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
 
R

Robert Klemme

2008/4/11 said:
well I would not consider 0 a boolean,

And a random sequence of 0's would look funny. :)
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. :)

Kind regards

robert
 
R

Robert Dober

And a random sequence of 0's would look funny. :)



In which case I would have missed the mark because my bit does not
return 0 but true / false. :)
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.
 
M

Martin DeMello

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)
=> "I9K@Nr"
irb(main):008:0> String.make_random(10)

martin
 
J

Joel VanderWerf

Martin said:
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)
 
R

Robert Klemme

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)

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
 
M

Marc Heiler

#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
 
R

Robert Klemme

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. :)
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
 
J

Joel VanderWerf

Robert said:
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.

I did say "wrongest" ;)

For floats, you usually get e, pi, or the max/min floats, if no other
floats have been instantiated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top