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
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 2:17 AM, Pokkai Dokkai <bad_good_lion@yahoo.com> wrote:
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
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.
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
···
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 ;).
--
use.inject do |as, often| as.you_can - without end
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 07:50, Joel VanderWerf wrote:
Martin DeMello wrote:
On Fri, Apr 11, 2008 at 2:17 AM, Pokkai Dokkai <bad_good_lion@yahoo.com> wrote:
how to create random object to a particular ruby object ?
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)
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!
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
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)
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.
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
>
> > [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
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
···
On Fri, Apr 11, 2008 at 2:39 PM, David A. Black <dblack@rubypal.com> wrote:
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 12.04.2008 15:33, Marc Heiler wrote:
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.
> 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 :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
···
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:
> On Fri, 11 Apr 2008, Robert Dober 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=:&
& 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!
>
>
> > 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.
> 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.
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.
···
On Fri, Apr 11, 2008 at 5:24 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
2008/4/11, Robert Dober <robert.dober@gmail.com>:
> On Fri, Apr 11, 2008 at 4:41 PM, Robert Klemme > > <shortcutter@googlemail.com> wrote:
> > 2008/4/11, Robert Dober <robert.dober@gmail.com>:
Kind regards
robert
--
use.inject do |as, often| as.you_can - without end
Missing the obvious, I was right after all, thank you Michael.
Robert
···
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