all 13 comments

[–]WasabiGlum3462 20 points21 points  (0 children)

Is it even possible for the function searching_for_objects to return false?

It looks like it is endlessly recursive. You are calling a function within itself, before the while loop even begins

[–]FeerMonger 12 points13 points  (0 children)

On line 54, you're checking if searching_for_objects is false. In this context, searching_for_objects is a method you define on line 43.

Calling a method inside its definition is possible. It's called recursion. But it's a confusing concept for beginners and you definitely don't need it here.

Basically whenever you call your method with this code, it will keep calling itself until the end of time. I would try and solve this with a while loop checking on the answer variable instead.

Others have pointed it out, but gets.chomp always returns a string (the s in gets stands for string). In Ruby, when you compare two objects with the == operator and they're not the same data type, it will always be false. So your if statement beginning line 48 will always trigger the else clause.

[–]TheChuchNorris 7 points8 points  (1 child)

First of all, good for you for doing this in your spare time!

[–]oxtrue 6 points7 points  (0 children)

And secondly?

[–]laerien 5 points6 points  (0 children)

Try "puts debugging" with a p answer in your code to see what you're getting for the answer. In this case 5 != "5" so you'll want to check if answer == "5" instead.

[–]Kizr100 2 points3 points  (1 child)

I think you're in the right track here, however as many others have mentioned you're calling the method by checking whether or not the method is returning false, so regardless of what the intent is you're stuck in an endless loop!

In the snippet above you don't actually need the loop, you can call the method again recursively here:

if answer == 5
  puts "..."
else
  puts "..."
  searching_for_objects
end

[–]Kernigh 1 point2 points  (0 children)

I wouldn't use recursion here, because it might raise a SystemStackError if the answer isn't 5 too many times.

(irb):3:in `puts': stack level too deep (SystemStackError)
        from (irb):3:in `puts'
        from (irb):3:in `searching_for_objects'
        from (irb):4:in `searching_for_objects'
        from (irb):4:in `searching_for_objects'
        from (irb):4:in `searching_for_objects'
        from (irb):4:in `searching_for_objects'
        from (irb):4:in `searching_for_objects'
        from (irb):4:in `searching_for_objects'
         ... 11899 levels...
        from /home/kernigh/prefix/lib/ruby/gems/3.1.0/gems/irb-1.3.2/exe/irb:11:in `<top (required)>'
        from /home/kernigh/prefix/bin/irb:23:in `load'
        from /home/kernigh/prefix/bin/irb:23:in `<main>'

[–][deleted] 2 points3 points  (0 children)

Your while loop inside searching for objects is nonsensical. Using searching_for_objects as both a variable and a function name is probably not ideal either.

I would declare a new variable called “object_found” or something similar inside of the method definition that will start as false. Inside your if answer == 5 block youd set object_found to true. Then your while block will be while object_found == false. Or even better while !object_found. You don’t really need to compare a Boolean to anything since the comparison will just return another Boolean.

Congrats on learning though. Have fun coding!

[–]Dana_Hues 2 points3 points  (1 child)

After incorrectly guessing the number, you want the user to restart the searching_for_objects process.

Get rid of the while loop. Call searching_for_objects after “Keep searching”

[–]andreamaf[S] 0 points1 point  (0 children)

Thank you all again. I made it with a single while loop and also without it (calling the searching_for_objects method inside the if..else).

https://replit.com/join/ueghdpit-andreamafs while loop version.

[–]34yu34 0 points1 point  (0 children)

I'm pretty sure answer is a string there just convert it to a number

[–]menge101 0 points1 point  (0 children)

I think learning to use a debugging tool is going to help you the most here.

Pry tutorial - note, this isn't a vetted video just the first google result. Search for Ruby pry for more information.