|
[edit] Ruby Classes
From what you've learned so far, you've probably come to realize that in Ruby everything is an object.All objects belong to a certain "type", a class in fact.
For example, let's take a number, any number, like 534 for example.This belongs to the class called Fixnum.How do you verify this?
Run this command in the irb:
puts 534.class
This is the output:
[edit] How to create a new class in Ruby
class MyClass
end
This is empty for starters.Also, notice how the class name needs to start with a capital letter.
Here's a more advanced example:
class Displaying
def initialize(msg)
@message = msg
end
def showmessage
return @message
end
end
display = Displaying.new("this is a test message")
puts display.showmessage
Here is the result:
|