How to display multiple items using puts? - ruby

I have written a Travel App with Tour Items and I'm trying to display the order at the end.
When I use puts #order_items I get {"SevendaySurfSportTour"=>2} for two tours.
I would like it to display
2 SevendaySurfSportTour at the end. But I don't know how, any help would be good?
class TourOrder
def initialize
#order_items = Hash.new(0)
end
def add_item(name, quantity)
#order_items[name] += quantity
end
def get_items
return #order_items
end
def display
puts "Thank you for coming!"
puts #order_items
end
end

#order_items is a hash and what has printed out if the string representation of such a hash. When you want to format the output differently, then you have to implement that on your own – for example like this:
def display
puts "Thank you for coming!"
#order_items.each do |name, quantity|
puts "#{quantity} #{name}"
end
end

Below code works for you
class TourOrder
def initialize
#order_items = Hash.new(0)
end
def add_item(name, quantity)
#order_items[name] += quantity
end
def get_items
#order_items
end
def display
puts "Thank you for coming!"
puts items
end
def items
#order_items.collect{ |k, v| "#{v} #{k}"}
end
end
t = TourOrder.new
t.add_item('SevendaySurfSportTour', 2)
t.add_item('Foo', 4)
t.add_item('Bar', 1)
t.display
=> Thank you for coming!
2 SevendaySurfSportTour
4 Foo
1 Bar

Related

Failed to pass the value of an instance variable to another from within the same class (Ruby)

this is my code.
class Dog
attr_accessor :name
attr_reader :breed, :age
def initialize(name, breed, age)
#name = name
#breed = breed
#age = age
#distance_in_kms = []
end
def walk(distance_in_kms)
#distance_in_kms << {distance: distance_in_kms}
end
def walked_distance
#walked_distance = #distance_in_kms.inject(0) {|sum, hash| sum + hash[:distance]}
end
def display_walks
puts "#{#name} has had #{#distance_in_kms.length} walks and walked #{#walked_distance} kms today:"
#distance_in_kms.each do |each_walk|
puts "#{each_walk[:distance]} km"
end
end
#overriding to_s to print a meaningful representation of a class
def to_s
return "Dog: breed-#{#breed} name-#{#name}"
end
end
doggo = Dog.new("Roy", "Labrador", 8)
doggo.walk(3)
doggo.walk(5)
doggo.walk(1)
puts doggo.name = "Dang"
puts doggo.breed
doggo.display_walks
The result I have is this
Dang
Labrador
Dang has had 3 walks and walked kms today:
3 km
5 km
1 km
The expected value before kms should be 9, the sum of each walk in a day. Obviously, I have done something wrong with the walked_distance method. I was trying to pass the value of array #distance_in_kms into #walked_distance, but it didn't work, the return value was nil.But what can I do to fix the problem and get expected results? Thank you!!
You must call the method to update the #walked_distance, for example:
def display_walks
walked_distance # <------ This line
puts "#{#name} has had #{#distance_in_kms.length} walks and walked #{#walked_distance} kms today:"
#distance_in_kms.each do |each_walk|
puts "#{each_walk[:distance]} km"
end
end
Or change the walked_distance method and call it directly from the string interpolation:
def walked_distance
#distance_in_kms.inject(0) {|sum, hash| sum + hash[:distance]}
end
def display_walks
puts "#{#name} has had #{#distance_in_kms.length} walks and walked #{walked_distance} kms today:"
#distance_in_kms.each do |each_walk|
puts "#{each_walk[:distance]} km"
end
end
Off topic, just a small change to evaluate:
#walked_distance = #distance_in_kms.values.sum
puts "#{#name} has had #{#distance_in_kms.length} walks and walked #{#walked_distance} kms today:"
But you never call the code to calculate #walked_distance. Either update it when you add a new walk or calculate total distance here (by calling walked_distance, for example)
I could've done this right at the beginning too:
class Dog
attr_accessor :name
attr_reader :breed, :age
def initialize(name, breed, age)
#name = name
#breed = breed
#age = age
#distance_in_kms = []
#walked_distance = []
end
def walk(distance_in_kms)
#distance_in_kms << {distance: distance_in_kms}
#walked_distance = #distance_in_kms.inject(0) {|sum, hash| sum + hash[:distance]}
end
def display_walks
puts "#{#name} has had #{#distance_in_kms.length} walks and walked #{#walked_distance} kms today:"
#distance_in_kms.each do |each_walk|
puts "#{each_walk[:distance]} km"
end
end
#overriding to_s to print a meaningful representation of a class
def to_s
return "Dog: breed-#{#breed} name-#{#name}"
end
end
doggo = Dog.new("Roy", "Labrador", 8)
doggo.walk(3)
doggo.walk(5)
doggo.walk(1)
puts doggo.name = "Dang"
puts doggo.breed
doggo.display_walks
or use class methods to create a counter then try to link it to the instances(It's a very long way though)
Indeed, understanding variable scope is a real struggle for a beginner. Anyway, much appreciation for anyone who answered my question. Thank you!

How do I randomly select a name from an array and check if it's uppercase

I need to randomly pick a name from an array in Ruby and then check if it uppercase. So far I have:
def namegenerator
return #name.sample
end
def namechecker
if name.upcase then
check = TRUE
else
check = FALSE
end
end
It needs to be as two separate methods like this.
Something like this:
def sample_word(words)
words.sample
end
def upcase?(word)
word == word.upcase
end
And then something like:
words = %w[APPLE banana CherRy GRAPE]
word = sample_word(words)
puts word # e.g. BANANA
puts upcase?(word) # will print true
If you just want to check just the first letter:
names = %w(Kirk mccoy scott Spock)
names.sample.then { |name| [name, name[0] == name[0].upcase] }
#=> ["mccoy", false]
Maybe something like this:
class NameGenerator
def initialize(size, items)
#name = ""
#size = size
#items = items
end
def namegenerator
#name = #items.sample(#size).to_s
end
def namechecker?
#name == #name.upcase
end
def name
#name
end
end
ng = NameGenerator.new 1, ["name", "Name", "NAME"]
ng.namegenerator
puts ng.name, ng.namechecker?
Update
I've posted code without much thinking about abstraction and i think it would be much better to encapsulate name and upper case check to separate class and make it immutable, then make generator class that selects one entity from collection.
class NameGenerator
def initialize(items)
#items = items
end
def next
#items.sample
end
end
class Name
attr_reader :name
def initialize(name)
#name = name
end
def is_uppercase?
#name.match(/\p{Lower}/) == nil
end
end
ng = NameGenerator.new [
Name.new("name"),
Name.new("Name"),
Name.new("NAME"),
Name.new("na-me")
]
name = ng.next
puts name.name, name.is_uppercase?

Can we add items and quantity to a list through shovel approach and call that list outside a class?

Write a simple DSL for creating a shopping list. We should be able to specify the item name and quantity..
Something like.
sl = ShoppingList.new
sl.items do
add("Toothpaste",2)
add("Computer",1)
I am trying to add items and quantity both to a list, but facing trouble using shovel operation and also faced error to call this list outside the class. Can I list these items without using hash ?
class Array
def initialize
#list = []
#total = 0
end
def add(items, quantity)
if #list.include?(items) == false
#list << items
else #list.include?(items) == true
#list
end
#total.each {|x| quantity += x }
end
def items(&block)
#list.each(&block)
end
def total
#total
end
def display
#list
end
end
sl = Array.new
sl.items do
add('Toothpaste', 2)
add('Computer', 1)
add('Toothpaste', 3)
end
puts sl.list
puts sl.total
Expected Result :
s.list # => Should display list of items with quantity.
s.total # => Should display total of all quantities.
There was an attr_reader and attr_accessor missing in your code and the loop was not working (at least for me). With those attr_reader and attr_accessor you can get rid of at least two methods.
I wonder why you don't want to use a hash ..
Here's some working code, no hashes but an array of arrays, in my implementation counting the total numbers of items must be done at the end when all items are added.
class ShoppingList
attr_reader :list
attr_accessor :total
def initialize
#list = []
#total = 0
end
def add(items, quantity)
if #list.include?(items) == false
#list << [items, quantity]
else
#list
end
end
def total_number_of_items
quantities = list.map { |item| item[1] }
total = quantities.inject(0){|sum,x| sum + x }
end
end
sl = ShoppingList.new
sl.add('Toothpaste', 2)
sl.add('Computer', 1)
sl.add('Toothpaste', 3)
puts sl.list
puts sl.total_number_of_items
P.S. I renamed the method from Array to ShoppingList.
I know, this was not the question, but isn't that super-easy with a hash?
class ShoppingList
attr_reader :list
def initialize
#list = {}
end
def add(product_type, quantity)
if list[product_type].nil?
list[product_type] = quantity
else
list[product_type] += quantity
end
end
end
sl = ShoppingList.new
sl.add('Toothpaste', 2)
sl.add('Computer', 1)
sl.add('Toothpaste', 3)
pp sl.list.inspect
pp sl.list.keys.inspect
pp sl.list.values.inspect
Result:
"{\"Toothpaste\"=>5, \"Computer\"=>1}"
"[\"Toothpaste\", \"Computer\"]"
"[5, 1]"

How can I combine two blocks to simplify my code?

Hi I am a student learning Ruby. I am using the quick start guide at ruby-lang.org, which has some examples of Ruby basics.
I studied the MegaGreeter class, and I am trying to figure out how to puts two arguments (name and age) in the same each block in order to simplify my code.
I think there would be another way. (Using regular loops instead of each.)
Calculate the array's size.
Use a loop like in C.
But I want to use the each loop. Below is my code:
class MegaGreeter
attr_accessor :name
attr_accessor :age
#Creat the object
def initialize(name=nil, age=0)
#name = name
#age = age
#tmp = Array.new()
#i = 0
end
#Say hi to everybody
def say_hi
if #name.nil?
puts "please give me the input !!"
elsif #name.respond_to?("each")
#list responding
#name.each do |name|
#tmp[#i] = "hi ~! #{name}"
#i += 1
end
#i=0
#age.each do |age|
#tmp[#i] += " and you are #{age} years old"
puts #tmp[#i]
#i += 1
end
else
puts "give me array ~"
end
end
end
a = MegaGreeter.new()
a.name = ["juno","yoonhe"]
a.age = [1,2]
a.say_hi
You can use the Array method zip to first combine your two arrays. It groups the elements by their position in the array, so the first element of the #name array will be grouped with the first element of the #age array and so on.
#name = ['Foo', 'Bar']
#age = [23, 41]
name_and_age = #name.zip(#age)
# [['Foo', 23], ['Bar' 41]]
Now the names and ages are grouped together, and you can iterate over them using each.
name_and_age.each do |name, age|
puts name, age
end
# Foo 23
# Bar 41
Putting it back into your original code:
class MegaGreeter
attr_accessor :name, :age
#Creat the object
def initialize(name = nil, age = 0)
#name = name
#age = age
end
#Say hi to everybody
def say_hi
if #name.nil?
puts "please give me the input !!"
elsif #name.respond_to?("each")
#list responding
#name.zip(#age).each do |name, age|
puts "hi ~! #{name} and you are #{age} years old"
end
else
puts "give me array ~"
end
end
end

create an adjustable playlist with ruby

I'm not sure why my code will not pass the corresponding test. Each time I try the code, the following error is reported: "rb:60:in <main>': undefined local variable or methodtrack' for main:Object (NameError)." What can I do without editing the tests? Thanks! Open to another approach...thanks!
strong text
class Song
attr_reader :song
def initialize(song, artist)
#song = song
#artist = artist
end
def play
puts "#{#song}by #{#artist}"
end
end
class Playlist
def initialize(player_list)
#player_list = player_list
end
def add(add_song)
add_song.each do |song|
#player_list << song
end
end
def track_number
#player_list.length
end
def remove(remove_song)
remove_song.each do |song|
#player_list.delete(song)
end
end
def includes?(from_list)
i = 0
from_list.each do |song|
if #player_list.include?(song)
i+=1
end
end
if i==from_list.length
true
else
false
end
end
def play_all
#player_list.each do |song|
song.play
end
end
def display
#player_list.each do |song|
puts song.song
end
end
end
one_by_one = Song.new("One by One", "Sirenia")
world_so_cold = Song.new("World So Cold", "Three Days Grace")
going_under = Song.new("Going Under", "Evanescence")
my_playlist = Playlist.new(one_by_one, world_so_cold, going_under)
lying_from_you = Song.new("Lying From You", "Linkin Park")
angels = Song.new("Angels", "Within Temptation")
my_playlist.add(lying_from_you, angels)
p my_playlist.track_number == 5
going_under.play
my_playlist.remove(angels)
p my_playlist.includes?(lying_from_you) == true
my_playlist.play_all
my_playlist.display
When you call the "add" method for PlayList, it is expecting a single Song. However, the "add" method is trying to call .each() which would work for an array of Songs but not for a single song.
The best solution, without changing your test code, would be to remove the ".each" calls in the "add", "remove" and "includes?" methods for PlayList. Have them each add, remove or check for a single song at a time.

Resources