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.
Related
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
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
I have created an array of objects containing information on the Oscars using a text file with all the category names, winners and nominees (winners appear in nominees list as well). I now want to be able to ask a user. Of which category would you like to know the winner? Once the question is asked it would return the answer. I can only get it to work on the last object of the array(best visual effects returns gravity). Can someone explain why this is happening?
class AwardCategory
attr_accessor :winner, :name, :nominees
def initialize(name)
#name = name
#nominees = []
end
end
class Nominee
attr_accessor :name
def initialize(name)
#name = name
end
end
file = File.open('oscar_noms.txt', 'r')
oscars = []
begin
while true do
award_category = AwardCategory.new(file.readline.downcase)
award_category.winner = file.readline.downcase
nominee = Nominee.new(file.readline.downcase)
award_category.nominees << nominee
next_nominee = Nominee.new(file.readline.downcase)
until next_nominee.name == "\n"
award_category.nominees << next_nominee
next_nominee = Nominee.new(file.readline.downcase)
end
oscars << award_category
end
rescue EOFError => e
puts 'rescued'
end
#puts oscars.inspect
#Read input here
puts "What category do you want to know the winner for?"
answer = gets
oscars.each
if answer.downcase == award_category.name
puts award_category.winner
else
puts "That is not a category"
end
That piece of code
puts "What category do you want to know the winner for?"
answer = gets
oscars.each
if answer.downcase == award_category.name
puts award_category.winner
else
puts "That is not a category"
end
Now with correct indentation
puts "What category do you want to know the winner for?"
answer = gets
oscars.each
if answer.downcase == award_category.name
puts award_category.winner
else
puts "That is not a category"
end
Note that the part below oscars.each is not indented, because each needs a do/end block which it will execute once for every element. What you probably want is this
puts "What category do you want to know the winner for?"
answer = gets
oscars.each do |award_category|
if answer.downcase == award_category.name
puts award_category.winner
else
puts "That is not a category"
end
end
Although I suggest you leave off the else, because you will get the message "That is not a category" for every answer that did not match. Also, you should use gets.chomp to remove the newline from the user input and do downcase outside of the each loop. As a last note, some of your variables are poorly named. For example, why should a list of award categories be named oscars? It should be award_categories instead.
I would like users to be able to dynamically create objects of the Incomes class below. That is, I would like to fire my program and let users enter as many incomes as they like, all stored as instances of the Incomes class.
def prompt
puts "> "
end
class Incomes
def initialize(aName, aAmount, aCOLA)
#name = aName
#amount = aAmount
#COLA = aCOLA
end
end
def addIncome
puts "What is the company name?"
prompt
aName = gets.chomp
puts "What is the monthly amount?"
aAmount = gets.chomp
puts "What is the cost of living adjustment?"
aCOLA = gets.chomp
end
#Now I want to be able to loop back through addIncome and create as many objects as the
#user wants. Perhaps there's a better way to store this type of data?
def prompt question
print "#{question} > "
gets
end
class Incomes
attr_reader :name, :amount, :COLA
##instances_of_Incomes = Array.new
def initialize(aName, aAmount, aCOLA)
#name = aName
#amount = aAmount
#COLA = aCOLA
#instances_of_Incomes = Array.new
end
def self.addIncome
name = prompt "What is the company name?"
amount = prompt "What is the monthly amount?"
_COLA = prompt "What is the cost of living adjustment?"
##instances_of_Incomes << Incomes.new(name, amount, _COLA)
end
def self.instances
##instances_of_Incomes
end
end
5.times do
Incomes.addIncome
end
puts Incomes.instances
Incomes.instances.each do |company|
puts company.name
end
I have refactored the code to show that you can use inputs to create the instances. They are unnamed classes, but stored in a class variable.
I also show that you can extract the name of each Incomes instance.
I have also edited your SE Code Review question, with the same code, so hopefully you can get some good reviews.
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.