Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6909185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:39:28+00:00 2026-05-27T08:39:28+00:00

Here is some code that i know works: require ‘csv’ class Motorcycle attr_reader :name,

  • 0

Here is some code that i know works:

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
    }
    return found
  end

  def self.create
    File.new('motorcycles.csv').readlines[1..-1].map{ |line|
        Motorcycle.new( *line.split( ',' )  )
    }
  end

  def initialize (name, weight)
    @name = name
    @weight = weight
    self.class.count += 1
  end

  def self.count
    return @@count
  end

  def self.count=( count ) 
    @@count = count 
  end

  def available_colors

    colors=[]
    colorsFile = File.read('colors.csv').split("\n").map { |line| line.split(',') }
    for i in (0..colorsFile.flatten.length) do
        if (colorsFile.flatten[i].to_s == self.name.to_s)
            colors.push(colorsFile.flatten[i+1])
        end
    end

    return colors
  end

  def contains (name,color)
    if(self.name.to_s == name)
    else
        return color
    end
  end

  def has_abs?
    File.open( 'abs.txt' ) do |io|
    io.each {|line| line.chomp! ; return true if line.include? self.name.to_s}
    end

    return false
  end

end

Motorcycle.create

the code must pass this tests on rspec:

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, after the first test (where it counts the amount of motrocicle instances) every instance is a nil, thats t say, every test is failed except for the fist one. here is the output log:

Failures:

  1) Motorcycle finding a motorcycle by name should return an instance of the Motorcycle class
     Failure/Error: Unable to find matching line from backtrace
       expected nil to be a kind of Motorcycle
     # ./motoapp.rb:76

  2) Motorcycle#weight should have a weight of 800 pounds for the 1200 RT
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `weight' for nil:NilClass
     # ./motoapp.rb:82

  3) Motorcycle#weight should have a weight of 500 pounds for the 600 GS
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `weight' for nil:NilClass
     # ./motoapp.rb:86

  4) Motorcycle#available colors should find 'red' and 'black' as available colors for the BMW 1200 RT
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `available_colors' for nil:NilClass
     # ./motoapp.rb:92

  5) Motorcycle#available colors should find 'green' and 'blue' as available colors for the BMW 600 GS
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `available_colors' for nil:NilClass
     # ./motoapp.rb:96

  6) Motorcycle#has_abs? should be true for a motorcycle that appears in abs_motorcycles.txt
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `has_abs?' for nil:NilClass
     # ./motoapp.rb:102

  7) Motorcycle#has_abs? should be false for a motorcycle that does not appear in abs_motorcycles.txt
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `has_abs?' for nil:NilClass
     # ./motoapp.rb:106

Finished in 0.01223 seconds
8     examples, 7 failures

i have been inclined to think this is some kind of bug or something due to my result doing the manual test like this:

puts Motorcycle.count
puts Motorcycle.find("1200 RT")
puts Motorcycle.find("1200 RT").weight
puts Motorcycle.find("600 GS").weight
puts Motorcycle.find("1200 RT").available_colors
puts Motorcycle.find("600 GS").available_colors
puts Motorcycle.find("1200 RT").has_abs?
puts Motorcycle.find("600 GS").has_abs?

which give me this output:

2
#<Motorcycle:0x7fd8bffcfd88>
800 pounds
500 pounds
red
black
green
blue
true
false

so i’m really pretty much on a dead end, ¿does anyone have a clue as to what could be happening?.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:39:28+00:00Added an answer on May 27, 2026 at 8:39 am

    it seems to me you are not storing the Motorcycle objects anywhere when doing the create call, so if the GC gets called the instances will just go away. Probably loading loads of more ruby code (rspec) causes the invocation of the GC to happen sooner.

    Why don’t you just store the instances in a class-level Set? (e.g. replacing the count call with instances << self) or Hash(storing the instances with their name as key)

    Also it seems to me you are relying on the rspec order of execution when you should have a before block in which you read from the CSV.

    Example code:

    Class Motorcycle
     # use a constant, this will always point to the same object even though
     # the content of the object changes. 
     # Using @@instances would also be ok
     Instances = Set.new # or you can use Instances = Hash.new
     def initialize (name, weight)
       @name = name
       @weight = weight
       # 
       Instances << self # or store the name/instance mapping Instances[name] = self
     end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some code here that works great on IPv4 machines, but on our
Here's some example code: class Obj attr :c, true def == that p '=='
I have some code that is using SyncEnumerator. As you can see here ,
So I have some PHP code that looks like: $message = 'Here is the
Given some JS code like that one here: for (var i = 0; i
Here's the situation: We have some generic graphics code that we use for one
We have some old C code here that's built with nmake. Is there an
I have a piece of code here that i really could use some help
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
Here is some code I could not get to format properly in markdown, this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.