From: David Alan Black Date: 2001-08-04T21:50:23+09:00 Subject: [ruby-talk:19129] Re: My 1st look @ ruby: No prototypes and problem with String#gsub Hello -- On Sat, 4 Aug 2001, Stefan Scholl wrote: > class Shuffle > def initialize(data) > raise(TypeError, "not an array") if data.type != Array > @store = data > @mark = Array.new > @marked = 0 > end > > def each > while @marked < @store.length > while @mark[i = rand(@store.length)] > end > @mark[i] = true > @marked += 1 > yield @store[i].gsub(/ /, '\\ ') > end > end > end > > Shuffle.new(ARGV).each {|s| print "#{s} " } This seems like a lot of code to write, and variables to use, to get this to happen. Also, I don't think Shuffle makes for a very good class. Shuffling is a more method-like concept. It's also a general enough thing that you could make a case for adding it directly to class Array. It might be handy to have a "bang" (modify in place) version and a non-bang version: class Array def my_shuffle! size.times do push slice! rand(size) end self end def my_shuffle dup.my_shuffle! end end ARGV.my_shuffle.each do |e| print e.gsub(/ /, '\\\\\&') + " " end (Maybe some mathematician could check this for correctness :-) It actually runs pretty fast for non-huge arrays, if I'm remembering some old benchmark results correctly.) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav