class Vector
def initialize(x, y, z)
@x, @y, @z = x, y, z
end
def length
(@x * @x + @y * @y + @z * @z) ** 0.5
end
def to_s() “(#@x, #@y, #@z)” end
end
orientation = Vector.new 1.0, 0.0, 1.0
puts orientation.length
@name, @age
class Vector
# ...
def x() @x end
def y() @y end
def z() @z end
end
v = Vector.new 1.0, 0.0, 1.0
puts v.x + v.y + v.z()
name=
class Thing
def name=(value)
puts “Name is set to #{value}”
end
end
>> bacon = Thing.new
>> bacon.name = “Bacon”
Name is set to Bacon
>> bacon.name=(“Chunky bacon”)
Name is set to Chunky bacon
class Vector
# ...
def x=(value)
@x = value
end
def y=(value)
@y = value
end
def z=(value)
@z = value
end
end
class Vector
attr_accessor :x, :y, :z
def initialize(x, y, z)
@x, @y, @z = x, y, z
end
end
attr_accessor
attr_accessor :name
def name(value)
@name
end
def name=(value)
@name = value
end
attr_reader
attr_writer
Meyer’s Uniform
Access Principle
class Person
attr_reader :first, :last
def initialize(first, last)
@first, @last = first, last
end
end
class Person
attr_reader :first, :last
def initialize(first, last)
@first, @last = first, last
end
def business_card
“#@first #@last”
end
end
class Person
attr_reader :first, :last
def initialize(first, last)
@first, @last = first, last
end
def business_card
“#@first #@last”
end
def solve_problem(problem)
until problem.solved? or self.bored?
stare_at problem
try_something_random
end
end
end
class Programmer < Person
end
class Programmer < Person
def solve_problem(problem)
thoughts = self.think_about problem
solution = self.invent_solution thoughts
self.write_tests_about solution
self.implement solution
self.party!
end
end
class SmartGuy < Person
def business_card
super() + “, Ph.D.”
end
end
Няма множествено
наследяване
Статични методи
Методи на класа
Променливи на класа
class Person
def initialize(first, last)
@first, @last = first, last
@@count ||= 0
@@count += 1
end
end
@@count ||= 12
@@count = @@count || 12
class Person
def initialize(first, last)
@first, @last = first, last
@@count ||= 0
@@count += 1
end
def Person.count
@@count
end
end
>> Person.new “Thelonious”, “Monk”
>> Person.new “Glenn”, “Gould”
>> puts Person.count
2
class Person
def self.count
@@count
end
end
Оператори
class Vector
attr_accessor :x, :y, :z
def initialize(x, y, z)
@x, @y, @z = x, y, z
end
def length
(@x * @x + @y * @y + @z * @z) ** 0.5
end
def to_s() “(#@x, #@y, #@z)” end
end
c = a + b
d = c * 7
class Vector
def +(other)
Vector.new self.x + other.x,
self.y + other.y,
self.z + other.z
end
def *(n)
Vector.new @x * n, @y * n, @z * n
end
end
>> a = Vector.new 1.0, 0.0, 0.0
>> b = Vector.new 0.0, 1.0, 0.0
>> puts a + b
(1.0, 1.0, 0.0)
>> puts a + b * 2
(1.0, 2.0, 0.0)
class Fixnum
alias broken_equal? ==
def ==(other)
if (self.equal?(0) and other.equal?(1)) or
(self.equal?(1) and other.equal?(0))
true
else
self.broken_equal?(other)
end
end
end
pesho = Coin.new 0.50
def pesho.pick_up(person)
person.get_hopes_high
person.humiliate
person.make_sad
end
method_missing
send
method_missing(name, *args)
class Thing
def foo
puts \"I pity the foo!\"
end
def method_missing(name, *args)
puts \"Called #{name} with #{args.inspect}\"
end
end
class Thing
def foo
puts \"I pity the foo!\"
end
def method_missing(name, *args)
puts \"Called #{name} with #{args.inspect}\"
end
end
>> thing = Thing.new
>> thing.foo
I pity the foo
>> thing.larodi 1, \"bar\", []
Called larodi with [1, \"bar\", []]
0 comments
Post a comment