From: Martin DeMello Date: 2008-04-12T02:08:55+09:00 Subject: Re: how to create random object to a particular ruby object ? On Fri, Apr 11, 2008 at 2:17 AM, Pokkai Dokkai 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) => "I9K@Nr" irb(main):008:0> String.make_random(10) martin