Hi everyone, i'm really new to ruby and i'm trying to solve the next
piece of code:
require 'csv'
class Motorcycle
attr_reader :name, :weight
@@count = 0
def self.find (name)
found = nil
ObjectSpace.each_object(Motorcycle) { |o|
found = o if o.name == name
}
found
end
#Dynamically create instances of Motrocycle
def self.create
PARAMS = File.read('motorcycles.csv').split("\n").map { |line|
line.split(',') }
end
def initialize (name, weight)
@name = name
@weight = weight
self.class.count += 1
end
def self.count
@@count
end
def available_colors
end
def has_abs?
end
end
My code must be able to get trough this test:
#Test that must pass
describe Motorcycle do
describe "loading the motorcycle list" do
it "should load 2 motorcycles from the CSV" do
Motorcycle.count.should == 2
end
end
describe "finding a motorcycle by name" do
it "should return an instance of the Motorcycle class" do
Motorcycle.find("1200 RT").should be_a Motorcycle
end
end
describe "#weight" do
it "should have a weight of 800 pounds for the 1200 RT" do
Motorcycle.find("1200 RT").weight.should == '800 pounds'
end
it "should have a weight of 500 pounds for the 600 GS" do
Motorcycle.find("600 GS").weight.should == '500 pounds'
end
end
describe "#available colors" do
it "should find 'red' and 'black' as available colors for the BMW
1200 RT" do
Motorcycle.find("1200 RT").available_colors.should == [ 'red',
'black' ]
end
it "should find 'green' and 'blue' as available colors for the BMW
600 GS" do
Motorcycle.find("600 GS").available_colors.should == [ 'green',
'blue' ]
end
end
describe "#has_abs?" do
it "should be true for a motorcycle that appears in
abs_motorcycles.txt" do
Motorcycle.find("1200 RT").has_abs?.should be_true
end
it "should be false for a motorcycle that does not appear in
abs_motorcycles.txt" do
Motorcycle.find("600 GS").has_abs?.should be_false
end
end
end
Problem is i don't know much about ruby. i already got over the fact of
getting a count of how many instances of the class have been created,
also on how to find a instance of the class by its name, but don't know
how to create the instances of said class dynamically (via the create
method i'm trying to write), i know that the code line inside the create
method gives me an array that contains the information extracted from
the file "motrocycle.csv", first row contains the headers for each
column (which i'm not sure if its really useful), first column contains
the name that each instance of motorcycle should have, this is where my
big problem is located, not sure how to do that, so please, if anyone
could help me out i would really appreciate it :D.
Thanks in advance, its really urgent.
···
--
Posted via http://www.ruby-forum.com/.