|
Allow me to explain now what the code does:
The class is created with the name "Displaying".
The initialize method is the constructor, you use it to create new objects and configure them.You must have a constructor for a class to function properly.The constructor is given a parameter or an argument, "msg" in our case.The "msg" will be in fact stored in a variable called "message".
For the purpose of our new class that is supposed to return "something" we created a method that will return us the message, method named "showmessage".
We've then created a new class object using the "new" method on the class Displaying and assigned this to the variable called "display", not before passing the message itself ("this is a new message") to the class constructor.
To print the message on screen, we've used a "puts" instruction along with using the "showmessage" method on the "display" variable we've recently created.
The variable prefixed with a "@" sign is an instance variable.A class variable should begin with double "@@" signs.The benefit in using class variables is that of them being shared by all instances of the class.If modified inside an object, the new value would be the same for the rest of the objects.When you modify an instance variable from an object, it's new value is only available to the same object.
[edit] More advanced:
The "msg" variable is called an instance variable.The "showmessage" is an "accessor", this is how methods that are used for accesing values inside an object.
Classes have basic functioning principles in all object-oriented programming languages.Please reffer to this Wikipedia article for more general info.
In consequence, as in other programming languages, "objects" inside a class can have 3 properties : they can either be readable, writable, or readable/writable.
Readable only means that the certain object can only be read.
Writable means that the objects can be modified.
Readable/writeable is similar to writeable.
Lets learn more about these attributes in Ruby:
In our example, "showmessage" from display.message is an attribute, a readable attribute since the value of the @message variable if you use it.
Another way of writing the code on the previous page(this time explicitly setting the message variable as readable):
class Displaying
attr_reader :message
def initialize(msg)
@message = msg
end
def showmessage
return @message
end
end
display = Displaying.new("this is a test message")
puts display.showmessage
[edit] How to create Ruby writable attributes
Let's imagine that you're creating a certain application and you want to give the user the ability to change font sizes.
class Font
attr_reader :size
def size=(size)
@size = size
end
def initialize(size)
@size = size
end
end
font = Font.new(“small”)
puts “The new font size is “ + font.size
font.size = “medium”
puts “The updated font size is “ + font.size
What you can notice from the code above is that the way to make writable attributes is by placing a "=" sign after the accessor method (discussed above), in our case, the "size" accessor method.
[edit] Another way of creating Ruby writable attribute
class Font
attr_reader :size
attr_writer :size
def initialize(size)
@size = size
end
end
font = Font.new(“small”)
puts “The new font size is “ + font.size
font.size = “medium”
puts “The updated font size is “ + font.size
|