SlideShare a Scribd company logo
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!

More Related Content

Viewers also liked

Pubic Art Proposal
Pubic Art ProposalPubic Art Proposal
Pubic Art Proposal
kzhou6692
 
How to Apply to Artist Residencies
How to Apply to Artist ResidenciesHow to Apply to Artist Residencies
How to Apply to Artist Residencies
Steve Giovinco
 
Proposal Writing tips by julia vogl
Proposal Writing tips by julia voglProposal Writing tips by julia vogl
Proposal Writing tips by julia vogl
AsylumArts
 
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
Victor Castilla
 
Approaching Galleries & Proposal Writing for Artists
Approaching Galleries & Proposal Writing for ArtistsApproaching Galleries & Proposal Writing for Artists
Approaching Galleries & Proposal Writing for Artists
ArtLinks
 
Residency Interview Advice
Residency Interview AdviceResidency Interview Advice
Residency Interview Advice
osumc2014
 

Viewers also liked (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
 

More from Papp Laszlo

Shade műhely
Shade műhelyShade műhely
Shade műhely
Papp Laszlo
 
Git thinking
Git thinkingGit thinking
Git thinking
Papp Laszlo
 
Open Academy - Ruby
Open Academy - RubyOpen Academy - Ruby
Open Academy - Ruby
Papp Laszlo
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Papp Laszlo
 
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
Papp Laszlo
 
Ruby gems
Ruby gemsRuby gems
Ruby gems
Papp Laszlo
 
Resource and view
Resource and viewResource and view
Resource and view
Papp Laszlo
 
Rails Models
Rails ModelsRails Models
Rails Models
Papp Laszlo
 
Balabit Meetup - Ruby on Rails
Balabit Meetup - Ruby on RailsBalabit Meetup - Ruby on Rails
Balabit Meetup - Ruby on Rails
Papp Laszlo
 
Rails
RailsRails

More from 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
 

Recently uploaded

Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 

Recently uploaded (20)

Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 

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