# Another noob question

**URL:** <https://www.ruby-forum.com/t/another-noob-question/185949>\
**Category:** Ruby\
**Created:** [March 29, 2010, 4:55pm UTC](https://www.ruby-forum.com/t/another-noob-question/185949 "2010-03-29T16:55:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mehektet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mehektet](https://www.ruby-forum.com/u/mehektet)\
**Post date:** [March 29, 2010, 4:55pm UTC](https://www.ruby-forum.com/t/another-noob-question/185949/1 "2010-03-29T16:55:09Z")

</div>

Thanks to everyone that helped me a few days ago. This time I am trying  
to write a program that incorporates everything that I have learned in  
the last week but I am more than sure that I am making mistakes  
somewhere. Also there is somethings I do not know how to do.  
Everything is commented so it should be easy to find. Also if anyone  
has any suggestions about my syntax style please give me advice. Thanks  
again everyone!

* * *

# Program written by Dr. Omar Israel Velez

# This program will try to integrate several concepts that I have

learned so far

# The program will ask for some basic information and then it will ask

for a

# list of favorite books. Afterwards it will sum up the total number of

# characters in the book titles and add how many of each letter there

are.

# finally it will offer a new book as a suggestion based on which letter

was the

# most popular.

puts “What\s your first name?” ; first\_name = gets.chomp ; puts  
puts “What’s your last name?” ; last\_name = gets.chomp ; puts

puts “Well, #{first\_name} #{last\_name}, tell me four books that you have  
read:”  
puts  
books\_one = gets.chomp ; books\_two = gets.chomp  
books\_three = gets.chomp ; books\_four = gets.chomp

all\_books = ["#{books\_one.downcase}", “#{books\_two.downcase}”,  
“#{books\_three.downcase}”, “#{books\_four.downcase}”].sort

# Is there a more efficient way to write this code where I don’t have to

# isolate each one and I can downcase everything at once?

puts

puts “These are the books that you entered #{first\_name}.” ; puts

all\_books.each do |books|  
puts books  
end

puts

puts “#{first\_name} are these books correct?” ; answer = gets.chomp ;  
puts

if answer == ‘yes’  
puts “Ah, very good choice of books #{first\_name}.”  
else  
puts “Well computers never make mistakes so it must be in your head.”  
puts “Run the program again please.”  
end

puts

a = books\_one.length ; b = books\_two.length  
c = books\_three.length ; d = books\_four.length

# Same question as before, can I .length everything at the same time

and make

# the code more efficient?

total\_characters = a + b + c + d

# I cheated and just went with the total number of characters. But how

do I

# add up the total number of each letter and then suggest a book for

each letter?

# just two or three examples will be ok I know asking for 26 examples

is just crazy.

puts “There are #{total\_characters} total characters in the tittles you  
read.”  
puts

if total\_characters \>= 50  
puts “I recommend reading The Hobbit.”  
puts “There is a #{rand(101)}% chance you will like this book.”  
puts “Would you like to read this book?” ; answer = gets.chomp ; puts  
if answer == ‘yes’  
puts “I am sure that you will enjoy it #{first\_name}”  
else  
puts “Oh that is too bad, how about Animal Farm then?”  
puts “There is a #{rand(101)}% chance you will like this book.”  
end  
else  
puts “I recommend you read The Tale of Two Cities.”  
puts “There is a #{rand(101)}% chance you will like this book.”  
puts “Would you like to read this book #{first\_name}?” ; answer =  
gets.chomp  
puts  
if answer == ‘yes’  
puts “I am sure that you will have a great time reading it!”  
else  
puts “Ok then, how about A Day in the Life of Alex Jones?”  
puts “There is a #{rand(101)}% chance you will like this book.”  
end  
end

# I also tried to create a global constant like so…

# percent = “There is a #{rand(101)}% chance you will like this book.”

# and then point to it to make the code look cleaner but it would not

let me.

# How would I do this?

---

<div class="post-metadata">

**Author:** ![mehektet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mehektet](https://www.ruby-forum.com/u/mehektet)\
**Post date:** [March 29, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/another-noob-question/185949/2 "2010-03-29T17:27:09Z")

</div>

On Mon, Mar 29, 2010 at 4:55 PM, Omar V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> # Program written by Dr. Omar Israel Velez
> 
> # most popular.
> 
> puts “What\s your first name?” ; first\_name = gets.chomp ; puts  
> puts “What's your last name?” ; last\_name = gets.chomp ; puts
> 
> puts “Well, #{first\_name} #{last\_name}, tell me four books that you have  
> read:”  
> puts

# \> books\_one = gets.chomp ; books\_two = gets.chomp

# \> books\_three = gets.chomp ; books\_four = gets.chomp

# \> all\_books = [“#{books\_one.downcase}”, “#{books\_two.downcase}”,

# \> “#{books\_three.downcase}”, “#{books\_four.downcase}”].sort

all\_books = []  
4.times do  
all\_books \<\< gets.chomp.downcase  
end

all\_books.sort!

> # Is there a more efficient way to write this code where I don’t have to
> 
> # isolate each one and I can downcase everything at once?
> 
> puts
> 
> puts “These are the books that you entered #{first\_name}.” ; puts

# \> all\_books.each do |books|

# \> puts books

# \> end

puts books

> puts “Run the program again please.”  
> exit

> end
> 
> puts

# \> a = books\_one.length ; b = books\_two.length

# \> c = books\_three.length ; d = books\_four.length

> # Same question as before, can I .length everything at the same time
> 
> and make
> 
> # the code more efficient?

# \> total\_characters = a + b + c + d

total\_characters = all\_books.inject(0) {|total, book| total +  
book.length}

> # I cheated and just went with the total number of characters. But how
> 
> do I
> 
> # add up the total number of each letter and then suggest a book for
> 
> each letter?
> 
> # just two or three examples will be ok I know asking for 26 examples
> 
> is just crazy.

# The letter histogram

h = Hash.new(0)  
all\_books.each {|b| b.split(//).each {|letter| h[letter] += 1}}

If you want to suggest a book for each letter, you’ll have to have a  
list of books by letter. Something like:

recommendations = {‘a’ =\> “A whatever”, ‘b’ =\> “because…”} # Crazy !  
I didn’t come up with any book title !!! (I’m not a native speaker)

Then you can recommend based on the present letters, if I understood  
correctly:

h.keys.each {|initial| puts “I recommend: #{recommendations[initial]}”}

> else  
> puts “I am sure that you will have a great time reading it!”  
> else  
> puts “Ok then, how about A Day in the Life of Alex Jones?”  
> puts “There is a #{rand(101)}% chance you will like this book.”  
> end  
> end

Whenever you see duplicated code, that’s a good place to refactor:

def recommend(name, books)  
books.each do |book|  
puts “I recommend you read #{book}”  
puts “There’s a #{rand(101)}% chance you’ll like it”  
puts “Would you like to read this book #{name}?”  
answer = gets.chomp  
if answer == “yes”  
puts “I’m sure you’ll like it”  
break  
end  
end  
end

or something like that, then:

if total\_characters \< 50  
recommend(first\_name, [“The Hobbit”, “Animal Farm”])  
else  
recommend(first\_name, [“The Tale of Two Cities”, “A Day in the Life  
of Alex Jones”])  
end

Well, this is not exactly equivalent to your code, but you get the idea.

> # I also tried to create a global constant like so…
> 
> # percent = “There is a #{rand(101)}% chance you will like this book.”
> 
> # and then point to it to make the code look cleaner but it would not
> 
> let me.
> 
> # How would I do this?

What didn’t work?

percent = “There is a #{rand(101)} chance you’ll like it”  
puts percent

works for me. If you mean that it’s always the same percentage, then  
make it a method:

def percentage\_sentence  
“There is a #{rand(101)} chance you’ll like it”  
end

puts percent

Jesus.
