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

So I am not getting similar code to what Chris is when I do the examples he wants us to do, and many of the things he brings up after Chapter 6 seem to be lacking some explanation to create a full understanding so I can fully absorb the concept.
Everything in previous chapters, we would get similar looking stuff. It’s only now that we’ve gotten to branching and looping, and everything afterward that things completely fall apart for me.
Deaf Grandma
My code
puts ''
puts ''
gma = 'Grandma replies: HUH?! SPEAK UP, SONNY!'
year = ''
year_roll = rand(20)
gma_canhear = 'Grandma replies: NO, NOT SINCE ' + year + '!'
if year_roll == 0
year = '1930'
elsif year_roll == 1
year = '1931'
elsif year_roll == 2
year = '1932'
elsif year_roll == 3
year = '1933'
elsif year_roll == 4
year = '1934'
elsif year_roll == 5
year = '1935'
elsif year_roll == 6
year = '1936'
elsif year_roll == 7
year = '1937'
elsif year_roll == 8
year = '1938'
elsif year_roll == 9
year = '1939'
elsif year_roll == 10
year = '1940'
elsif year_roll == 11
year = '1941'
elsif year_roll == 12
year = '1942'
elsif year_roll == 13
year = '1943'
elsif year_roll == 14
year = '1944'
elsif year_roll == 15
year = '1945'
elsif year_roll == 16
year = '1946'
elsif year_roll == 17
year = '1947'
elsif year_roll == 18
year = '1948'
elsif year_roll == 19
year = '1949'
else year_roll == 20
year = '1950'
end
gma_canhear = 'Grandma replies: NO, NOT SINCE ' + year + '!'
#Update gma_canhear variable with year value after the year_roll if branch.
wistg = 'a'
# what I say to grandma = wistg | This is it's inital value before being updated. If I left it blank it
# would cause an error. I don't know why.
while wistg != wistg.upcase
puts 'You say to Grandma...'
wistg = gets.chomp
puts ''
if wistg == wistg.upcase
puts ''
puts gma_canhear
puts ''
elsif wistg == 'BYE'
break
else puts gma
puts ''
end
end
puts ''
puts ''
Chris’ code
puts 'HEY THERE, SONNY! GIVE GRANDMA A KISS!'
while true
said = gets.chomp
if said == "BYE"
puts 'BYE SWEETIE!'
break
end
if said != said.upcase
puts 'HUH?! SPEAK UP, SONNY!'
else
random_year = 1930 + rand(21)
puts 'NO, NOT SINCE ' + random_year.to_s + '!'
end
end
or
puts 'HEY THERE, SONNY! GIVE GRANDMA A KISS!'
while true
said = gets.chomp
break if said == "BYE"
response = if said != said.upcase
'HUH?! SPEAK UP, SONNY!'
else
"NO, NOT SINCE #{rand(1930..1950)}!"
end
puts response
end
puts 'BYE SWEETIE!'
His code is vastly more compact. I just don’t understand how he was able to do that. I barely understand what he did.
I did what I could, with what I learned form the book up to that point and never in my wildest dreams would I have come up with anything like what he did.
===
Popular Backend topics

Real-time applications come with real challenges—persistent connections, multi-server deployment, and strict performance requirements are...
New

Go is a modern programming language that combines the reliability of compiled languages with the ease of use and flexibility of dynamic t...
New

Learning Clojure involves much more than just learning the mechanics. To really get Clojure you need to understand the ideas underlying i...
New

Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New

Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New

Hello folks! We had a pretty fun thread here around the same time last year - talking about Advent of Code problems. That also happened t...
New

This is going to be a long an frequently posted thread.
While talking to a friend of mine who has taken data structure and algorithm cou...
New

Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New

Hello,
I am working on a new application with Elixir, Dish_out. I want to see Data I follow this tutorial with Elixir Casts. However, I ...
New

If when trying to create (or recreate) your dev db with rails db:create you are getting:
PG::ConnectionBad: connection to server on soc...
New
Other popular topics

Any thoughts on Svelte?
Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New

I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New

SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New

Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New

I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl:
The Ser...
New

Do the test and post your score :nerd_face:
:keyboard:
If possible, please add info such as the keyboard you’re using, the layout (Qw...
New

Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalabilit...
New

Author Spotlight: Mike Riley (@mriley)
This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s bo...
New

Author Spotlight: VM Brasseur (@vmbrasseur)
We have a treat for you today! We turn the spotlight onto Open Source as we sit down with...
New

This is cool!
DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON
We just witnessed something incredible: the largest open-s...
New
Latest in Backend
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /preact
- /flutter
- /c-plus-plus
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm