Quick introduction to the Ruby programming language
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Peter J. Jones c9b2e5212b Track changes to devalot-slides il y a 5 ans
doc Finish basic presentation il y a 5 ans
script Finish basic presentation il y a 5 ans
src Fix a typo il y a 5 ans
vendor Track changes to devalot-slides il y a 5 ans
.gitignore Start working on the real content of the presentation il y a 5 ans
.gitmodules New presentation: Introduction to Ruby il y a 5 ans
GNUmakefile Track changes to devalot-slides il y a 5 ans
README.md Fix a typo il y a 5 ans
slides.md Finish basic presentation il y a 5 ans

README.md

Introduction

class You
  def ask_questions
    …
  end
end

class Me
  def keep_on_track
    …
  end
end
  • Slide Time: 00:02 (00:02)
  • Please ask questions at any time.
  • Your job is to understand the material.
  • My job is to keep us on track and on time.

30-second History of Ruby

Time.now + 30 * 60
  • When and where?
  • Influences.
  • “Modern” Ruby.

  • Slide Time: 00:02 (00:04)

  • First released in 1995 by Japanese Programmer “Matz”.

  • Syntax influenced by Perl (C-style).

  • Features influenced by Smalltalk and Lisp.

  • Modern Ruby began with v1.9 (2007)

  • Several Implementations, main is MRI.

What is Ruby?

ruby.is_a?(Language)     &&
  ruby.typing?(:dynamic) &&
  ruby.memory?(:gc)      # => true
  • Slide Time: 00:05 (00:09)
  • Purely object-oriented.
  • General purpose (not just web apps!)
  • Dynamically typed.
  • Garbage collected.

Ruby Syntax

class Person
  def initialize (first, last)
    @first_name = first
    @last_name  = last
  end

  def name
    @first_name + " " + @last_name
  end
end
character = Person.new("Sheldon", "Cooper")
character.name
  • Slide Time: 00:08 (00:17)
  • Perl-flavored, C-like syntax
  • Semicolons are optional (line-based)
  • Whitespace insignificant.
  • Constants use CamelCase
  • Variables and methods use snake_case.
  • Blocks of code are delimited by begin...end or do...end.
  • All methods return a value (no void functions).
  • Implicit return of final value.
  • The scope of a variable is determined by its name.

Everything is an Object

Consider this irb session:

>> 1.class
=> Fixnum
>> "Hello World".class
=> String
  • Slide Time: 00:02 (00:19)
  • Numeric literals are objects.
  • String literals are objects.

Everything is an Object: Assignment

Open irb and experiment with the following:

  • Numeric literals.
  • String literals.
  • true and false
  • (0..10).class

  • Slide Time: 00:06 (00:25)

Everything is an Expression

def create_logger
  logger = if $stdout.tty?
             Logger.new($stdout)
           else
             Logger.new("events.log")
           end
  …
end
  • Slide Time: 00:02 (00:27)
  • An if statement is really an expression.
  • Using if in string interpolation.
  • Class definition is an expression.
  • Method definition is an expression.
  • Loading a file is an expression.

Everything is an Expression: Assignment

Open irb and let’s play with some expressions together.

  • Slide Time: 00:05 (00:32)
  • if true then "hello" end
  • if false then "hello" end

Object-oriented Programming

class Customer < Person
  include(UniqueID)
  private(:make_unique_id)
end
module UniqueID
  def make_unique_id
    …
  end
end
  • Slide Time: 00:05 (00:37)
  • Ruby only allows single inheritance.
  • Multiple inheritance is emulated with modules.
  • Modules are not interfaces.

Everything is an Object (Part 2)

Consider this irb session:

>> String.class
=> Class
>> anon = Class.new do
?>   def hello; "hi"; end
>> end
>> object = anon.new
>> object.hello
=> "hi"
  • Slide Time: 00:05 (00:42)
  • Even classes are objects!
  • Creating classes at runtime.

Blocks and Iterators

list = ["Jasmine", "Lavender", "Lilac"]

# Long-form block.
list.each do |item|
  $stdout.puts("I like #{item}!")
end
  • Slide Time: 00:03 (00:45)
  • Blocks are borrowed from Smalltalk.
  • Blocks can be used as iterators (Array#each).

Blocks and Iterators: Assignment

Open irb and play with long- and short-form blocks.

list = ["Jasmine", "Lavender", "Lilac"]

# Long-form block.
list.each do |item|
  $stdout.puts("I like #{item}!")
end
# Short-form block.
list.each {|item| puts(item)}
  • Slide Notes: 00:08:00 (00:53:00)
  • Long form: do |x| ... end.
  • Short form: {|x| ... }.

Blocks and Resource Management

# Exception-safe file closing.
File.open("/dev/urandom") do |file|
  bytes = file.read(10)
  …
end
# Exception-safe database transactions.
transaction do
  record_a.save!
  record_b.save!
end
  • Slide Notes: 00:04 (00:57)
  • And also for resource management (File::open).
  • You can write methods which accept blocks.
  • Blocks can be passed around from method to method.

Closing

Open irb and ask some questions!

  • Slide Time: 00:03 (01:00)