. View this ad or post your own Ruby Job!

Friday, November 11, 2005

Writing One's Own Class in Ruby

What have I found out so far?
  • Definition of Ruby
  • Ruby Windows Installer
  • Interactive Ruby - Open a new DOS window and type irb --simple-prompt A prompt appears as >>
  • Run a Ruby program by typing ruby hello.rb
  • Learnt some features of Ruby like comments, naming rules, variables, constants, strings etc.
  • Various Text Editors/IDEs like SciTE, NotePad2
  • Constructs like the if else and while
  • Some methods in Ruby.
  • Arrays
  • Writing one's own methods


Today, I am going to have a quick look at Writing one's own Class in Ruby.

Let us look at a simple program.
class MotorCycle
# Every class must contain this method
def initialize(make, color)
# Instance variables
@make = make
@color = color
end
def startEngine
if (@engineState)
puts 'Engine Running'
else
@engineState = true
puts 'Engine Idle'
end
end
def dispAttr
puts 'Color of MotorCycle is ' + @color
puts 'Make of MotorCycle is ' + @make
end
m = MotorCycle.new('Yamaha', 'red')
m.startEngine
m.dispAttr
m.startEngine
end


First Post | Previous | Next



Technorati Tags:
Blogs linking to this article

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home

Valid XHTML 1.0!