Ruby Primer

Blocks

Blocks in ruby are anonymous blocks of code passed to a method. In the following examples, we are passing a block of code to the each method of the players array. Blocks are delimited with either do ... end keywords or braces. The iterated value is set to the variable name in the special "goal posts" (eg |player| in the first example, |p| in the second). The variable name can be any legal variable name and is local to the block

Array Iteration with Each

Iterate through an array of player instances.


players.each do |player|
  puts "#{player.name} is a #{player.char_class} with a constitution of #{player.constitution}"
end

Iterate using the alternate brace syntax for block.


players.each {|p|  puts "#{p.name} is resting to regain health." }

Yield to my Authoriteh!

to be written

Regular Expressions

In ruby regular expressions are a built-in data type, which means you can write code like:


ignore_email if /bossy@palin2016.com/

In fact ruby case statements are built on regexp's like this:


cat_name = case email
  when /bossy@palin2016\.com/ then "Bossy Girrl"
  when /looseslots@CatsCasino\.con/ then "Lucky"
  when /inspector.*?@darkalleys\.org/ then "Seamus"
end

Two important notes about Ruby that can be confusing

"Hello There" =~ /Hello/

  1. The above expression returns 0. Which is the first position where /Hello/ matches.
  2. In a boolean expression 0 evaluates to true
  3. (In fact in ruby everything but nil evalutes as true.)

So, you can use code like this:


puts "hellooooo" if "Hello There, from Bossy!" =~ /Hello/

Classes

Classes in ruby have two main areas of functionality.

  • As a container for holding methods and instance variables

    
    class Cat
      attr_accessor :name, :eye_color, :coat_color
    
      def initialize(name, eye, coat)
        @name = name
        @eye_color = eye
        @coat_color = coat
      end
    
      def to_s
       "#{@name} has #{@eye_color} eyes and a #{@coat_color} coat!"
      end
    end
    

  • As a factory for creating new instances of the class.

    
    lucky = Cat.new("Lucky", "Green", "Black")
    puts lucky
    

    Modules

    Modules vs Classes

    • Modules in ruby are containers for holding methods and constants.
    • Classes also are containers, but add a Factory functionality

    You can create a new instance of a document class with:

    report = Document.new

    With Modules, you can only call methods on the main module or get constants. Just place to colons after the module name.

    
    require 'Pet'
    
    vet_phone = Pet::Vet.phone
    

    Modules of Modules

    You can use modules to hold other classes and modules as well.

    
    module Pet
      class Cat
        attr_accessor :name, :eye_color, :coat_color
        def initialize(name, eye, coat)
          @name = name
          @eye_color = eye
          @coat_color = coat
        end
        def to_s
         "#{@name} has #{@eye_color} eyes and a #{@coat_color} coat!"
        end
      end
    end
    
    bossy = Pet::Cat.new("Bossy", "Gold", "Brown")
    puts bossy
    

    Module level utility functions

    
    module Cats
      def self.call_to_dinner
         "Here Kitty, Kitty, Kitteh!"
      end
    end
    
    Cats.call_to_dinner
    
  • Tags: