1

So this is my code. I'm learning while loops and not sure why this doesn't work. I'm getting an error.

i = 0
numbers = []
def while_var(x)
  while i < #{x}
  print "Entry #{i}: i is now #{i}."
  numbers.push(i)
  puts "The numbers array is now #{numbers}."
  i = i + 1
  puts "variable i just increased by 1. It is now #{i}."
 end

while_var(6)
 puts "Want to see all the entries of the numbers array individually (i.e. not in array format)? Here you go!"

for num in numbers
  puts num
 end

puts "1337"

This is my error

1.rb:5: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
  print "Entry #{i}: i is now #{i}."
         ^

I have no idea what this is all about. Thanks.

EDIT

So I have this revised code

def while_var(x)

  i = 0
  numbers = []

  while i < x
    print "Entry #{i}: i is now #{i}."
    numbers.push(i)
    puts "The numbers array is now #{numbers}."
    i = i + 1
    puts "variable i just increased by 1. It is now #{i}."
  end

  puts "next part"

  for num in numbers
    puts num
  end

end


while_var(6)

It works when I type it line-by-line into irb, but not when I run the file with ruby. What gives? I'm getting this error:

Entry 0: i is now 0.1.rb:8:in `while_var': undefined method `push' for nil:NilClass (NoMethodError)
    from 1.rb:23:in `<main>'

EDIT : Figured it out. All I had to do was change the "print" to "puts" for some reason.

macsplean
  • 611
  • 3
  • 8
  • 22

2 Answers2

2

Here is the fixed code :

def while_var(x)
  i = 0
  numbers = []
  while i < x
    print "Entry #{i}: i is now #{i}."
    numbers.push(i)
    puts "The numbers array is now #{numbers}."
    i = i + 1
    puts "variable i just increased by 1. It is now #{i}."
  end
end

You did several mistakes :

  • You forgot to close the while loop.
  • You used #{x} which is not correct syntax for interpolation,But you don't need interpolation here. make it only x.
  • Inside the method two local variables i and numbers can't be used,as they have created at the top level. So you need to create those variables locally inside the method.
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • 2
    Would you format it properly? To illustrate importance of formatting in spotting bugs? :) – Sergio Tulentsev Oct 02 '13 at 07:32
  • @MarekLipka You are most welcome.. please go ahead :) – Arup Rakshit Oct 02 '13 at 07:36
  • 1
    @macsplean did you also modify your `while` condition? – Marek Lipka Oct 02 '13 at 07:37
  • @MarekLipka no ... how would I recommend I do so? Thank you for your help btw! – macsplean Oct 02 '13 at 07:38
  • @macsplean it should be `while i < x`, just as ArupRakshit wrote. – Marek Lipka Oct 02 '13 at 07:40
  • now i am getting 1.rb:4:in `while_var': undefined local variable or method `i' for main:Object (NameError) from 1.rb:13:in `
    '
    – macsplean Oct 02 '13 at 07:41
  • hmm, now the code works up until the for loop, where I get an error regarding an undefined local variable or method 'numbers'. I must say I am rather confused about this issue. I am surprised that I need to define numbers and i within the while_var (I thought they were defined globally in the code I submitted in my question). – macsplean Oct 02 '13 at 07:46
  • 3
    Also check this for further reference why variables defined outside the method cannot be seen: http://stackoverflow.com/questions/9389432/ruby-can-not-access-variable-outside-the-method – Matt Oct 02 '13 at 07:46
  • Thanks. I just needed to put a $ in front of my variables to make them global. – macsplean Oct 02 '13 at 07:50
  • 1
    @macsplean abusing global variables is anti-pattern. And it's not Ruby-way at all. – Marek Lipka Oct 02 '13 at 07:54
  • Hey, I've revised my code, and something weird is still happening. Do you mind looking above at my edits and letting me know what you think? – macsplean Oct 02 '13 at 08:37
  • Never mind. Figured it out. All I needed to do was change the "print" to "puts", for some reason. – macsplean Oct 02 '13 at 08:40
2

This code should work:

def while_var(x)
  i  = 0
  numbers = []

  while i < x
    puts "Entry #{i}: i is now #{i}."

    numbers.push(i)
    puts "The numbers array is now #{numbers}."

    i = i + 1
    puts "variable i just increased by 1. It is now #{i}."
  end

  numbers
end

numbers = while_var(6)
puts "Want to see all the entries of the numbers array individually (i.e. not in array format)? Here you go!"

for num in numbers
  puts num
end

I hope it does what you wanted to achieve.

You should use puts to print something to console. And move i and numbers variables to while_var method.

Andrei
  • 486
  • 4
  • 5