seven-languages-in-seven-weeks/slides/ruby.org

3.6 KiB

Seven Languages in Seven Weeks

Introduction

Introduction

Ruby   BMCOL

Created
~1993
Author
Yukihiro Matsumoto

Interpreted, object-oriented, and dynamically typed.

Mary Poppins   BMCOL

/github/seven-languages-in-seven-weeks/media/commit/5935a5e6d36b047db156500c6de8d3e3b60cbe89/slides/mary_poppins_remake_by_gali_miau-d4nyzkc.jpg

Example

Source

  properties = ['object oriented', 'duck typed',
                'productive', 'fun']
  properties.each {|property| puts "Ruby is #{property}."}

Output

Ruby is object oriented.
Ruby is duck typed.
Ruby is productive.
Ruby is fun.

Day 1

Day 1: Finding a Nanny   B_frame

  • Object-Oriented
  • Simple Conditionals
  • Strong / Dynamic Typing
  • Duck Typing

Duck Typing

Source

  i = 0
  a = ['100', 100.0]
  while i < 2
    puts a[i].to_i
    i = i + 1
  end

Output

100
100

Exercises

EXERCISES

Day 2

Day 2: Floating Down from the Sky

  • Defining Functions
  • Arrays
  • Hashes
  • Code Blocks and Yield
  • Running Ruby from a File
  • Defining Classes
  • Writing a Mixin
  • Modules, Enumerable, and Sets

Defining Functions

  def tell_the_truth
    true
  end

Code Blocks

Source

  class Fixnum
    def my_times
      i = self
      while i > 0
        i = i - 1
        yield
      end
    end
  end

  3.my_times {puts 'mangy moose'}

Results

mangy moose
mangy moose
mangy moose

Classes

/github/seven-languages-in-seven-weeks/media/commit/5935a5e6d36b047db156500c6de8d3e3b60cbe89/slides/ruby-metamodel.png

Exercises

EXERCISES

Day 3

Day 3: Serious Change

  • Open Classes
  • method_missing
  • Modules

method_missing

Source   BMCOL

  class Roman
    def self.method_missing name, *args
      roman = name.to_s
      roman.gsub!("IV", "IIII")
      roman.gsub!("IX", "VIIII")
      roman.gsub!("XL", "XXXX")
      roman.gsub!("XC", "LXXXX")

      (roman.count("I") +  
       roman.count("V") * 5 +
       roman.count("X") * 10 + 
       roman.count("L") * 50 +
       roman.count("C") * 100)
    end
  end

  puts Roman.X
  puts Roman.XC
  puts Roman.XII
  puts Roman.X

Results   BMCOL

10
90
12
10

Exercises

EXERCISES

Wrapping Up

Wrapping Up: Core Strengths

  • Scripting
  • Web Development
  • Time to Market

Wrapping Up: Weaknesses

  • Performance
  • Concurrency and OOP
  • Type Safety

Final Thoughts

Syntax and flexibility vs. performance.