Last active
August 29, 2015 13:57
-
-
Save mertyildiran/9696680 to your computer and use it in GitHub Desktop.
Object Oriented Programming in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
class Point | |
# XXX Her nitelik için okuyucu yazmamız gerekmiyor | |
attr_reader :x, :y | |
def initialize(x, y) | |
@x, @y = x, y | |
end | |
# Ruby'de "doğrulayıcı" (predicator) böyle yazılır | |
def right?(other) | |
@x > other.x | |
end | |
def left?(other) | |
@x < other.x | |
end | |
def above?(other) | |
@y > other.y | |
end | |
def below?(other) | |
@y < other.y | |
end | |
# FIXME foo.equal?(bar) yerine foo == bar yazılamaz mı? | |
def equal?(other) | |
@x == other.x and @y == other.y | |
end | |
def move(x, y) | |
@x += x | |
@y += y | |
# XXX Taşımadan sonra nesnenin kendisini dön ki | |
# çağıran ayrı bir atama yapma zorunda kalmasın | |
self | |
end | |
def to_s | |
"(#{@x},#{@y})" | |
end | |
end | |
def main | |
p, q = Point.new(3, 5), Point.new(9, 7) | |
puts "#{p} noktası #{q} noktasının " + (p.left?(q) ? 'solunda' : 'sağında') | |
end | |
main if __FILE__ == $PROGRAM_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
class Point | |
attr_reader :x, :y | |
ORIGIN = 0, 0 | |
def initialize(x, y) | |
@x, @y = x, y | |
end | |
def >(other) | |
@x > other.x | |
end | |
def <(other) | |
@x < other.x | |
end | |
def >=(other) | |
@x > other.x or @x == other.x | |
end | |
def <=(other) | |
@x < other.x or @x == other.x | |
end | |
def ==(other) | |
@x == other.x and @y == other.y | |
end | |
# + işlecini overload ederek sınırsız sayıda noktayı da | |
# toplama olanağına kavuştuk. | |
def +(other = nil) | |
# Nesne metodundan sınıf metodu (origin) nasıl çağrılır? | |
# Point.origin olabilir ama DRY değil, cevap: self.class | |
other ||= self.class.origin | |
self.class.new @x + other.x, @y + other.y | |
end | |
def move(x, y) | |
@x += x | |
@y += y | |
self | |
end | |
def to_s | |
"(#{@x},#{@y})" | |
end | |
# Sınıf metodları | |
# Her seferinde self. yazmak yerine sınıfı yarıp metodu | |
# içine koyalım. Böylesi daha DRY | |
# Burada self ne değer alır? Point | |
class << self | |
def origin | |
# new metodunun alıcısı kim? self | |
# self burada ne değer alır? Point (yani sınıf) | |
new(*ORIGIN) | |
end | |
def distance(here, there = nil) | |
there ||= origin | |
Math.sqrt((here.x - there.x)**2 + (here.y - there.y)**2) | |
end | |
end | |
end | |
def main | |
p, q, r = Point.new(3, 5), Point.new(9, 7), Point.new(-2, 5) | |
puts "Noktalar: #{p} #{q} #{r}" | |
puts "Toplam noktası: #{p + q + r}" | |
puts "#{p} - #{q} uzaklığı: #{Point.distance(p, q)}" | |
puts "#{p} - orijin uzaklığı: #{Point.distance(p)}" | |
end | |
main if __FILE__ == $PROGRAM_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
class Engine | |
# Motor'la ilgili tüm ortak akıl burada... | |
def start | |
puts "başla" | |
end | |
def stop | |
puts "dur" | |
end | |
end | |
class Car | |
def initialize | |
@engine = Engine.new | |
end | |
# Hafta sonu turu | |
def sunday_drive | |
@engine.start | |
# Gez dolaş ve dön... | |
@engine.stop | |
end | |
end | |
if __FILE__ == $0 | |
car = Car.new | |
car.sunday_drive | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
class Engine | |
# Motor'la ilgili tüm ortak akıl burada... | |
def start | |
puts "başla" | |
end | |
def stop | |
puts "dur" | |
end | |
end | |
class GasolineEngine < Engine | |
# Benzinli Motor | |
end | |
class DieselEngine < Engine | |
# Dizel Motor | |
end | |
class Car | |
def initialize | |
@engine = GasolineEngine.new | |
end | |
# Hafta sonu turu | |
def sunday_drive | |
puts "Motor tipi: #{@engine.class}" | |
@engine.start | |
# Gez dolaş ve dön... | |
@engine.stop | |
end | |
# Dizel'e çevir | |
def switch_to_diesel | |
@engine = DieselEngine.new | |
end | |
end | |
if __FILE__ == $0 | |
car = Car.new | |
car.sunday_drive | |
car.switch_to_diesel | |
car.sunday_drive | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
class Engine | |
# Motor'la ilgili tüm ortak akıl burada... | |
def start | |
puts "başla" | |
end | |
def stop | |
puts "dur" | |
end | |
end | |
class GasolineEngine < Engine | |
# Benzinli Motor | |
end | |
class DieselEngine < Engine | |
# Dizel Motor | |
end | |
class Car | |
def initialize | |
@engine = GasolineEngine.new | |
end | |
def sunday_drive | |
puts "Motor tipi: #{@engine.class}" | |
start | |
# Cruise out into the country and return... | |
stop | |
end | |
def switch_to_diesel | |
@engine = DieselEngine.new | |
end | |
def start | |
@engine.start | |
end | |
def stop | |
@engine.stop | |
end | |
end | |
if __FILE__ == $0 | |
car = Car.new | |
car.sunday_drive | |
car.switch_to_diesel | |
car.sunday_drive | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
require 'forwardable' | |
# Motor | |
class Engine | |
# Motor'la ilgili tüm ortak akıl burada... | |
def start | |
puts 'başla' | |
end | |
def stop | |
puts 'dur' | |
end | |
end | |
class GasolineEngine < Engine | |
# Benzinli Motor | |
end | |
class DieselEngine < Engine | |
# Dizel Motor | |
end | |
# Araba | |
class Car | |
extend Forwardable | |
def_delegators :@engine, :start, :stop | |
def initialize | |
@engine = GasolineEngine.new | |
end | |
def sunday_drive | |
puts "Motor tipi: #{@engine.class}" | |
start | |
# Cruise out into the country and return... | |
stop | |
end | |
def switch_to_diesel | |
@engine = DieselEngine.new | |
end | |
end | |
if __FILE__ == $PROGRAM_NAME | |
car = Car.new | |
car.sunday_drive | |
car.switch_to_diesel | |
car.sunday_drive | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
class Vehicle | |
# Araç'la ilgili tüm ortak akıl burada... | |
def start_engine | |
puts "başla" | |
end | |
def stop_engine | |
puts "dur" | |
end | |
end | |
class Car < Vehicle | |
# Hafta sonu turu | |
def sunday_drive | |
start_engine | |
# Gez dolaş ve dön... | |
stop_engine | |
end | |
end | |
if __FILE__ == $0 | |
car = Car.new | |
car.sunday_drive | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
class Point | |
attr_reader :x, :y | |
ORIGIN = 0, 0 | |
def initialize(x, y) | |
@x, @y = x, y | |
end | |
def >(other) | |
@x > other.x | |
end | |
def <(other) | |
@x < other.x | |
end | |
def >=(other) | |
@x > other.x or @x == other.x | |
end | |
def <=(other) | |
@x < other.x or @x == other.x | |
end | |
def ==(other) | |
@x == other.x and @y == other.y | |
end | |
def +(other = nil) | |
other ||= self.class.origin | |
self.class.new @x + other.x, @y + other.y | |
end | |
def move(x, y) | |
@x += x | |
@y += y | |
self | |
end | |
def to_s | |
"(#{@x},#{@y})" | |
end | |
# Sınıf metodları | |
class << self | |
def origin | |
new(*ORIGIN) | |
end | |
def distance(here, there = nil) | |
there ||= origin | |
Math.sqrt((here.x - there.x)**2 + (here.y - there.y)**2) | |
end | |
end | |
end | |
class Circle < Point | |
attr_reader :r | |
def initialize(*coords, r) | |
super(*coords) | |
@r = r | |
end | |
def perimeter | |
2 * Math::PI * @r | |
end | |
def area | |
Math::PI * @r**2 | |
end | |
def to_s | |
# XXX Ooooo! Artık DRY ama kara büyü :-/ | |
"#{@r}@#{self.class.superclass.instance_method(:to_s).bind(self).call}" | |
end | |
# XXX Anlamlı olmayan işleçler şuna benzer bir şey yapılabilir | |
def +(*) # argümanlarla ilgilenmediğimizden * diyoruz | |
raise "+ işleci #{self.class} sınıfı için anlamlı değil" | |
end | |
end | |
def main | |
c = Circle.new 3, 5, 2 | |
puts "#{c} çemberinin alanı: #{c.area}" | |
puts "#{c} çemberi #{c.move(7, 9)} çemberine dönüştü" | |
end | |
main if __FILE__ == $PROGRAM_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
class Point | |
attr_reader :x, :y | |
def initialize(x, y) | |
@x, @y = x, y | |
end | |
def >(other) | |
@x > other.x | |
end | |
def <(other) | |
@x < other.x | |
end | |
def >=(other) | |
@x > other.x or @x == other.x | |
end | |
def <=(other) | |
@x < other.x or @x == other.x | |
end | |
def ==(other) | |
@x == other.x and @y == other.y | |
end | |
def to_s | |
"(#{@x},#{@y})" | |
end | |
end | |
def main | |
p, q = Point.new(3, 5), Point.new(9, 7) | |
puts "#{p} noktası #{q} noktasının " + (p < q ? 'solunda' : 'sağında') | |
end | |
main if __FILE__ == $PROGRAM_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
class Point | |
attr_reader :x, :y | |
ORIGIN = 0, 0 | |
NAME = 'nokta' | |
def initialize(x, y) | |
@x, @y = x, y | |
end | |
def >(other) | |
@x > other.x | |
end | |
def <(other) | |
@x < other.x | |
end | |
def >=(other) | |
@x > other.x or @x == other.x | |
end | |
def <=(other) | |
@x < other.x or @x == other.x | |
end | |
def ==(other) | |
@x == other.x and @y == other.y | |
end | |
def +(other = nil) | |
other ||= origin | |
self.class.new @x + other.x, @y + other.y | |
end | |
def move(x, y) | |
@x += x | |
@y += y | |
self | |
end | |
def to_s | |
"(#{@x},#{@y})" | |
end | |
# Sınıf metodları | |
class << self | |
def name | |
NAME | |
end | |
def origin | |
new(*ORIGIN) | |
end | |
def distance(here, there = nil) | |
there ||= origin | |
Math.sqrt((here.x - there.x)**2 + (here.y - there.y)**2) | |
end | |
end | |
end | |
class Circle < Point | |
attr_reader :r | |
NAME = 'çember' | |
def initialize(*coords, r) | |
super(*coords) | |
@r = r | |
end | |
def +(*) | |
raise 'Bu şeklin toplanması anlamı değil' | |
end | |
def perimeter | |
2 * Math::PI * @r | |
end | |
def area | |
Math::PI * @r**2 | |
end | |
def to_s | |
"#{@r}@(#{self.x},#{self.y})" # XXX DRY değil ama okunur | |
end | |
end | |
class Square < Circle | |
FACTOR = 2 / Math.sqrt(2) | |
NAME = 'kare' | |
def perimeter | |
4 * FACTOR * self.r | |
end | |
def area | |
(FACTOR * self.r)**2 | |
end | |
end | |
def main | |
[Circle.new(3, 5, 2), Square.new(9, 4, 1)].each do |shape| | |
puts "#{shape} şeklinin alanı: #{shape.area}" | |
end | |
end | |
main if __FILE__ == $PROGRAM_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
class Point | |
def initialize(x, y) | |
@x, @y = x, y | |
end | |
# FIXME Her nitelik için böyle okuyucu mu yazacağız? | |
def x | |
@x | |
end | |
def y | |
@y | |
end | |
def isright(other) | |
@x > other.x | |
end | |
def isleft(other) | |
@x < other.x | |
end | |
def isabove(other) | |
@y > other.y | |
end | |
def isbelow(other) | |
@y < other.y | |
end | |
def isequal(other) | |
@x == other.x and @y == other.y | |
end | |
def move(x, y) | |
@x += x | |
@y += y | |
# FIXME Bu metod (şu noktada) ne döner? Zararı? | |
end | |
def to_s | |
"(#{@x},#{@y})" | |
end | |
end | |
def main | |
p, q = Point.new(3, 5), Point.new(9, 7) | |
puts "#{p} noktası #{q} noktasının " + (p.isleft(q) ? 'solunda' : 'sağında') | |
end | |
main if __FILE__ == $PROGRAM_NAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment