SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simply Rails 2: Chapter 3 - Running Ruby files

    Hey everyone,

    Just started reading the book and enjoying it so far, but I do need some help.
    I have just gotten the car-classes.rb file (shown below), and would like to run it in irb so I can create Car objects to follow along in the book.
    This is what I've done to try and achieve this:
    • Went to Ruby Console Window in Instant Rails
    • Navigated to the directory containing the .rb file
    • typed in "ruby" followed by the filename
    • typed in irb
    • Tried to instantiate a new Car object
    • Error!


    Any chance someone can lend me a helping hand?



    Code:
    class Car
      WHEELS = 4                # class constant
      @@number_of_cars = 0      # class variable
        
      def initialize
        @@number_of_cars = @@number_of_cars + 1
      end
      
      def self.count
        @@number_of_cars
      end
      
      def mileage=(x)           # instance variable writer
        @mileage = x            
      end
      
      def mileage               # instance variable reader
        @mileage
      end
    end
    
    class StretchLimo < Car
      WHEELS = 6                # class constant
      @@televisions = 1         # class variable
      
      def turn_on_television    
        # Invoke code for switching on on-board TV here
      end
    end
    
    class PontiacFirebird < Car
    end
    
    class VolksWagen < Car
    end

  2. #2
    SitePoint Zealot atetlaw's Avatar
    Join Date
    Feb 2007
    Location
    Melbourne, Australia
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey zeenko,

    What's the error you are seeing?

    Also, if you could paste the contents of the command prompt window, that'd help too.

  3. #3
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey atetlaw,

    Thanks for taking the time to read and respond to my comment. I was actually able to figure out my problem. To those who are experiencing the same problem where you can't seem to create an object in irb by using a class file that you saved, try to do the following:
    • Go to the Ruby Constole Window in Instant Rails
    • Navigate to the directory containing the class .rb file
    • Type in: irb
    • Type in: require 'nameofyourclassfile'
    • Console should return: => true
    • Now you can create objects using your class file!

  4. #4
    SitePoint Zealot atetlaw's Avatar
    Join Date
    Feb 2007
    Location
    Melbourne, Australia
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah cool, thanks for posting the solution too.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •