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 ?

···

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

···

2008/4/11, Pokkai Dokkai <bad_good_lion@yahoo.com>:

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

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

···

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 ?

for example i want like this

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

> 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

···

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>:
--
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

Martin DeMello 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

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

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

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 ?

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

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)

Joel VanderWerf wrote:

Martin DeMello wrote:

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

but that is worked for me .

module ObjectSpace

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

thank you to all

···

--
Posted via http://www.ruby-forum.com/\.

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

well I would not consider 0 a boolean, but maybe that was what OP wanted.

R.

···

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

#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

···

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote:

···

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 ?

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

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" :wink:

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

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 :frowning:
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:

On Fri, 11 Apr 2008, Robert Dober wrote:

        from :0

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

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

> 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

···

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>:

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

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

Kind regards

  robert

> 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 :frowning:
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:

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

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

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

···

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

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

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

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

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

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