T O P

why doesn't this code work properly? i want the program to taken fifty steps and stop, but it keeps counting

why doesn't this code work properly? i want the program to taken fifty steps and stop, but it keeps counting

ankole_watusi

You told it to loop forever. It loops forever! Telling it to print "Stop." doesn't make it stop. It makes it print "Stop.", on the 50'th time through the loop. The ***50.times*** answer is the Ruby way.


Cyanogen101

This


justanemptyvoice

Because you don’t break the while loop anywhere. You start off with a while true, which is always true. It’s an infinite loop.


[deleted]

As others have said there are better ways to do this in ruby but your way would be more like this `def start_game` `while @steps_taken <= 50` `puts "you have taken #{@steps_taken} steps."` `@steps_taken +=1` `end` `end`


letscallitanight

Use a 50.times loop


ankole_watusi

Yes, this is the ruby way.


juanmanuelramallo

This is the way


CollectiveOfCells

This. Your entire program can be expressed as: 50.times {|i| puts "You have taken #{i} steps"}


pseudoentropic

Add a `break` to your `if @steps_taken == 50` branch, after the `puts`


armahillo

Do you want `steps_limit == 50` to do an assignment or actually check equality? If it's an assignment, you should use an instance variable (@steps_limit) and the assignment operator (=) if it's an equality check then you should move it out if the initialixer and info the main loop Also --are you sure you want to do `while true` instead of ` while @steps < @steps_limit` or something? Using break from a loop feels like codesmell here


wernercd

Maybe I'm crazy but a pet peeve of mine: Rule #1. Pictures of code are the root of all evil. Here's a working sample of your code: [http://rubyfiddle.com/riddles/3c180/4](http://rubyfiddle.com/riddles/3c180/4) * You do "while true" and never break the loop. * You create steps\_taken... then don't use it. Most likely because... * initialize: * assign steps\_taken * run game method * assign steps\_limit * when you run the game method, `steps_limit` hasn't been assigned yet. I assume you were getting a "nil" error of some sort because limit wasn't assigned yet. If you use a WHILE, you need to have a control statement. "While true" means the control statement is "true" and it will continue forever. If you want to use while, you should use "while steps taken less than steps limit". Or you could use "loop do ... break... while" or "for x = 1 to 50" or "until condition do ... end" [https://www.geeksforgeeks.org/ruby-loops-for-while-do-while-until/](https://www.geeksforgeeks.org/ruby-loops-for-while-do-while-until/) Lots of variations in the above article.


Eat_the_Path

Am I only one deeply concerned that there's a method call followed by a boolean in `initialize`?


deillo

Is this not needlessly cryptic for a beginner? OP: you might want to revisit and better utilise the steps_limit variable and set it correctly with one =, move it before the method, and then in the method use it to compare to @steps_taken along with some of the other suggestions here to stop the infinite loop


Eat_the_Path

I didn't mean it to be. `initialize` is a special method called when new instances of a class are created with `#new`. It is used to set the initial value of variables you want to use in the class. Multiple methods in the class may use these variables. You may want to set instance variables from class variables, changing their scope. It is not for calling methods and the last line returns a boolean which is not assigned to anything and thus has no purpose. If you want to use the method in this class, outside of the class, define something like : `Game1 = Game.new` to create a new instance of the Game class. The variable name can be anything you like. `Game1.start_game` to call the method on that individual instance. Personally, I would replace `while` with `until` and make your `step_limit` value an argument.


deillo

Thank you for taking the time to explain that. Things like this may seem obvious, but when you’re starting out they are really useful to have explained.


Mobile-Revolution-19

i've been following this tutorial https://jsrn.gitbooks.io/make-your-first-text-adventure-in-ruby/content/the_game_loop.html


hereticmoox

You mean broken code? It'll error as soon as it hits that line since `steps_limit` is not defined anywhere. I imagine OP meant to initialize and assign on that line, but added an extra `=`.


ForwardAerial

I haven't written in ruby in quite some time, but it might be because you're using `while true`. Can you use `while steps_taken < 50`?


theo_ed_tdaar

you never break it


bradland

You have `puts “Stop.”` inside your 50th step conditional, but that won’t affect script execution. It’ll just print that text to the screen with a newline and keep going. On the next iteration of the while loop, `@steps_taken` will be 51, 52, etc. You have to use a flow control keyword to tell the script to jump out of the loop. I’m this case, `break` is the keyword you want.


[deleted]

[удалено]


concretman

`steps_limit` is not used anywhere else, this code just needs to have a `break` statement.


erikamaker

this


achauv1

This is depressing


iam_thesauce

Yeah you need a break statement and after it passes I would refactor to a few lines


andyk314

Instead of the while true, you can just create a variable like completed = false, and set the variable to be true once you hit 50 steps. And use while !completed… another alternative. Hope that make senss


seanyover9000

Add a break after puts "stop".


vigilexe

you didn't break out of the while loop so it's going to keep incramenting the step counter. once it hits your finish condition add a break statement to stop the loop.


skotchpine

4 spaces not 2 spaces... so I’m not going to read anything else and assume lack of attention to convention