Post on 04-Jul-2015
description
transcript
First steps in Ruby
March 17, 2010nucc@balabit.com
MeetUP @ Balabit
Reality is just a scope of an artist...
Balabit Meetup - March 17, 2010 nucc@balabit.hu
Balabit Meetup - March 17, 2010 nucc@balabit.com
Ruby
Yukihiro Matsumoto (1995)
www.meetup.com/budapest-rb
“... They are focusing on machines. But in fact we need to focus on humans, on how humans care about doing programming or operating the application of the machines. We are the masters. They are the slaves.”
Balabit Meetup - March 17, 2010 nucc@balabit.com
Objects
class Brickend
class Workerend
class Treeend
class RoofTileend
Balabit Meetup - March 17, 2010 nucc@balabit.com
Objects
class Brickend
class Workerend
class Treeend
class RoofTileend
2
Balabit Meetup - March 17, 2010 nucc@balabit.com
Objects
class Brickend
class Workerend
class Treeend
class RoofTileend
22.prim?2.upto 5
Balabit Meetup - March 17, 2010 nucc@balabit.com
Objects
class Brickend
class Workerend
class Treeend
class RoofTileend
22.prim?2.upto 5nil
Balabit Meetup - March 17, 2010 nucc@balabit.com
Objects
class Brickend
class Workerend
class Treeend
class RoofTileend
22.prim?2.upto 5nilnil.nil?
Balabit Meetup - March 17, 2010 nucc@balabit.com
Brick objects
class Brick attr :color attr :socketsend
Balabit Meetup - March 17, 2010 nucc@balabit.com
Yellow and Red bricks
class YellowBrick < Brick def initialize @color = :yellow # @ instance variable (always protected!) @sockets = 6 # @@ class variable endend
class RedBrick < Brick def initialize @color = :red @sockets = 6 endend
Balabit Meetup - March 17, 2010 nucc@balabit.com
Yellow and Red bricks
class YellowBrick < Brick def initialize @color = :yellow # @ instance variable (always protected!) @sockets = 6 # @@ class variable endend
class RedBrick < Brick def initialize @color = :red @sockets = 6 endend
> yellowBrick = YellowBrick.new > redBrick = RedBrick.new
> p yellowBrick #<YellowBrick:0x10012ac68 @color=:yellow, @sockets=6>
Balabit Meetup - March 17, 2010 nucc@balabit.com
Altering objects
class Brick attr :color attr_accessor :sockets end
attrattr_accessor
Balabit Meetup - March 17, 2010 nucc@balabit.com
Altering objects
class Brick attr :color attr_accessor :sockets end
class Brick def color return @color end end
# return is not required!
attr
attr_accessor
Balabit Meetup - March 17, 2010 nucc@balabit.com
Altering objects
class Brick attr :color attr_accessor :sockets end
class Brick def color return @color end end
# return is not required!
class Brick def sockets @sockets end def sockets= (value) @sockets = value end end
attr attr_accessor
Balabit Meetup - March 17, 2010 nucc@balabit.com
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
Balabit Meetup - March 17, 2010 nucc@balabit.com
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
> yellowBrick = YellowBrick.new > yellowBrick.sockets = 4 > puts yellowBrick.sockets # 4
> yellowBrick.sockets = 5 # Exception: Invalid socket number
Balabit Meetup - March 17, 2010 nucc@balabit.com
Box
class Box def initialize @items = [] end
def << (item) @items << item endend
Balabit Meetup - March 17, 2010 nucc@balabit.com
Box
class Box def initialize @items = [] end
def << (item) @items << item endend
> box = Box.new > > 1.upto 5 do |number| > brick = YellowBrick.new > brick.sockets = number * 2 > box << brick > end
> p box > #<Box:0x10012a650 @items=[#<YellowBrick:0x10012a498 @color=:yellow, @sockets=2>, #<YellowBrick...
Balabit Meetup - March 17, 2010 nucc@balabit.com
Box
class Box def initialize @items = [] end
def << (item) @items << item endend
> box = Box.new > > 1.upto 5 do |number| > brick = YellowBrick.new > brick.sockets = number * 2 > box << brick > end
> p box > #<Box:0x10012a650 @items=[#<YellowBrick:0x10012a498 @color=:yellow, @sockets=2>, #<YellowBrick...
#<Box:0x10012a650 @items=[#<YellowBrick:0x10012a498 @color=:yellow, @sockets=2>, #<YellowBrick:0x10012a510 @color=:yellow, @sockets=4>, #<YellowBrick:0x10012a4c0 @color=:yellow, @sockets=6>, #<YellowBrick:0x10012a470 @color=:yellow, @sockets=8>, #<YellowBrick:0x10012a448 @color=:yellow, @sockets=10>]>
Balabit Meetup - March 17, 2010 nucc@balabit.com
Searching
class Box def search (&block) @items.each do |item| yield item end endend
Balabit Meetup - March 17, 2010 nucc@balabit.com
Searching
class Box def search (&block) @items.each do |item| yield item end endend
> box = Box.new > > 1.upto 5 do |number| > brick = YellowBrick.new > brick.sockets = number * 2 > box << brick > end
> box.search do |brick| > puts “#{brick.sockets} “ > end
# 2 4 6 8 10
Balabit Meetup - March 17, 2010 nucc@balabit.com
Searching
class Box def search (&block) @items.each do |item| yield item end endend
> box = Box.new > > 1.upto 5 do |number| > brick = YellowBrick.new > brick.sockets = number * 2 > box << brick > end
> box.search do |brick| > puts “#{brick.sockets} “ > end
# 2 4 6 8 10
Balabit Meetup - March 17, 2010 nucc@balabit.com
How to start...
apt-get install ruby
apt-get install irb
apt-get install rubygems (11345 packages)
gem install rails gem install SyslogLogger gem install hpricot
Balabit Meetup - March 17, 2010 nucc@balabit.com
More information...
http://www.ruby-lang.org
http://www.rubygems.org
http://TryRuby.org
Balabit Meetup - March 17, 2010 nucc@balabit.com
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 endend
Balabit Meetup - March 17, 2010 nucc@balabit.com
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 endend
Questions?
Balabit Meetup - March 17, 2010 nucc@balabit.com
Thank you!