Maybe I'm just being thick, but wouldn't it just be easier (and much
closer to what you are actually trying to do) to say
["a", "b", "c", "d", "e"].each do |x|
puts x unless x == "c"
end
I'll admit I'm also new to Ruby but isn't the above the more "rubyish"
way to achieve what you're trying to do?
On 12/5/05, Patrick Gundlach <clr9.10.randomuser / spamgourmet.com> wrote:
> 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[i]
> if a[i]=="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[i]
> if a[i]=="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[i]
> if a[i]=="b"
> # skip next element
> i += 1
> end
> i += 1
> end
>
>