Coding crisis: Stuck in a loop

So for the past week-and-a-half – much of which was marked by wondering/dreading about the not-so-slow march to our collective doom – I have been trying to do a Launch School assignment to code a variation of rock, paper, scissors [yes, the one also featured on Big Bang Theory, which I laughed at when I originally saw the episode]

Guess who hasn’t been laughing now?

I’ve coded so many variations of this that has broken so many times over the past week that it seems like a blur. Today I worked on this with a friend in Launch School, and we went back to the basics to figure out the things I was struggling with.

Input:

The first exercise he set was for me to look at input and how to validate input. First, it was to check whether an input was the right size, which required using an if/else condition to return whether an inputted string was equal to or greater than 1 character(s).

The next stage was to validate this input, so the code would tell the user to try again if it didn’t meet the conditions.

This requires a loop. My recurring error was not figuring out where the loop began – essentially, I had to wrap all of the code in a loop to start off with.

This was where I struggled since I didn’t figure out how to wrap the entire code in a loop and where to place a break. This is something I tend to do ordinarily with a lot of trial and error so it was good to work on a small piece step by step

array = ["coffee", "tea", "icecream"]
loop do
  puts "Make a choice"
  answer = gets.chomp
  if array.include?(answer)
    puts "This is available."
    break # also remember where to place the break statement!
  else
    puts "This choice is not listed. Try again."
    end
  end

Puts and return

The next step was to validate an input using a hash-key structure inside a method. While I wrote the conditional statements correctly, I also included puts with string interpolation to show me the output, forgetting that inside the method I was not working with a string, and also that I had to return the method.

How could I return it without puts? Puts isn’t the right way — puts is essentially returning nil, which isn’t useful.

One of my challenges is that I need to see code work in real-time. I tend to use a lot of puts statements when writing code so I can see if I’m on the right track . I also run small pieces of code in irb.

I had to use return — but I couldn’t see the code “work”, which is why I didn’t know whether I should use return.

OPTIONS = ['red', 'green', 'blue']
ALTERNATIVE_OPTIONS = {
  'r' => 'red',
  'g' => 'green',
  'b' => 'blue'
}

def converter(string)
  if ALTERNATIVE_OPTIONS.include?(string)
   return ALTERNATIVE_OPTIONS[string]
  else
    puts "Try again"
  end
end
converter('r') == "red"

# puts
def say(string)
  puts string
end
say("Speak!")

#return
def say(string)
  return string
end
say("Speak!")

Indenting!

I’m trying to get a bit better at this because running Rubocop on my code throws up a lot of errors that force me to fix it after. One of my problems is not indenting loops properly when I’m working, which is probably why I get so confused with where I’m supposed to place a break statement or where my code is breaking because it can all look like one big paragraph.

badly indented code:
puts "Welcome to Rock Paper Scissors Spock Lizard!" 
game_choices.each{|key,value| puts "Enter #{value} or #{key} for #{value}"}
    choice = gets.chomp
    comp = VALID_CHOICES.sample
    puts "You chose #{choice}, computer chose #{comp}"
answer = ''
comp_choice = ''

Methods

I think I find this really challenging, though when I do it in small bits it’s fine. I don’t know whether it’s a confidence thing, or that I need more practice.

Conclusion

All in all, this was a really good afternoon of coding even despite my frustration at seemingly failing at fundamental concepts. What I think happens is that I know the general logic, but I feel like I don’t know the language to be able to translate that logic - so I know what needs to happen but the actual code has me drawing a blank. (though that knowledge is somewhere in the recesses of my brain, I just have to invoke it!) I guess this will probably only come with solving lots of similar exercises where I have more confidence in myself and understand what each line of the code is doing.