Ruby/Class/Class Defintion

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

A class is defined with the class keyword, followed by an end.

# The class identifier is a constant, so it must be capitalized (the norm)
class Hello
  def initialize( name )
    @name = name
  end
  def hello_myValue
    puts "Hello, " + @name + "!"
  end
end
hi = Hello.new( "myValue" )
hi.hello_myValue



A class is itself an object.

class Array
  def array_of_ten
    (1..10).to_a
  end
end
arr = Array.new
ten = arr.array_of_ten
p ten



A simple class that"s been documented using comments.

# This class stores information about people.
class Person
  attr_accessor :name, :age, :gender
  # Create the person object and store their name
  def initialize(name)
    @name = name
  end
  # Print this person"s name to the screen
  def print_name
    puts "Person called #{@name}"
  end
end
# RDoc can turn it into a pretty set of HTML documentation in seconds.
# rdoc person.rb



class template

class Person
  def anyone_can_access_this
    # ...
  end
  private
  def this_is_private
    # ...
  end
  public
  def another_public_method
    # ...
  end
end



Class variable as condition variable

class House
  @@current_temp = 70
  attr_accessor :atomic_number
  def House.current_temp=(x)
    @@current_temp = x
  end
  def House.current_temp
    @@current_temp
  end
  def hot?
    @@current_temp >= @melting
  end
  def initialize(atnum, melt)
    @atomic_number = atnum
    @melting = melt
  end
end
 
app = House.new(13, 1236)
app2 = House.new(29, 1982)
app3 = House.new(79, 1948)
House.current_temp = 1600
puts app.hot?        # true
puts app2.hot?          # false
puts app3.hot?            # false
House.current_temp = 2100
puts app.hot?        # true
puts app2.hot?          # true
puts app3.hot?            # true



Class with symboles

class PersonalComputer
  attr_accessor :manufacturer,
    :model, :processor, :clock,
    :ram, :disk, :monitor,
    :colors, :vres, :hres, :net
  def initialize(&block)
    instance_eval █
  end
  # Other methods...
end
desktop = PersonalComputer.new do
  self.manufacturer = "Acme"
  self.model = "THX-1138"
  self.processor = "986"
  self.clock = 2.4        # GHz
  self.ram = 1024         # Mb
  self.disk = 800         # Gb
  self.monitor = 25       # inches
  self.colors = 16777216
  self.vres = 1280
  self.hres = 1600
  self.net = "T3"
end
p desktop



names of classes in Ruby start with a capital letter

class Animal
end



To create a class in Ruby, you simply use the class statement.

class Animal
end



To embed a module in a class, you use the include statement in the class: include modulename

module Week
     First_day = "Sunday"
     def Week.weeks_in_month
            puts "You have four weeks in a month"
     end
     def Week.weeks_in_year
            puts "You have 52 weeks in a year"
     end
end
class Decade
include Week
     no_of_yrs=10
     def no_of_months
            puts Week::First_day
            number=10*12
            puts number
     end
end
d1=Decade.new
puts Week::First_day
Week.weeks_in_month
Week.weeks_in_year
d1.no_of_months



Your class with constructor, to string, setter, getter and inspect

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
  def duration=(new_duration)
    @duration = new_duration
  end
  def duration_in_minutes
    @duration/60.0   # force floating point
  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



Your own class with inspect method

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
  def duration=(new_duration)
    @duration = new_duration
  end
  def duration_in_minutes
    @duration/60.0   # force floating point
  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
  def initialize(name, artist, duration)
    @name     = name
    @artist   = artist
    @duration = duration
  end
end
 
d = CD.new("A", "B", 260)
d.inspect  #!to_s!