SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
First steps in Ruby

   MeetUP @ Balabit



       March 17, 2010
      nucc@balabit.com
Reality is just a scope of an artist...
Ruby
 Yukihiro Matsumoto (1995)

 www.meetup.com/budapest-rb
Lego
Objects
 class Brick
 end


 class Worker
 end


 class Tree
 end


 class RoofTile
 end
Objects
 class Brick
 end


 class Worker


                  2
 end


 class Tree
 end


 class RoofTile
 end
Objects
 class Brick
 end


 class Worker
                  2.prim?
                     2 5
 end


 class Tree
 end
                  2.upto
 class RoofTile
 end
Objects
 class Brick
 end


 class Worker
                  2.prim?
                     2 5
 end

                     nil
 class Tree
 end
                  2.upto
 class RoofTile
 end
Objects
 class Brick
 end


 class Worker
                  2.prim?
                      2 5
 end

                   nil.nil?
                     nil
 class Tree
 end
                  2.upto
 class RoofTile
 end
Brick objects
class Brick
   attr :color
   attr :sockets
end
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
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
Altering objects
class Brick
  attr :color
  attr_accessor :sockets
end
Altering objects
class Brick
  attr :color
  attr_accessor :sockets
end                                    attr
           class Brick
              def color
                return @color
              end
           end

           # return is not required!
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
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
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
Box
class Box
   def initialize
     @items = []
   end

  def << (item)
    @items << item
  end
end
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...
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...
Searching
class Box
   def search (&block)
     @items.each do |item|
       yield item
     end
   end
end
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
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
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
More information...
 http://www.ruby-lang.org

 http://www.rubygems.org

 http://TryRuby.org
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
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
Thank you!

Weitere ähnliche Inhalte

Andere mochten auch (6)

Pubic Art Proposal
Pubic Art ProposalPubic Art Proposal
Pubic Art Proposal
 
How to Apply to Artist Residencies
How to Apply to Artist ResidenciesHow to Apply to Artist Residencies
How to Apply to Artist Residencies
 
Proposal Writing tips by julia vogl
Proposal Writing tips by julia voglProposal Writing tips by julia vogl
Proposal Writing tips by julia vogl
 
Making the Most of Your Residency Application: What to Do ... or Not
Making the Most of Your Residency Application: What to Do ... or NotMaking the Most of Your Residency Application: What to Do ... or Not
Making the Most of Your Residency Application: What to Do ... or Not
 
Approaching Galleries & Proposal Writing for Artists
Approaching Galleries & Proposal Writing for ArtistsApproaching Galleries & Proposal Writing for Artists
Approaching Galleries & Proposal Writing for Artists
 
Residency Interview Advice
Residency Interview AdviceResidency Interview Advice
Residency Interview Advice
 

Mehr von Papp Laszlo (11)

Shade műhely
Shade műhelyShade műhely
Shade műhely
 
Git thinking
Git thinkingGit thinking
Git thinking
 
Open Academy - Ruby
Open Academy - RubyOpen Academy - Ruby
Open Academy - Ruby
 
Have2do.it
Have2do.itHave2do.it
Have2do.it
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Munkafolyamatok modellezése OPM segítségével
Munkafolyamatok modellezése OPM segítségévelMunkafolyamatok modellezése OPM segítségével
Munkafolyamatok modellezése OPM segítségével
 
Ruby gems
Ruby gemsRuby gems
Ruby gems
 
Resource and view
Resource and viewResource and view
Resource and view
 
Rails Models
Rails ModelsRails Models
Rails Models
 
Balabit Meetup - Ruby on Rails
Balabit Meetup - Ruby on RailsBalabit Meetup - Ruby on Rails
Balabit Meetup - Ruby on Rails
 
Rails
RailsRails
Rails
 

Kürzlich hochgeladen

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

Ruby Meetup Balabit

  • 1. First steps in Ruby MeetUP @ Balabit March 17, 2010 nucc@balabit.com
  • 2. Reality is just a scope of an artist...
  • 3.
  • 4.
  • 5. Ruby Yukihiro Matsumoto (1995) www.meetup.com/budapest-rb
  • 7. Objects class Brick end class Worker end class Tree end class RoofTile end
  • 8. Objects class Brick end class Worker 2 end class Tree end class RoofTile end
  • 9. Objects class Brick end class Worker 2.prim? 2 5 end class Tree end 2.upto class RoofTile end
  • 10. Objects class Brick end class Worker 2.prim? 2 5 end nil class Tree end 2.upto class RoofTile end
  • 11. Objects class Brick end class Worker 2.prim? 2 5 end nil.nil? nil class Tree end 2.upto class RoofTile end
  • 12. Brick objects class Brick attr :color attr :sockets end
  • 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
  • 15. Altering objects class Brick attr :color attr_accessor :sockets 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
  • 26. 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
  • 27. More information... http://www.ruby-lang.org http://www.rubygems.org http://TryRuby.org
  • 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