Hi Peter,
In ruby you can extend built-in objects, so you can get the same
(general) functionality by adding a method to Enumerable (which is
mixed-in to String and Array [and Hash, too, but step is not meaningful
for that class]):
module Enumerable
def step(*args)
start, stop = 0, -1
case args.size
when 1
step = args[0]
when 2
start = args[0]
step = args[1]
when 3
start = args[0]
stop = args[1]
step = args[2]
else
raise(ArgumentError,
"`step': wrong number of " +
"arguments (#{args.size} for 3)",
caller)
end
if self.is_a?(Hash)
raise(NoMethodError,
"`step': not implemented for Hash",
caller)
end
seq = self[start..stop]
pos = 0
if self.is_a?(Array)
out = seq[0,1]
elsif self.is_a?(String)
out = seq[0,1].to_s
end
while pos < seq.size
val = seq[(pos += step)]
out << val unless val.nil?
end
out
end
end
# start at index 2 and step by 2...
p "abcdefg".step(2, 2) # => "ceg"
p %w{a b c d e f g}.step(2, 2) # => ["c", "e", "g"]
# this breaks
{'a'=>'b'}.step(2) # => -:60: `step': not implemented for Hash
(NoMethodError)
Regards,
Jordan