Get user input x times and output all dog data sets - ruby

I am creating a program which creates a record for a Dog, that has the attributes (id, breed year born, and name). I have to use arrays to store the attributes of 3 dogs and then print them to the terminal.
require './input_functions'
class Dog
attr_accessor :id, :breed, :year_born, :name
def initialise (id, breed, year_born, name)
#id = id
#breed = breed
#year_born = year_born
#name = name
end
end
# Complete the missing code below
# Note: If your tasks requires outputting a floating point number you may wish to use the following:
# print out my_variable with 2 decimal places:
# printf("$%.2f\n", my_variable)
def read_dog()
puts "Enter the ID of your dog"
$dog_id = gets.to_s
puts "Enter the Breed of your dog"
$dog_breed = gets.to_s
puts "Enter the birth year of your dog"
$dog_year_born = gets.to_s
puts "Enter the name of your dog"
$dog_name = gets.to_s
end
def display_dog
puts $dog_id + "The dog ID is an Integer which is unique to your dog"
puts $dog_breed + "The dog breed is a String which defines the ancestors of your dog"
puts $dog_year_born + "The year born is an Integer which contains what year your dog was born"
puts $dog_name + "The dog name is a String which contains what your dog's name is"
end
def read_dogs
dogs = Array.new
index = 0
while (index < 3)
dogs << read_dog[index]
index = index + 1
end
return dogs
end
def display_dogs(dogs)
index = 0
while (index < 3)
dogs[display_dog.to_i]
index = index + 1
end
end
def main
dogs = read_dogs
display_dogs(dogs)
end
main
The expected result is that the program displays all entered information back to the user. What happens instead is that only the last set of data that is entered is shown three times. It obviously has something to do with how I am storing or extracting the data from the array but I can't figure out what that is.

First of all you must return some value from read_dog method
def read_dog()
puts "Enter the ID of your dog"
$dog_id = gets.to_s
puts "Enter the Breed of your dog"
$dog_breed = gets.to_s
puts "Enter the birth year of your dog"
$dog_year_born = gets.to_s
puts "Enter the name of your dog"
$dog_name = gets.to_s
return [$dog_id, $dog_breed, $dog_year_born, $dog_name]
end
Let me update you on other things as it only printing last information
Btw I am not seeing any use of Dog class code in your snippet at the moment

class Dog
attr_accessor :id, :breed, :year_born, :name
def self.ask_for_dogs(n_times)
all_dogs = []
n_times.times do
all_dogs << new
end
all_dogs
end
def self.display(dogs)
dogs.each do |dog|
puts "Id: #{dog.id}"
puts "Name: #{dog.name}"
puts "Breed: #{dog.breed}"
puts "Year born: #{dog.year_born}\n"
end
end
private
def initialize
puts "Enter the dog's ID:"
#id = gets.to_s
puts "Enter the dog's breed:"
#breed = gets.to_s
puts "Enter the dog's birth year:"
#year_born = gets.to_s
puts "Enter the dog's name:"
#name = gets.to_s
end
end
Run it, by defining the number of dogs you want to create.
In the loop dog objects are created, which are then printed out by the diplay method all at once, by accessing the attributes of the dog object.
dogs = Dog.ask_for_dogs(3)
Dog.display(dogs)

Related

Ruby labeling printed outputs

I'm writing a short program that asks a user to enter a car model, maker, and year input and it passes that input through an algorithm. My question is, is there a way to label multiple printed outputs after it has been put through the formula to where it will number each output? Would I have to use a for each loop? I'm just trying to get a general idea of how I would accomplish this.
say for example the printed output would look like this.
class Car
attr_reader :make, :model, :year
def initialize
end
def set_make(make)
#make = make
end
def set_model(model)
#model = model
end
def set_year(year)
#year = year
end
def get_make
#make
end
def get_year
#year
end
def get_model
#model
end
end
array_of_cars = Array.new
print "How many cars do you want to create? "
num_cars = gets.to_i
for i in 1..num_cars
puts
print "Enter make for car #{i}: "
make = gets.chomp
print "Enter model for car #{i}: "
model = gets.chomp
print "Enter year of car #{i}: "
year = gets.to_i
c = Car.new
c.set_make(make)
c.set_model(model)
c.set_year(year)
array_of_cars << c
end
puts
puts "You have the following cars: "
puts
for car in array_of_cars
puts "#{car.get_year} #{car.get_make} #{car.get_model}"
end
puts
2014 Ford Expedition
2017 Toyota 86
2017 Aston Martin DB11
is there a way to add those numbers to the output?
Instead using a for loop you could try using each_with_index, which will allow you to get each element inside the array_of_cars and also the index for each element, in this case adding 1 to the current index will give you the value starting from 1:
array_of_cars.each_with_index do |car, index|
puts "#{index + 1}. #{car.get_year} #{car.get_make} #{car.get_model}"
end
Or you can use each and with_index passing the first element, in this case 1 as argument:
array_of_cars.each.with_index(1) do |car, index|
puts "#{index}. #{car.get_year} #{car.get_make} #{car.get_model}"
end
You don't need so many methods. Use attr_accessor to set getters and setters and utilize initialize better. Then using the basic idea from this answer by tadman, we can collect newly made objects into an array within the class itself. All together we can compress your class to:
class Car
attr_accessor :make, :model, :year
def self.all
#all ||= []
end
def initialize(make, model, year)
#make = make
#model = model
#year = year
Car.all << self
end
end
We can use times to run a piece of code n times.
puts "How many cars do you want to create? "
n = gets.to_i
n.times.with_index(1) { |_,i|
puts "Enter make for car #{i}"
make = gets.chomp
puts "Enter model for car #{i}: "
model = gets.chomp
puts "Enter year of car #{i}: "
year = gets.to_i
puts
Car.new(make, model, year)
}
Then as Sebastián Palma has already suggested, use each.with_index(1) to print your cars. The argument offsets the index by 1.
Car.all.each.with_index(1) { |c, i| puts "#{i}. #{c.year} #{c.make} #{c.make}" }
Sidenotes: 1. Avoid using for loops in Ruby 2. Use puts not print.

Need a bit of help defining some methods

I would really appreciate it if I could get some help on some questions regarding an assignment that prints out the data typed in by a user(In this specific example, the year, model, and make of the car):
# DEFINE YOUR CAR CLASS HERE
# create empty array
array_of_cars = Array.new
# prompt for and get number of cars
print "How many cars do you want to create? "
num_cars = gets.to_i
# create that many cars
for i in 1..num_cars
# get the make, model, and year
puts
print "Enter make for car #{i}: "
make = gets.chomp
print "Enter model for car #{i}: "
model = gets.chomp
print "Enter year of car #{i}: "
year = gets.to_i
# create a new Car object
c = Car.new
# set the make, model, and year
# that we just read in
c.set_make(make)
c.set_model(model)
c.set_year(year)
# add the Car object to the array
array_of_cars << c
end
# print out the information for
# the Car objects in the array
puts
puts "You have the following cars: "
for car in array_of_cars
puts "#{car.get_year} #{car.get_make} #{car.get_model}"
end
I already have some part of the program, but struggle with the main portion of it, since I kind of know what to do but not how to implement it.
So for this part: # DEFINE YOUR CAR CLASS HERE
I got this:
class Car
def assign(m,n,y)
#instance variables
#make = m
#model = n
#year = y
end
#instance methods
def set_make
end
def set_model
end
def set_year
end
def get_make
end
def get_model
end
def get_year
end
First, did I do it right with the instance variables?
And then, the purpose of "set" is to save the value into the array right? and then "get" allows me to extract them later. I think I would understand the concept if someone could show me how to define one of them.
I know that this seems a bit vague, so I will try my best to clarify if some questions occur. Also sorry for the wall of text and thank you!
In the first place, in “idiomatic ruby” we call getters and setters for #variable as variable (getter) and variable= (setter.) The constructor is to be named initialize, not assign.
There is a helper to define both for the class, Module#attr_accessor that declares both getter and setter under the hood, so that your class definition might be as short as:
class Car
attr_accessor :make, :model, :year
def initialize(make, model, year)
#make = make
#model = model
#year = year
end
end
So far so good. The rest of your code would be:
array_of_cars = [] # use Array.new with parameter, [] otherwise
# prompt for and get number of cars
print "How many cars do you want to create? "
num_cars = gets.to_i
# create that many cars
(1..num_cars).each do |i| # in idiomatic ruby, never use for loops
# get the make, model, and year
puts "Enter make for car #{i}: "
make = gets.chomp
print "Enter model for car #{i}: "
model = gets.chomp
print "Enter year of car #{i}: "
year = gets.to_i
# create a new Car object
c = Car.new(make, model, year)
# add the Car object to the array
array_of_cars << c
end
# print out the information for
# the Car objects in the array
puts
puts "You have the following cars: "
array_of_cars.each do |car|
puts "#{car.year} #{car.make} #{car.model}"
end
BTW, instead of pre-creating an array, one might better use Enumerable#map:
# prompt for and get number of cars
print "How many cars do you want to create? "
num_cars = gets.to_i
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ DECLARE IT IN-PLACE
array_of_cars = (1..num_cars).map do |i|
puts "Enter make for car #{i}: "
make = gets.chomp
print "Enter model for car #{i}: "
model = gets.chomp
print "Enter year of car #{i}: "
year = gets.to_i
# create a new Car object
Car.new(make, model, year)
end
That would produce an array needed out of the box.
Addendum: to define getter and setter manually:
class Car
attr_accessor :make, :model
def initialize(make, model, year)
#make = make
#model = model
#year = year
end
# manually declare getter for year:
def year
#year
end
# manually declare setter for year:
def year=(year)
#year = year
end
end

Ruby - Making my code object orientated

I'm just starting out with OOP and am struggling to work out how to implement it. I can't work out how to create an instance of the class using the hash.new method. It's a basic program that takes input from a user and stores it in a hash. I was then planning on pushing the hash into an array to take advantage of the index. Below is the code
class Customer
def initialize(id, name, address, phone, email, bank_balance)
#id = id
#name = name
#address = address
#phone = phone
#email = email
#bank_balance = bank_balance
end
end
puts "Would you like to create an account?"
answer = gets.chomp
loop do
new_hash = {}
if answer.downcase.start_with?('y')
puts "Enter your name"
name = gets.chomp.downcase
new_hash['name'] = name
puts "Enter your address"
address = gets.chomp.downcase
new_hash['address'] = address
puts "Enter your ph number"
number = gets.chomp
number = number.split.join
new_hash['number'] = number
puts "Enter your email"
email = gets.chomp.downcase
new_hash['email'] = email
puts "Enter your bank balance"
bank_balance = gets.chomp
bank_balance = "$" + '%.2f' % bank_balance
new_hash['bank_balance'] = bank_balance
customer << new_hash
binding.pry
puts "Thankyou, details successfully Added"
break
else
break
end
end
you should remove everything inside initialize(...) and instead use
attr_accessor :name, :address, :phone, :email, :bank_balance) write it above your initialize statement
for more information on attr_accessor see this post What is attr_accessor in Ruby?
also follow it up by reading up on attr_reader and attr_writer
you can then assign values to those initialized variables in the rest of your code.
you will call your class by running Customer.new
you can get a little more snap out of your code by assigning your gets.chomp values to additional methods, then you can do something like
class Customer
attr_accessor :name, :address, :phone
def initialize
#name = ''
#address = ''
#phone = ''
end
def name
puts "Enter your name"
name = gets.chomp.downcase
end
def address
puts "Enter your address"
address = gets.chomp.downcase
end
def phone
puts 'enter your phone'
phone = gets.chomp
phone = phone.split.join
end
def all
name
address
phone
end
end
customer = Customer.new
customer.all

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

Creating Multiple Objects in Ruby and then Comparing Them

My goal is to be able to assign each tea their own ID, compare prices and weights between teas, and do it all within the command line. What's a smart way to do this? Here is my code so far:
class Tea
def initialize(name, price, shipping, weight)
#name = name
#price = price
#shipping = shipping
#weight = weight
get_tea_details
#total_price = total_price
end
def get_tea_details
puts "Enter name: "
#name = gets.chomp
puts "Enter price: "
#price = gets.chomp.to_f
puts "Enter shipping cost: "
#shipping = gets.chomp.to_f
puts "Enter weight: "
#weight = gets.chomp.to_i
end
def total_price
#total_price = #price + #shipping
end
def price_difference
price_difference = t1.total_price - t2.total_price
print "#{price_difference}"
end
end
puts "Do you want to compare teas?: "
answer = gets.chomp
if answer == "yes"
t1 = Tea.new(#name, #price, #shipping, #weight)
t1 = Tea.new(#name, #price, #shipping, #weight)
end
price_difference
I'm not exactly sure what you're asking, but my guess is you want to know how to write a function to compare your Tea objects. You can do something like this:
class Tea
attr_accessor :name, :price
def price_difference(other)
print (#price - other.price).abs
end
def compare(other)
same = true
if(#name != other.name)
puts "They have different names."
same = false
end
if(#price != other.price)
puts "They have different prices."
same = false
end
if same
puts "They are exactly the same!"
end
end
end
t1 = Tea.new
t2 = Tea.new
t1.compare t2
"They are exactly the same!"
I'd also recommend dropping the "tea_" prefix from your variables. It's unnecessary and adds a little readability.

Resources