Вопрос

I'm on Chapter 33 of Learn Ruby the Hard Way.

Extra credit exercise 1 asks:

Convert this while loop to a function that you can call, and replace 6 in the test (i < 6) with a variable.

The code:

i = 0
numbers = []

while i < 6
  puts "At the top i is #{i}"
  numbers.push(i)

  i = i + 1
  puts "Numbers now: #{numbers}"
  puts "At the bottom i is #{i}"
end

puts "The numbers: "

for num in numbers
  puts num
end

My attempt:

i = 0
numbers = []

def loops
while i < 6
  puts "At the top i is #{i}"
  numbers.push(i)

  i = i + 1
  puts "Numbers now: #{numbers}"
  puts "At the bottom i is #{i}"
end
 end

 loops
 puts "The numbers: "

for num in numbers
  puts num
end

As you can see, I got as far as trying to make the block into a function, not yet making the 6 a variable.

Error:

ex33.rb:5:in `loops': undefined local variable or method `i' for main:Object (Na
meError)
        from ex33.rb:15:in `<main>'
    from ex33.rb:15:in `<main>'

What am I doing wrong?

EDIT: Okay, improved it a little. Now the numbers variable is out of scope...

def loops (i, second_number)
numbers = []
while i < second_number
  puts "At the top i is #{i}"
    i = i + 1
  numbers.push(i)
  puts "Numbers now: #{numbers}"
  puts "At the bottom i is #{i}"
end
 end

loops(0,6)
puts "The numbers: "

for num in numbers
  puts num
end
Это было полезно?

Решение

As @steenslag says, i is out of scope within loops. I would not recommend switching to using @i because i is only used by loops.

Your function is a utility that can be used to produce an array of numbers. The function uses i to figure out how far through it is (but the caller of the function doesn't care about this, it only wants the resulting numbers). The function also needs to return numbers, so move that inside loops too.

def loops
  i = 0
  numbers = []

  while i < 6
    puts "At the top i is #{i}"
    numbers.push(i)

    i = i + 1
    puts "Numbers now: #{numbers}"
    puts "At the bottom i is #{i}"
  end
end

You now have to think about the fact that the caller of loops can no longer see numbers. Good luck with your learning.

Другие советы

When you say def, i goes out of scope. The method can't "see" it. Use @i in stead (the @ gives the variable a greater "visibility"), or move the i=6 inside the method, or figure out how to use parameters with a method.

I may have misread the 'convert the while loop' but my solution was:

def loop(x, y)

   i = 0
   numbers = []

   while i < y
      puts "At the top i is #{i}"
      numbers.push(i)

      i += 1
      puts "Numbers now: ", numbers
      puts "At the bottom i is #{i}"
   end

   puts "The numbers: "

# remember you can write this 2 other ways?
numbers.each {|num| puts num }

end

loop(1, 6)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top