codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
# Examples of defining a CLASS, instatiating objects based on that class and doing something useful # Run this from a CMD/Shell window, you need Ruby installed, navigate to the folder this file is in and type: ruby classStudy.rb #define the class class Boat attr_accessor :name, :colour, :type, :prop def initialize(name, colour, type, prop) @name = name @colour = colour @type = type @prop = prop #as in Propulsion end def catalogue puts "#{@name} is a #{@colour}, #{@type} with #{@prop}" puts "The current sale price is #{163.chr}#{rand(10)*2},000 - cash only please." # hmm... the 163 for a £ symbol isn't working... end end #instantiate the class a few times to get the instances that make up the catalogue sailboat = Boat.new("Ely II", "Red", "Long Hull", "Sail") hybrid = Boat.new("Spirit Song", "Blue", "Wide Hull", "Sail and Engine") powerboat = Boat.new("Bluster","Yellow","Sleek Hull","Engine") #Ask what boat is wanted puts "What boat is wanted? (Sailboat, Hybrid or Powerboat)" boatwanted = gets.chomp.downcase #Put to screen depending on which boat type was wanted case boatwanted when "sailboat" sailboat.catalogue # run the catalogue method against the Boat instance wanted when "hybrid" hybrid.catalogue when "powerboat" powerboat.catalogue end
Private
[
?
]
Run code
Submit