The_Exile

The_Exile

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.

===

Most Liked

AstonJ

AstonJ

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 :smiley:

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

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 :+1:

chrispine

chrispine

Author of Learn to Program

Yes, my book is geared for the beginner, and was specifically intended to be a springboard for the Pickaxe, which was already an excellent book. I was just trying to fill that gap.

And about my omission of parens in method definitions: that was the style 17 years ago when I first started working on this book. I am currently working on the 3rd Edition (I think this sentence is the first time I’ve said so publicly), and it includes parens for method definitions and most method calls (as well as numerous other changes, a totally new chapter, etc).

Margaret

Margaret

Editor at PragProg

So excited to have your new edition coming out, Chris! And thank you for being so generous with your time. I have seen how much you help your readers when they get stuck. It’s so valuable to have that kind of support when learning!

Popular Backend topics Top

andrea
Can Phoenix LiveView be used in multi-page applications, unlike React/Vue/Blazor which seems to be targeted for SPA?
New
joshi
Hey everybody! I’m working on the project that includes import of Oracle data to PostgreSQL. That data comes as Oracle export (expdp) fi...
New
GermaVinsmoke
Reading Programming Elixir 1.6 book, I’ve completed part 1 of the book. Now I’m thinking of reading Elixir in Action. What do you all sug...
New
sampu
I have a use case where a client is invoking a Rest endpoint via a load balancer, which in turn invokes a third party endpoint which is r...
New
Fl4m3Ph03n1x
Background I am trying to encode a structure into json format using the Jason library. However, this is not working as expected. Code L...
New
GermaVinsmoke
Does anyone know beginner friendly Elixir/Phoenix Open source projects? For learning purpose :slightly_smiling_face:
New
harwind
I received this error for a binary search programme in C, despite the fact that it requested for inputs and produced the right output. Th...
/c
New
pillaiindu
Currently reading the book “Programming Phoenix LiveView”. At the end of the Chapter 1, I’m trying to solve the guess game. If the user ...
New
Fl4m3Ph03n1x
Background I have a release file inside a tarball. However I want the final release to have some additional files and to move things aro...
New
Fl4m3Ph03n1x
Background I have an umbrella project, where I run mix test from the root. In one of the apps, I am mocking the File module using the Mo...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
AstonJ
Or looking forward to? :nerd_face:
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New