Ruby Meetup Balabit

30
First steps in Ruby March 17, 2010 [email protected] MeetUP @ Balabit

description

Short tricks in Ruby in a meetup event...

Transcript of Ruby Meetup Balabit

Page 1: Ruby Meetup Balabit

First steps in Ruby

March 17, [email protected]

MeetUP @ Balabit

Page 2: Ruby Meetup Balabit

Reality is just a scope of an artist...

Balabit Meetup - March 17, 2010 [email protected]

Page 5: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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.”

Page 6: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Lego

Page 7: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Objects

class Brickend

class Workerend

class Treeend

class RoofTileend

Page 8: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Objects

class Brickend

class Workerend

class Treeend

class RoofTileend

2

Page 9: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Objects

class Brickend

class Workerend

class Treeend

class RoofTileend

22.prim?2.upto 5

Page 10: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Objects

class Brickend

class Workerend

class Treeend

class RoofTileend

22.prim?2.upto 5nil

Page 11: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Objects

class Brickend

class Workerend

class Treeend

class RoofTileend

22.prim?2.upto 5nilnil.nil?

Page 12: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Brick objects

class Brick attr :color attr :socketsend

Page 13: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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

Page 14: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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>

Page 15: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Altering objects

class Brick attr :color attr_accessor :sockets end

attrattr_accessor

Page 16: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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

Page 17: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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

Page 18: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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

Page 19: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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

Page 20: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Box

class Box def initialize @items = [] end

def << (item) @items << item endend

Page 21: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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...

Page 22: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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>]>

Page 23: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Searching

class Box def search (&block) @items.each do |item| yield item end endend

Page 24: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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

Page 25: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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

Page 26: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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

Page 27: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

More information...

http://www.ruby-lang.org

http://www.rubygems.org

http://TryRuby.org

Page 28: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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

Page 29: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

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?

Page 30: Ruby Meetup Balabit

Balabit Meetup - March 17, 2010 [email protected]

Thank you!