help with simple game

B

Blake Atl

My boss told me to pick up a ruby book and start figuring, so he gave me
this game, I have another assignment for you, to write a small game:

race.rb
construct an array of cars,
accept entry of two cars,
race the two cars,
determine and display a winner
example:

#each car is [name, horsepower, weight] cars = [
['mustang', 300, 3480 ],
['camaro', 400, 3780 ],
['s2000', 237, 2864 ],
['350z', 287, 3288 ]
]

run it giving two car names on the command line, example:
ruby race.rb mustang camaro
Use ARGV[0] as the name of the first car, and ARGV[1] as the name of
the second car, as follows: name1 = ARGV[0] || 'mustang'
name2 = ARGV[1] || 'camaro'

Locate both cars in the array, compare their performance numbers, and
pick a winner

So how on earth do i do this???? Please help!!
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

This sounds more like a homework problem, but I'll give you the benefit of
the doubt.

You can start with something like this:

CARS = {
'mustang' => [300.0, 3480],
'camaro' => [300.0, 3780]
}

car1,car2 = ARGV

def compare car1, car2
ratio1 = ratio car1
ratio2 = ratio car2

ratio1 >= ratio2 ? car1 : car2
end

def ratio car
CARS[car][0] / CARS[car][1]
end

puts compare car1, car2
 
B

Blake Atl

Andrew Wagner wrote in post #963161:
This sounds more like a homework problem, but I'll give you the benefit
of
the doubt.

You can start with something like this:

CARS = {
'mustang' => [300.0, 3480],
'camaro' => [300.0, 3780]
}

car1,car2 = ARGV

def compare car1, car2
ratio1 = ratio car1
ratio2 = ratio car2

ratio1 >= ratio2 ? car1 : car2
end

def ratio car
CARS[car][0] / CARS[car][1]
end

puts compare car1, car2

allrite allrite, how do i insert this in an interactive ruby window?
where do i go to build this code?

im actually interning at a communications company
 
T

Tim Chen

[Note: parts of this message were removed to make it a legal post.]

You can copy and paste ofcourse.

But if you're going to do anything serious with Ruby, you gotta start
creating a folder and have ruby files (*.rb).

Copy and paste the code into a ruby file and run it using the command line

% ruby car.rb

And ruby is a script language, which there is no compiling (build) needed.

The most common building is when you want to pack your code into a ruby gem,
and you'll most likely want to use rake (with a Rakefile).

Tim

Andrew Wagner wrote in post #963161:
This sounds more like a homework problem, but I'll give you the benefit
of
the doubt.

You can start with something like this:

CARS = {
'mustang' => [300.0, 3480],
'camaro' => [300.0, 3780]
}

car1,car2 = ARGV

def compare car1, car2
ratio1 = ratio car1
ratio2 = ratio car2

ratio1 >= ratio2 ? car1 : car2
end

def ratio car
CARS[car][0] / CARS[car][1]
end

puts compare car1, car2

allrite allrite, how do i insert this in an interactive ruby window?
where do i go to build this code?

im actually interning at a communications company
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

You can copy and paste ofcourse.

But if you're going to do anything serious with Ruby, you gotta start
creating a folder and have ruby files (*.rb).

Copy and paste the code into a ruby file and run it using the command line

% ruby car.rb

And ruby is a script language, which there is no compiling (build) needed.

The most common building is when you want to pack your code into a ruby
gem,
and you'll most likely want to use rake (with a Rakefile).

Tim
Well-said. For completeness, in this case it would be something like:
% ruby car.rb mustang camaro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top