Adding Functionality to Ruby Strings

Ok, here is the scoop. I was working on a project using Ruby and needed to grab three characters from a string skipping one character in between.

ex –> "123456789" would become 234 and 567

I thought this would be a great opportunity to try out blocks in Ruby and test open classes

class String
   def blocker(sizeOfGroup,offset)
     while(val=self.slice!(0..offset.abs-1))
        break if val.to_s.length < 1
        yield  self.slice!(0..(sizeOfGroup.abs-1))
      end
  end
end

here is the code I used to test with

string = "12345678910"
ary=[]
string.blocker(3,-1){|v| ary.push(v)}
ary.each{|i|puts i}
puts "here is ary "+ary.inspect

And this is the result.

>>> test.rb

234
678
10
here is ary ["234", "678", "10"]

This was definitely a fun experiment and I can't wait to try out more stuff. If you haven't played around with blocks, give it a try...you just might like it.