3

Using the following code in Ruby:

if (tickFormat.length > 12 && tickFormat.length < 24)
    i = 1
    while(i < tickFormat.length) do
        if (i%2 != 0)
          tickFormat.at(i)[1] = ''
        end
        i++
    end
end

I get an "unexpected keyword_end" for the 2nd "end" statement. If I remove the while loop the code runs without error. Any ideas?

2
  • 5
    There's no i++ in Ruby. Commented Jan 17, 2013 at 18:40
  • I'm curious to know why this question was down-voted so that I don't make the same mistake in the future? Commented Jan 17, 2013 at 19:24

5 Answers 5

3

Try this:

if (tickFormat.length > 12 && tickFormat.length < 24)
  i = 1
  while(i < tickFormat.length) do
    if (i%2 != 0)
      tickFormat.at(i)[1] = ''
    end
    i += 1
  end
end

The i++ syntax doesn't work in Ruby

Sign up to request clarification or add additional context in comments.

1 Comment

Probably because there's no such thing in Ruby :p
3

You're obviously from the C corner so some additional info from my side:

First I would like to quote Alexey Anufriyev

I think you should use naming convention adopted by your platform. underscore_case will look weird in C# code, as camelCase in Ruby =)

So you should consider using underscore notation.

Also you're code is pretty C style. This is a bit more ruby like (though it is a matter of taste whether you write short statements in the same line or not):

if tick_format.length === (13..23)
  tick_format.each_with_index do |tf, i|
    tf[1] = '' if i.odd?
  end
end

# if you use active support's core extensions (which is recommended imho)
# you can rewrite line 1 as:
if tick_format.length.in? 13..23

Comments

1

Ruby has too much sugar to code it C-style :)

Try something like

if tickFormat.length.between?(13,23)
  (1..tickFormat.length).step(2) do |i|
    tickFormat.at(i)[1] = ''
  end
end

Comments

0

You want to use i+=1 to increment.

if tickFormat.length > 12 && tickFormat.length < 24
        i = 1
        while i < tickFormat.length do
            if (i%2 != 0)
              tickFormat.at(i)[1] = ''
            end
            i+=1
        end
    end

Comments

0

The only thing wrong is the i++. I might suggest migrating the style a bit from C-like to canonical-Ruby, but that's just a personal choice...

if (13..23) === tickFormat.length
  i = 1
  while i < tickFormat.length
    if i % 2 != 0
      tickFormat.at(i)[1] = ''
    end
    i += 1
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.