Ruby Meetup Balabit

602 views

Published on

Short tricks in Ruby in a meetup event...

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
602
On SlideShare
0
From Embeds
0
Number of Embeds
89
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ruby Meetup Balabit

  1. 1. First steps in Ruby MeetUP @ Balabit March 17, 2010 nucc@balabit.com
  2. 2. Reality is just a scope of an artist...
  3. 3. Ruby Yukihiro Matsumoto (1995) www.meetup.com/budapest-rb
  4. 4. Lego
  5. 5. Objects class Brick end class Worker end class Tree end class RoofTile end
  6. 6. Objects class Brick end class Worker 2 end class Tree end class RoofTile end
  7. 7. Objects class Brick end class Worker 2.prim? 2 5 end class Tree end 2.upto class RoofTile end
  8. 8. Objects class Brick end class Worker 2.prim? 2 5 end nil class Tree end 2.upto class RoofTile end
  9. 9. Objects class Brick end class Worker 2.prim? 2 5 end nil.nil? nil class Tree end 2.upto class RoofTile end
  10. 10. Brick objects class Brick attr :color attr :sockets end
  11. 11. 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
  12. 12. 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
  13. 13. Altering objects class Brick attr :color attr_accessor :sockets end
  14. 14. Altering objects class Brick attr :color attr_accessor :sockets end attr class Brick def color return @color end end # return is not required!
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. Box class Box def initialize @items = [] end def << (item) @items << item end end
  19. 19. 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...
  20. 20. 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...
  21. 21. Searching class Box def search (&block) @items.each do |item| yield item end end end
  22. 22. 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
  23. 23. 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
  24. 24. 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
  25. 25. More information... http://www.ruby-lang.org http://www.rubygems.org http://TryRuby.org
  26. 26. 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
  27. 27. 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
  28. 28. Thank you!

×