Ruby(2) - Variables and Class
There are four kinds of variables in Ruby, and varialbes are not required to be declared before use.
| Type | Name Convention | Example | Comments |
| local variable | starting with a lowercase letter. | name | like variables in function |
| instance variable | preceded by an ``at'' sign (``@''). | @name | like variables in Java Class |
| class variable | preceded by two ``at'' sign (``@''). | @@name | like static variables in Java Class |
| constant | start with an uppercase letter. | Name |
2.2 Class
2.2.1 New a class instance
In Ruby you should create a instance of a class by calling the class's new method. The new method will invoke the matched initialize method in the class according the parameters you provided to the new method.
#-------------Class Example----------------
class Song
def initialize(name, artist, duration) # Constructor
@name = name
@artist = artist
@duration = duration
end
@@plays = 0 # Class variable
def initialize(name, artist, duration, plays) # Constructor
@name = name
@artist = artist
@duration = duration
@plays = 0
end
def play # Instance Method
@plays += 1
@@plays += 1
"This song: #@plays plays. Total #@@plays plays." # Last
end
def Song.setPlays(times) # Class Method
@@plays = times
end
end
2.2.2 Attributes
Attribute can be defined in two ways:
1. use attr_reader and attr_writer.
2. define method
#------------------Attribute Example--------------------------------------
class Song
#attr_reader :name, :artist, :duration # Attribute name readonly
def name # Attribute name readonly
@name
end
def artist # Attribute artist readonly
@artist
end
def duration # Attribute duration readonly
@duration
end
#attr_writer :duration # Attribute duration writable
def duration=(newDuration)
@duration = newDuration
end
end
Virtual attribute : a kind of attribute not associated with an instance variable directly
class Song
def durationInMinutes
@duration/60.0 # force floating point
end
def durationInMinutes=(value)
@duration = (value*60).to_i
end
end
2.2.3 Access Control
The only way to change an object's state in Ruby is by calling one of its methods., no class variable can be accessed directly.
Three access level:
- Public methods can be called by anyone---there is no access control. Methods are public by default (except for
initialize, which is always private). - Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.
- Private methods cannot be called with an explicit receiver. Because you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendents within that same object.
#-------------------------Specifying Access Control------------------------------
class MyClass
def method1 # default is 'public'
#...
end
protected # subsequent methods will be 'protected'
def method2 # will be 'protected'
#...
end
private # subsequent methods will be 'private'
def method3 # will be 'private'
#...
end
public # subsequent methods will be 'public'
def method4 # and this will be 'public'
#...
end
end
#---------------------Another way-----------------------------
class MyClass
def method1
end
# ... and so on
public :method1, :method4
protected :method2
private :method3
end
浙公网安备 33010602011771号