Ruby/Class/getter setter
Материал из Wiki.crossplatform.ru
Accessors
# Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. # By supplying the attr method, Ruby provides a way to quickly create the equivalent of six lines of code with a single line. # The method attr creates a single getter method, named by a symbol, with an optional setter method (if the second argument is true) #!/usr/bin/env ruby class Dog attr :bark, true end Dog.instance_methods - Object.instance_methods # => ["bark", "bark="] dog = Dog.new dog.bark="Woof!" puts dog.bark # => Woof! # single line attr :bark, true is equivalent to writing out the bark and bark= methods in six lines of code: class Dog def bark @bark end def bark=(val) @bark = val end end
Add a method named get_color, which returns the color of animal objects created from this class:
class Animal def initialize @color = "red" end def get_color return @color end end
Call a member method
class Animal def initialize @color = "red" end def get_color return @color end end animal = Animal.new puts "The new animal is " + animal.get_color
Define getter for a attribute
#!/usr/bin/env ruby class Horse def initialize( name ) @name = name end def horse_name @name end end horse = Horse.new( "Doc Bar" ) puts horse.horse_name # => Doc Bar
getter and setter
# The setter method name= follows a Ruby convention: the name of the method ends with an equals sign (=). class Horse def name @name end def name=( value ) @name = value end end h = Horse.new h.name= "Poco Bueno" h.name # => "Poco Bueno"
Make an attribute readable and writable by using attr_reader, attr_writer
#!/usr/bin/env ruby class Dog attr_reader :bark attr_writer :bark end dog = Dog.new dog.bark="Woof!" puts dog.bark # => Woof! p dog.instance_variables.sort # => ["@bark"] p Dog.instance_methods.sort - Object.instance_methods # => [ "bark", "bark=" ]
Provide the getter
class CD include Comparable @@plays = 0 attr_reader :name, :artist, :duration attr_writer :duration def initialize(name, artist, duration) @name = name @artist = artist @duration = duration @plays = 0 end def to_s "CD: #@name--#@artist (#@duration)" end def name @name end def artist @artist end def duration @duration end end d = CD.new("B", "F", 260) d.artist d.name d.duration
Setter with =
class Song include Comparable @@plays = 0 attr_reader :name, :artist, :duration attr_writer :duration def initialize(name, artist, duration) @name = name @artist = artist @duration = duration @plays = 0 end def to_s "Song: #@name--#@artist (#@duration)" end def duration=(new_duration) @duration = new_duration end def inspect self.to_s end end song = Song.new("Bicylops", "Fleck", 260) song.duration song.duration = 257 # set attribute with updated value song.duration
Setter with calculation
class CD include Comparable @@plays = 0 attr_reader :name, :artist, :duration attr_writer :duration def initialize(name, artist, duration) @name = name @artist = artist @duration = duration @plays = 0 end def to_s "CD: #@name--#@artist (#@duration)" end def duration_in_minutes=(new_duration) @duration = (new_duration*60).to_i end def play @plays += 1 # same as @plays = @plays + 1 @@plays += 1 "This CD: #@plays plays. Total #@@plays plays." end def record "Recording..." end def inspect self.to_s end def <=>(other) self.duration <=> other.duration end end