tags:

views:

155

answers:

5

I find the .each do hard to get to stick, so I was hoping for regular use of C for loop syntax which seems to not work, so I tried a while but still get errors.

I have tried this.

i = 0
while i < SampleCount
    samples[i] = amplitude
    amplitude *= -1
    i++
end

I get complaints about the end statement here.

+8  A: 

There are several problems with your code. Rather than just fixing the errors, I'd suggest it's better long-term for you to learn the Ruby way - it will save you time and energy later. In this case, it's

5.times do |i|
  samples[i] = amplitude  # assumes samples already exists and has 5 entries.
  amplitude *= -1
end

If you insist on keeping a similar style, you can do this:

samples = []
i = 0
while i < sample_count
    samples << amplitude  # add new item to array.
    amplitude *= -1
    i += 1                # you can't use ++.
end

Note that SampleCount's initial capital letter, by Ruby convention, means a constant, which I'm guessing isn't what you really mean.

Peter
Thank you. The reason to go for a while loop is that I'm familiar with it from C. SampleCount is a constant.
Fred
yep, I understand you're familiar with C, but that's not really a good idea to use C-like syntax - you can see already that the differences between C and Ruby got you in trouble.
Peter
Thanks Peter, I'll follow your advice and go with your first example. I'm pretty new with Ruby as you probably can tell. :-)
Fred
+1  A: 

You originally mentioned trying to use a for loop. Notwithstanding the various other comments in the answers, here's the for loop approach:

for i in 0...5
  samples[i] = amplitude
  amplitude *= -1
end
Peter
That's great, I think I got confused with Python which if I remember correctly allows for C style syntax in for loops. Anyway it's good to get a variety of approaches. Thanks.
Fred
You want `in` where you have `=`, that is `for i in 0..5` will do it.
Telemachus
absolutely. how embarassing.
Peter
@Peter: no, not at all. Just a typo. Looking now at my comment, I had an off-by-one goof as well (should be `for i in 0...5`).
Telemachus
+2  A: 

I agree with Peter that there are other (more idiomatic) ways to do this in Ruby, but just to be clear: the error message you saw misdirected you. There wasn't anything wrong with your while loop per se. The problem was i++ since there is no ++ operator in Ruby.

This would work just fine:

limit = 10

i = 0
while i < limit
    puts i
    i += 1
end

Again, I'm not recommending it, but if you're just learning the language, it may help to know where the problem really was.

Ruby has a lot of built-in ways to iterate other than for or while (which tend to be seen less often, as far as I can tell). A few other examples:

(1..10).each do |x| # 1..10 is a range which you can iterate over with each
  puts x
end

1.upto(10) { |x| puts x } # Integers have upto and downto methods that can be useful
Telemachus
Thanks, yeah that is an example of what makes Ruby interesting IMO. It's elegant but also kind of freaky looking when you just start out.I realize that this is more in line of how Ruby suppose to look though.
Fred
A: 

Nobody here has actually offered an alternate solution that actually does what Fred originally intended - and that's iterate around the value of the constant SampleCount. So could you do:

SampleCount.times do |i|

Or:

limit = SampleCount
limit.times do |i|

Or:

for i in 0..SampleCount

Would any of those be Ruby-esque enough?

Cam Fox
Actually, both Peter's answer and mine provide more than enough information for Fred to create his own loop. Your second answer has an off-by-one error, since Fred's original said `while i < SampleCount` - substitute `for i in 0...SampleCount` to omit the upper number.
Telemachus
"...left as an exercise for the reader..." kind of supercilious answer isn't really necessary (or necessarily helpful), though. Although, it's clear you're trying to help, using SampleCount seems to be a bit of an key factor (and the SampleCount.times do |i| pattern was taken from Peter's first suggestion anyway).
Cam Fox
Thanks for the contribution. I think I'm staring to get the hang of Ruby loops. What kept me puzzled was mainly that there always seems to be ten ways (at least) to do something in Ruby.
Fred
A: 

The problem with the end statement is related to i++. Ruby wants to add something. There is no increment operator in Ruby. You need to use i += 1. With that change you can use your C style loop as is.

Gerhard
Yes, that will actually fix the problem. I think that the error message wasn't that descriptive, but then again perhaps it's considered common knowledge that Ruby lacks an increment operator. Definately good to know though.
Fred