This document introduces Ruby and object-oriented programming concepts using Ruby. It discusses creating classes like Brick and Worker, defining attributes and methods, inheritance with subclasses like YellowBrick and RedBrick, and contains examples of creating and manipulating objects. It also shows containing objects in a Box class, searching through objects in a box, and determining if a number is prime. The document provides resources for learning more about Ruby.
13. Yellow and Red bricks
class YellowBrick < Brick
def initialize
@color = :yellow # @ instance variable (always protected!)
@sockets = 6 # @@ class variable
end
end
class RedBrick < Brick
def initialize
@color = :red
@sockets = 6
end
end
14. Yellow and Red bricks
class YellowBrick < Brick
def initialize
@color = :yellow # @ instance variable (always protected!)
@sockets = 6 # @@ class variable
end
end
> yellowBrick = YellowBrick.new
> redBrick = RedBrick.new
class RedBrick < Brick
def initialize
> p yellowBrick
@color = :red
#<YellowBrick:0x10012ac68
@sockets = 6 @color=:yellow, @sockets=6>
end
end
16. Altering objects
class Brick
attr :color
attr_accessor :sockets
end attr
class Brick
def color
return @color
end
end
# return is not required!
17. Altering objects
class Brick
attr :color
attr_accessor :sockets
end attr attr_accessor
class Brick class Brick
def color def sockets
return @color @sockets
end end
end def sockets= (value)
@sockets = value
# return is not required! end
end
18. Assigning
class Brick
attr :color
attr_writer :sockets
def sockets= (number)
raise Exception.new("Invalid socket number") if number % 2 != 0
raise Exception.new("Too many sockets") unless number <= 10
@sockets = number
end
end
19. Assigning
class Brick
attr :color
attr_writer :sockets
def sockets= (number)
raise Exception.new("Invalid socket number") if number % 2 != 0
raise Exception.new("Too many sockets") = YellowBrick.new 10
> yellowBrick unless number <=
@sockets = number > yellowBrick.sockets = 4
end > puts yellowBrick.sockets
end #4
> yellowBrick.sockets = 5
# Exception: Invalid socket number
20. Box
class Box
def initialize
@items = []
end
def << (item)
@items << item
end
end
21. Box
class Box
def initialize
@items = [] > box = Box.new
end >
> 1.upto 5 do |number|
def << (item) > brick = YellowBrick.new
@items << item > brick.sockets = number * 2
end > box << brick
end > end
> p box
> #<Box:0x10012a650 @items=
[#<YellowBrick:0x10012a498
@color=:yellow, @sockets=2>,
#<YellowBrick...
22. Box
class Box
def initialize
@items = [] > box = Box.new
end >
> 1.upto 5 do |number|
def << (item) > brick = YellowBrick.new
@items << item > brick.sockets = number * 2
end > box << brick
end > end
#<Box:0x10012a650 @items=
> p box
[#<YellowBrick:0x10012a498 @color=:yellow, @sockets=2>,
> #<Box:0x10012a650 @items=
#<YellowBrick:0x10012a510 @color=:yellow, @sockets=4>,
#<YellowBrick:0x10012a4c0 @color=:yellow, @sockets=6>,
[#<YellowBrick:0x10012a498
#<YellowBrick:0x10012a470 @color=:yellow, @sockets=8>,
@color=:yellow, @sockets=2>,
#<YellowBrick:0x10012a448 @color=:yellow, @sockets=10>]>
#<YellowBrick...
23. Searching
class Box
def search (&block)
@items.each do |item|
yield item
end
end
end
24. Searching
class Box
> box = Box.new
def search (&block)
@items.each do |item| >
yield item > 1.upto 5 do |number|
end > brick = YellowBrick.new
end > brick.sockets = number * 2
end > box << brick
> end
> box.search do |brick|
> puts “#{brick.sockets} “
> end
# 2 4 6 8 10
25. Searching
class Box
> box = Box.new
def search (&block)
@items.each do |item| >
yield item > 1.upto 5 do |number|
end > brick = YellowBrick.new
end > brick.sockets = number * 2
end > box << brick
> end
> box.search do |brick|
> puts “#{brick.sockets} “
> end
# 2 4 6 8 10
28. 2.prim?
class Integer
def prim?
myValue = self.to_i
return false if myValue == 1
2.upto myValue-1 do | i |
return false if (myValue % i) == 0
end
return true
end
end
29. 2.prim?
Q
ue
class Integer
st
def prim?
io
myValue = self.to_i
return false if myValue == 1 ns
2.upto myValue-1 do | i |
?
return false if (myValue % i) == 0
end
return true
end
end