SlideShare a Scribd company logo
1 of 29
Download to read offline
A partial introduction to the
                        ^
    The Ruby Programming Language
                                   by Anthony W. Brown




Tuesday, March 26, 13
Introduction

         This presentation is an adaptation from “The
         Ruby Programming Language” by Matz and the
         University of Washington’s Ruby and Rails PCE
         course.

         This deck was originally intended to cover all object-oriented principles, popular
         tools and the Rails framework. If anyone likes this deck, I will create rest for
         you...




Tuesday, March 26, 13
Useful Links


            •    http://www.ruby-doc.org/
            •    http://apidock.com/ruby
            •    http://apidock.com/rspec
            •    http://zenspider.com/Languages/Ruby/
                 quickref.html




Tuesday, March 26, 13
Origins
          “Ruby is simple in appearance, but is very complex
          inside, just like our human body”
                                                           -Matz




Tuesday, March 26, 13
Ruby is...


            •    Object Oriented
            •    Dynamic
            •    Reflective
            •    Interpreted




Tuesday, March 26, 13
Ruby in a Slide

            •    TDD
            •    IRB
            •    Gems
            •    Bundler
            •    Rspec




Tuesday, March 26, 13
Tools

            •    git, github
            •    RVM
            •    Ruby Gems
            •    RSpec
            •    Rake
            •    Guard




Tuesday, March 26, 13
Install Check



                           awb - bash - 80x24
      Last login: Sun Feb 10 11:28:48
      macpro:~ awb$ git --version                 #   Check git version
      macpro:~ awb$ ruby -v                       #   Check Ruby version, 1.9.3?
      macpro:~ awb$ irb                           #   Interactive Ruby Environment
      macpro:~ awb$ gem install rspec
                                                  #   Install the RSpec test tools




Tuesday, March 26, 13
git


                           awb - bash - 80x24
   Last login: Sun Feb 10 11:28:48
    macpro:~ awb$ git config                     #   set or view configuration
    macpro:~ awb$ git clone                      #   copy repository
    macpro:~ awb$ git init                       #   make current dir a repo
    macpro:~ awb$ git status                     #   get current state or repo
    macpro:~ awb$ git add                        #   add items to index
    macpro:~ awb$ git rm                         #   remove items from index
    macpro:~ awb$ git commit                     #   commit index to repo
   macpro:~ awb$ git push
                                                 #   move commits to remoter server
   macpro:~ awb$ git log
                                                 #   list of commits in this repo




Tuesday, March 26, 13
RSpec
                        http://rubydoc.info/gems/rspec-expectations/2.4.0/RSpec/Matchers


       describe                                     #describes “what” in a test
         context                                    #describes “when” in a test
           it                                       #performs an operation,“does”
             should                                 #Class attribute
               eq                                   #matcher
               be_a                                 #matcher
               kind_of                              #matcher


                                                                            awb - bash - 80x24
                                                    Last login: Sun Feb 10 11:28:48
                                                    macpro:~ awb$ rspec -c -f d ‘filename’




Tuesday, March 26, 13
Rake
                        Rake is Ruby’s version of make: a domain specific language to define tasks.
                        Easy to manage dependencies and tasks.


     task :clean_update do                           =begin
       system(clean_up_script.sh)                    Ruby version of make; A DSL to
       MyClass.update_everything(‘./                 define tasks
       path’)                                        Easy to manage dependencies and
     end                                             tasks
                                                     =end


                                                                             awb - bash - 80x24
                                                     Last login: Sun Feb 10 11:28:48
                                                     macpro:~ awb$ rake clean_update




Tuesday, March 26, 13
Objects
                        Everything in Ruby is an object.


         “hello”                                      #string

         4                                            #integer

         []                                           #empty array

         my_array

         :symbol
                                                      #comment

                                                      =begin
                                                      multi-line comment
                                                      =end




Tuesday, March 26, 13
Operators

         -                  #Unary operators
         !
         *
         ~

         + -                #Binary operators
         * / **
         <<
         && ||


         my_variable?       #Ternary operators
         ‘True’:




Tuesday, March 26, 13
Assignment
                        Hold values, or references to objects.

                                                      # lvalue are assignment,rvalue
                                                      # are reference


         a = b                                        #lvalue = rvalue

         a += 1                                       #abbreviated assignment same
                                                      #as a = a + 1

         a, b = 1, 2                                  # a = 1, b = 2

         a, b, c = [1,2]                              # a = 1, b = 2, c = nil




Tuesday, March 26, 13
Expressions

         ‘hello’             =begin

         File                Expressions evaluate to and
                             return a value.
         true
                             Compound expressions leverage
         false               an operator (+/-/*/%)
         nil                 =end
         self




Tuesday, March 26, 13
Variables
                        Variables hold values, or references to objects
                                                       #initialize to prevent NameError
         a = “Woof!”

         b = a                                         #b = “Woof!”

         b.object_id                                   #b = “Woof!”

         c = a.dup                                     #c = “Woof!”

         b[0] = “P”                                    #a and b = “Poof!” c still
                                                       #“Woof!”

                                                       #Local: my_var
                                                       #Global: $password
                                                       #Class: @@counter
                                                       #Instance: @name
                                                       #Constant: MY_CONST



Tuesday, March 26, 13
Strings
                        Text is represented by strings, which are of class String.



         ’35’                                          #single-quoted string literal

         “35”                                          #double-quoted string literal


         my_age = “35”                                 #Assigned string variable

         “ My age is #{my_age}”                        #Interpolation




Tuesday, March 26, 13
Numbers
                        Ruby has 5 built-in classes for numbers in the standard library.



         123456789                                     #Literal Objects

         3.141519                                      #Class Hierarchy:

         1,000,000,000,000                             #Numeric

                                                         #Integer

                                                            #Fixnum

                                                               #Bignum




Tuesday, March 26, 13
Arrays
                        Arrays are lists of values that can be accessed by their position.

         my_array = [“1”, “2”, “3”                     =begin

         1, 2, 3                                       Arrays are not typed, ordered,
                                                       mutable, literal, and
         a, << “1”, “2”, “3”                           Enumerable.They contain many
                                                       operators and methods like:
         my_array = []
                                                       + << *
         []
                                                       .push .pop
         Array.new
                                                       .sort.

                                                       .uniq!

                                                       =end




Tuesday, March 26, 13
Hashes
                        Hashes are key/value pairs.

         my_hash = {:a =>“1”, :b =>“2”} =begin

         my_hash = {}                                 Hashes like arrays are not
                                                      typed. They are ordered,and
         {a: 1, b: “2”}                               Enumerable.They contain many
                                                      operators and methods like:
         my_hash = [:a] = 1
                                                      =end
         my_hash = [:b] = 2

         Hash.new




Tuesday, March 26, 13
Iterators
                        Like a loop, iterators perform operations on data sets.


         3.times {puts “thank you!”}                   #

         my_data.each { |x| print x}                   #

         [1,2,3].map { |x| x*5}                        #




Tuesday, March 26, 13
Methods
                        Methods pass messages to objects. They have 5 parts; the object, message,
                        parameter, block and body.


         my_method                                  #Various ways to call a method

         my_method parameter

         my_method (parameter)

         my_method (parameter1, 2)

         object.my_method

         my_method par1, par2, do|                  #Syntax structure
         block| tweet! “#{par1} just                #object.method(parameter) {block}
         did #{par2}”

         end




Tuesday, March 26, 13
Method Object
                        Objects receive messages. Every object has methods and self


         def my_method; end                         #dangerous methods end in ‘!’

         object.respond_to? :my_method              # => true




Tuesday, March 26, 13
Method Message
                        The Message is required. Style is to use snake_case naming conventions.


                                                     #predicate methods end in ‘?’

         [].empty?                                   # => true


         my_method.uniq                              #dangerous methods end in ‘!’

         my_method.uniq!                             doesn’t modify the method

                                                     does modify the method




Tuesday, March 26, 13
Method Message
                        The Message is required. Style is to use snake_case naming conventions.
                                                     #predicate methods end in ‘?’
         [].empty?                                   # => true

                                                     #dangerous methods end in ‘!’
         my_method.uniq                              #doesn’t modify the method
         my_method.uniq!                             #does modify the method


         def name = str                              #attribute setter method name end =
           @name = str
         end


         class MyObject                             #index lookup (-ish) methods use []
           def [] num
             @collection[num]
           end
         end


Tuesday, March 26, 13
Method Parameters

                                    #0 or more, comma separated,
                                    #defaults can be defined.
         def say str1, str2, str3
           “#{str1} --- #{str2}”
         end

         say ‘hi’, ‘bye’            # => ‘hi --- bye’




Tuesday, March 26, 13
Blocks
                        Nameless function (arbitrary code) passed to a method with {code} or ‘do
                        code end’. Any method can receive a block.


         def my_method par                           =begin
           {|block_par| use block par}
         end                                         Blocks to not have to be named
                                                     as a parameter and are ignored
                                                     if not explicitly used by a
         def my_dog                                  method. The block passed
           print “I’m a dog...”                      describes what to do with each
           yield                                     thing.
         end
                                                     =end

         run {print “Bark, bark!”}                   # => I’m a dog. Bark, bark!

                                                     #




Tuesday, March 26, 13
Blocks
                        Example


         module Enumerable                 # Calling Enumerable

              def map                      # Create a method
                result = []                # Assign a variable
                self.each do |object|      # Create a block
                  result << yield object
                end
                result
              end

         end

         [1, 2, 3].map { |n| n * 2 }       # => [2,4,6]




Tuesday, March 26, 13
More to come. If you have any questions, please contact me on various social
         media networks with the alias @awbrown.




Tuesday, March 26, 13

More Related Content

What's hot

Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to RubyRanjith Siji
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java ProgrammersMike Bowler
 
Ruby for Java Developers
Ruby for Java DevelopersRuby for Java Developers
Ruby for Java DevelopersRobert Reiz
 
Agile DSL Development in Ruby
Agile DSL Development in RubyAgile DSL Development in Ruby
Agile DSL Development in Rubyelliando dias
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyNick Sieger
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# DevelopersCory Foy
 
Developing cross platform desktop application with Ruby
Developing cross platform desktop application with RubyDeveloping cross platform desktop application with Ruby
Developing cross platform desktop application with RubyAnis Ahmad
 
Ruby for C#-ers (ScanDevConf 2010)
Ruby for C#-ers (ScanDevConf 2010)Ruby for C#-ers (ScanDevConf 2010)
Ruby for C#-ers (ScanDevConf 2010)Thomas Lundström
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.pptcallroom
 
name name2 n2
name name2 n2name name2 n2
name name2 n2callroom
 

What's hot (13)

Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
 
Ruby for Java Developers
Ruby for Java DevelopersRuby for Java Developers
Ruby for Java Developers
 
Agile DSL Development in Ruby
Agile DSL Development in RubyAgile DSL Development in Ruby
Agile DSL Development in Ruby
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
 
Developing cross platform desktop application with Ruby
Developing cross platform desktop application with RubyDeveloping cross platform desktop application with Ruby
Developing cross platform desktop application with Ruby
 
Ruby and japanese
Ruby and japaneseRuby and japanese
Ruby and japanese
 
Ruby for C#-ers (ScanDevConf 2010)
Ruby for C#-ers (ScanDevConf 2010)Ruby for C#-ers (ScanDevConf 2010)
Ruby for C#-ers (ScanDevConf 2010)
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
 
ppt9
ppt9ppt9
ppt9
 
ppt18
ppt18ppt18
ppt18
 
name name2 n2
name name2 n2name name2 n2
name name2 n2
 

Similar to Ruby Programming Introduction

Ruby vs Java
Ruby vs JavaRuby vs Java
Ruby vs JavaBelighted
 
Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)SATOSHI TAGOMORI
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to heroDiego Lemos
 
Charla ruby nscodermad
Charla ruby nscodermadCharla ruby nscodermad
Charla ruby nscodermadnscoder_mad
 
Hacking with ruby2ruby
Hacking with ruby2rubyHacking with ruby2ruby
Hacking with ruby2rubyMarc Chung
 
Rails 3 Beautiful Code
Rails 3 Beautiful CodeRails 3 Beautiful Code
Rails 3 Beautiful CodeGreggPollack
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Henry S
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on RailsManoj Kumar
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Mark Menard
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentNicolas Ledez
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortegaarman o
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Railselliando dias
 
Hijacking Ruby Syntax in Ruby
Hijacking Ruby Syntax in RubyHijacking Ruby Syntax in Ruby
Hijacking Ruby Syntax in RubySATOSHI TAGOMORI
 
Puppet: Orchestration framework?
Puppet: Orchestration framework?Puppet: Orchestration framework?
Puppet: Orchestration framework?bodepd
 

Similar to Ruby Programming Introduction (20)

Ruby vs Java
Ruby vs JavaRuby vs Java
Ruby vs Java
 
Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
 
Ruby 20th birthday
Ruby 20th birthdayRuby 20th birthday
Ruby 20th birthday
 
Charla ruby nscodermad
Charla ruby nscodermadCharla ruby nscodermad
Charla ruby nscodermad
 
Hacking with ruby2ruby
Hacking with ruby2rubyHacking with ruby2ruby
Hacking with ruby2ruby
 
Rails 3 Beautiful Code
Rails 3 Beautiful CodeRails 3 Beautiful Code
Rails 3 Beautiful Code
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
 
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1
 
Babushka
BabushkaBabushka
Babushka
 
Setup ruby
Setup rubySetup ruby
Setup ruby
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirent
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortega
 
Ruby
RubyRuby
Ruby
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
 
Hijacking Ruby Syntax in Ruby
Hijacking Ruby Syntax in RubyHijacking Ruby Syntax in Ruby
Hijacking Ruby Syntax in Ruby
 
Puppet: Orchestration framework?
Puppet: Orchestration framework?Puppet: Orchestration framework?
Puppet: Orchestration framework?
 
Practical ngx_mruby
Practical ngx_mrubyPractical ngx_mruby
Practical ngx_mruby
 
REDStudy#1
REDStudy#1REDStudy#1
REDStudy#1
 

Ruby Programming Introduction

  • 1. A partial introduction to the ^ The Ruby Programming Language by Anthony W. Brown Tuesday, March 26, 13
  • 2. Introduction This presentation is an adaptation from “The Ruby Programming Language” by Matz and the University of Washington’s Ruby and Rails PCE course. This deck was originally intended to cover all object-oriented principles, popular tools and the Rails framework. If anyone likes this deck, I will create rest for you... Tuesday, March 26, 13
  • 3. Useful Links • http://www.ruby-doc.org/ • http://apidock.com/ruby • http://apidock.com/rspec • http://zenspider.com/Languages/Ruby/ quickref.html Tuesday, March 26, 13
  • 4. Origins “Ruby is simple in appearance, but is very complex inside, just like our human body” -Matz Tuesday, March 26, 13
  • 5. Ruby is... • Object Oriented • Dynamic • Reflective • Interpreted Tuesday, March 26, 13
  • 6. Ruby in a Slide • TDD • IRB • Gems • Bundler • Rspec Tuesday, March 26, 13
  • 7. Tools • git, github • RVM • Ruby Gems • RSpec • Rake • Guard Tuesday, March 26, 13
  • 8. Install Check awb - bash - 80x24 Last login: Sun Feb 10 11:28:48 macpro:~ awb$ git --version # Check git version macpro:~ awb$ ruby -v # Check Ruby version, 1.9.3? macpro:~ awb$ irb # Interactive Ruby Environment macpro:~ awb$ gem install rspec # Install the RSpec test tools Tuesday, March 26, 13
  • 9. git awb - bash - 80x24 Last login: Sun Feb 10 11:28:48 macpro:~ awb$ git config # set or view configuration macpro:~ awb$ git clone # copy repository macpro:~ awb$ git init # make current dir a repo macpro:~ awb$ git status # get current state or repo macpro:~ awb$ git add # add items to index macpro:~ awb$ git rm # remove items from index macpro:~ awb$ git commit # commit index to repo macpro:~ awb$ git push # move commits to remoter server macpro:~ awb$ git log # list of commits in this repo Tuesday, March 26, 13
  • 10. RSpec http://rubydoc.info/gems/rspec-expectations/2.4.0/RSpec/Matchers describe #describes “what” in a test context #describes “when” in a test it #performs an operation,“does” should #Class attribute eq #matcher be_a #matcher kind_of #matcher awb - bash - 80x24 Last login: Sun Feb 10 11:28:48 macpro:~ awb$ rspec -c -f d ‘filename’ Tuesday, March 26, 13
  • 11. Rake Rake is Ruby’s version of make: a domain specific language to define tasks. Easy to manage dependencies and tasks. task :clean_update do =begin system(clean_up_script.sh) Ruby version of make; A DSL to MyClass.update_everything(‘./ define tasks path’) Easy to manage dependencies and end tasks =end awb - bash - 80x24 Last login: Sun Feb 10 11:28:48 macpro:~ awb$ rake clean_update Tuesday, March 26, 13
  • 12. Objects Everything in Ruby is an object. “hello” #string 4 #integer [] #empty array my_array :symbol #comment =begin multi-line comment =end Tuesday, March 26, 13
  • 13. Operators - #Unary operators ! * ~ + - #Binary operators * / ** << && || my_variable? #Ternary operators ‘True’: Tuesday, March 26, 13
  • 14. Assignment Hold values, or references to objects. # lvalue are assignment,rvalue # are reference a = b #lvalue = rvalue a += 1 #abbreviated assignment same #as a = a + 1 a, b = 1, 2 # a = 1, b = 2 a, b, c = [1,2] # a = 1, b = 2, c = nil Tuesday, March 26, 13
  • 15. Expressions ‘hello’ =begin File Expressions evaluate to and return a value. true Compound expressions leverage false an operator (+/-/*/%) nil =end self Tuesday, March 26, 13
  • 16. Variables Variables hold values, or references to objects #initialize to prevent NameError a = “Woof!” b = a #b = “Woof!” b.object_id #b = “Woof!” c = a.dup #c = “Woof!” b[0] = “P” #a and b = “Poof!” c still #“Woof!” #Local: my_var #Global: $password #Class: @@counter #Instance: @name #Constant: MY_CONST Tuesday, March 26, 13
  • 17. Strings Text is represented by strings, which are of class String. ’35’ #single-quoted string literal “35” #double-quoted string literal my_age = “35” #Assigned string variable “ My age is #{my_age}” #Interpolation Tuesday, March 26, 13
  • 18. Numbers Ruby has 5 built-in classes for numbers in the standard library. 123456789 #Literal Objects 3.141519 #Class Hierarchy: 1,000,000,000,000 #Numeric #Integer #Fixnum #Bignum Tuesday, March 26, 13
  • 19. Arrays Arrays are lists of values that can be accessed by their position. my_array = [“1”, “2”, “3” =begin 1, 2, 3 Arrays are not typed, ordered, mutable, literal, and a, << “1”, “2”, “3” Enumerable.They contain many operators and methods like: my_array = [] + << * [] .push .pop Array.new .sort. .uniq! =end Tuesday, March 26, 13
  • 20. Hashes Hashes are key/value pairs. my_hash = {:a =>“1”, :b =>“2”} =begin my_hash = {} Hashes like arrays are not typed. They are ordered,and {a: 1, b: “2”} Enumerable.They contain many operators and methods like: my_hash = [:a] = 1 =end my_hash = [:b] = 2 Hash.new Tuesday, March 26, 13
  • 21. Iterators Like a loop, iterators perform operations on data sets. 3.times {puts “thank you!”} # my_data.each { |x| print x} # [1,2,3].map { |x| x*5} # Tuesday, March 26, 13
  • 22. Methods Methods pass messages to objects. They have 5 parts; the object, message, parameter, block and body. my_method #Various ways to call a method my_method parameter my_method (parameter) my_method (parameter1, 2) object.my_method my_method par1, par2, do| #Syntax structure block| tweet! “#{par1} just #object.method(parameter) {block} did #{par2}” end Tuesday, March 26, 13
  • 23. Method Object Objects receive messages. Every object has methods and self def my_method; end #dangerous methods end in ‘!’ object.respond_to? :my_method # => true Tuesday, March 26, 13
  • 24. Method Message The Message is required. Style is to use snake_case naming conventions. #predicate methods end in ‘?’ [].empty? # => true my_method.uniq #dangerous methods end in ‘!’ my_method.uniq! doesn’t modify the method does modify the method Tuesday, March 26, 13
  • 25. Method Message The Message is required. Style is to use snake_case naming conventions. #predicate methods end in ‘?’ [].empty? # => true #dangerous methods end in ‘!’ my_method.uniq #doesn’t modify the method my_method.uniq! #does modify the method def name = str #attribute setter method name end = @name = str end class MyObject #index lookup (-ish) methods use [] def [] num @collection[num] end end Tuesday, March 26, 13
  • 26. Method Parameters #0 or more, comma separated, #defaults can be defined. def say str1, str2, str3 “#{str1} --- #{str2}” end say ‘hi’, ‘bye’ # => ‘hi --- bye’ Tuesday, March 26, 13
  • 27. Blocks Nameless function (arbitrary code) passed to a method with {code} or ‘do code end’. Any method can receive a block. def my_method par =begin {|block_par| use block par} end Blocks to not have to be named as a parameter and are ignored if not explicitly used by a def my_dog method. The block passed print “I’m a dog...” describes what to do with each yield thing. end =end run {print “Bark, bark!”} # => I’m a dog. Bark, bark! # Tuesday, March 26, 13
  • 28. Blocks Example module Enumerable # Calling Enumerable def map # Create a method result = [] # Assign a variable self.each do |object| # Create a block result << yield object end result end end [1, 2, 3].map { |n| n * 2 } # => [2,4,6] Tuesday, March 26, 13
  • 29. More to come. If you have any questions, please contact me on various social media networks with the alias @awbrown. Tuesday, March 26, 13