# Not fully understanding the code in the example in "Learn to Program"

**URL:** <https://forum.devtalk.com/t/not-fully-understanding-the-code-in-the-example-in-learn-to-program/1123>\
**Category:** Backend Questions/Help\
**Tags:** ruby, book-learn-to-program-2nd-edition\
**Created:** [21 July 2020 03:04 UTC](https://forum.devtalk.com/t/not-fully-understanding-the-code-in-the-example-in-learn-to-program/1123 "2020-07-21T03:04:43Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![AstonJ](https://forum.devtalk.com/user_avatar/forum.devtalk.com/astonj/32/7_2.png) [@AstonJ](https://forum.devtalk.com/u/AstonJ)\
**Post date:** [21 July 2020 13:16 UTC](https://forum.devtalk.com/t/not-fully-understanding-the-code-in-the-example-in-learn-to-program/1123/2 "2020-07-21T13:16:17Z")

</div>

Firstly, well done for writing what you did! I wouldn’t worry too much about it being different to what Chris wrote because often programmers will quite write something just to get things working, then they’ll rewrite (refactor) their code to make it better. That could be to make it shorter, or to reorganise or split it up into multiple methods to make it easier to maintain and re-use etc. The thing to note is that this is completely normal - what you end up with is often quite different to your first attempt 😃

Ok let’s have a look at Chris’s code:

```ruby
puts 'HEY THERE, SONNY! GIVE GRANDMA A KISS!'
while true
  users_input = gets.chomp
  if users_input == "BYE"
    puts 'BYE SWEETIE!'
    break
  end
  if users_input != users_input.upcase
    puts 'HUH?! SPEAK UP, SONNY!'
  else
    random_year = 1930 + rand(21)
    puts 'NO, NOT SINCE ' + random_year.to_s + '!'
  end
end

```

I have changed it very slightly - I changed the name of the `said` variable, to `users_input`.

The program starts by printing “HEY THERE, SONNY! GIVE GRANDMA A KISS!” and then waits for user input with the `gets.chomp` command. By prefixing it with `users_input = ` means the result of it will be bound to the `users_input` variable - which means you can then use/do something with the result.

Which is exactly what you do with the code below it; `if users_input == "BYE"` this is what you’ll use to terminate the program, so it needs to be assessed before anything else. If the user’s input is ‘BYE’ the program terminates by first saying “BYE SWEETIE!” then `break`.

If the user’s input isn’t “BYE” then that bit of code is ignored and the program moves on to the next bit of code: `if users_input != users_input.upcase` and what that’s doing is asking the computer to check whether the user’s input is in capital letters, which is what `upcase` is used for. The `!=` bit means ‘does not’ so that full line of code is saying: let’s compare the user’s input to what we want it to to be - the user’s input in capitol letters, so for example if the user’s input was “please give me a kiss” and what we wanted was “PLEASE GIVE ME A KISS” then they would not be equal, making the `if users_input != users_input.upcase` statement true, which would then execute the code in that block, which is `puts 'HUH?! SPEAK UP, SONNY!'`.

The final part of the code will run if the user’s input is in capital letters, and in that case the program will first create a random year by choosing a random number from 0 to 21 with the `rand(21)` function and assign that year to the `random_year` variable, and then print “NO, NOT SINCE” along with the `random_year` converted to a string with the `to_s` method (followed by an exclamation mark).

Does that make sense? If so here’s what I recommend you do - change it! Alter the code to do something a little bit different. When you start doing that it will make even more sense!

_Btw, I’ve split this into a dedicated thread as it could help someone else stuck on the same exercise 👍_

---

_[View the full topic](https://forum.devtalk.com/t/not-fully-understanding-the-code-in-the-example-in-learn-to-program/1123)._
