P
Patrick Gundlach
Hi,
a very basic question...
I'd like to output the sequence "a b d e", by testing if the current
element is == "b" then skip the next element and continue the loop. The
obvious solution doesn't look rubyish to me, how could I use the first
or second attempt to get the desired solution?
Patrick
--------------------------------------------------
a=%w( a b c d e )
# incorrect, outputs "a b c d e"
0.upto(a.size - 1) do |i|
puts a
if a=="b"
# skip next element
# but i won't get affected
i += 1
end
end
# incorrect, outputs "a b c d e"
for i in 0...a.size
puts a
if a=="b"
# skip next element
# but i won't get affected
i += 1
end
end
# incorrect, outputs nothing... is there a next_next ?
a.each do |elt|
puts elt
if elt=="b"
# skip next element
# ??
end
end
# this one works, but is ugly
i=0
while i < a.size
puts a
if a=="b"
# skip next element
i += 1
end
i += 1
end
a very basic question...
I'd like to output the sequence "a b d e", by testing if the current
element is == "b" then skip the next element and continue the loop. The
obvious solution doesn't look rubyish to me, how could I use the first
or second attempt to get the desired solution?
Patrick
--------------------------------------------------
a=%w( a b c d e )
# incorrect, outputs "a b c d e"
0.upto(a.size - 1) do |i|
puts a
if a=="b"
# skip next element
# but i won't get affected
i += 1
end
end
# incorrect, outputs "a b c d e"
for i in 0...a.size
puts a
if a=="b"
# skip next element
# but i won't get affected
i += 1
end
end
# incorrect, outputs nothing... is there a next_next ?
a.each do |elt|
puts elt
if elt=="b"
# skip next element
# ??
end
end
# this one works, but is ugly
i=0
while i < a.size
puts a
if a=="b"
# skip next element
i += 1
end
i += 1
end