Browse Source

Finish basic presentation

master
Peter J. Jones 5 years ago
parent
commit
23f452df93
8 changed files with 395 additions and 23 deletions
  1. 144
    6
      README.md
  2. 10
    10
      doc/plan.org
  3. 4
    2
      script/irb.sh
  4. 98
    4
      slides.md
  5. 77
    0
      src/blocks.rb
  6. 26
    0
      src/everything.rb
  7. 35
    0
      src/oop.rb
  8. 1
    1
      src/syntax.rb

+ 144
- 6
README.md View File

@@ -16,6 +16,7 @@ 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.
@@ -34,6 +35,7 @@ Time.now + 30 * 60



- 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.
@@ -52,6 +54,7 @@ ruby.is_a?(Language) &&



- Slide Time: 00:05 (00:09)
- Purely object-oriented.
- General purpose (not just web apps!)
- Dynamically typed.
@@ -69,7 +72,7 @@ class Person
end

def name
@first_name + ' ' + @last_name
@first_name + " " + @last_name
end
end
~~~~
@@ -81,6 +84,7 @@ character.name



- Slide Time: 00:08 (00:17)
- Perl-flavored, C-like syntax
- Semicolons are optional (line-based)
- Whitespace insignificant.
@@ -95,35 +99,94 @@ character.name

# Everything is an Object

Consider this `irb` session:

~~~~ {.ruby exec="script/irb.sh"}
irb(main):001:0> 1.class
>> 1.class
=> Fixnum
irb(main):002:0> "Hello World".class
>> "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

~~~~ {.ruby include="src/everything.rb" token="create_logger"}
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

~~~~ {.ruby include="src/oop.rb" token="customer"}
class Customer < Person
include(UniqueID)
private(:make_unique_id)
end
~~~~

~~~~ {.ruby include="src/oop.rb" token="unique"}
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.
@@ -132,21 +195,94 @@ irb(main):002:0> "Hello World".class

# Everything is an Object (Part 2)

Consider this `irb` session:

~~~~ {.ruby exec="script/irb.sh"}
>> 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
# Blocks and Iterators

~~~~ {.ruby include="src/blocks.rb" token="it-long"}
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`).
- And also for resource management (`File::open`).



# Blocks and Iterators: Assignment

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

~~~~ {.ruby include="src/blocks.rb" token="it-long"}
list = ["Jasmine", "Lavender", "Lilac"]

# Long-form block.
list.each do |item|
$stdout.puts("I like #{item}!")
end
~~~~

~~~~ {.ruby include="src/blocks.rb" token="it-short"}
# 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

~~~~ {.ruby include="src/blocks.rb" token="resource"}
# Exception safe file closing.
File.open("/dev/urandom") do |file|
bytes = file.read(10)
end
~~~~

~~~~ {.ruby include="src/blocks.rb" token="trans"}
# 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.

@@ -154,8 +290,10 @@ irb(main):002:0> "Hello World".class

# Closing

Open `irb` and ask some questions!



- Open IRB and ask questions.
- Slide Time: 00:03 (01:00)



+ 10
- 10
doc/plan.org View File

@@ -12,14 +12,14 @@
:END:
** Introduction
:PROPERTIES:
:Effort: 02:00
:Effort: 00:02
:END:
- Ask questions at any time
- Your job is to understand
- My job is to keep us on time
** 30-second History of Ruby
:PROPERTIES:
:Effort: 02:00
:Effort: 00:02
:END:
- First release: 1995
- Syntax influenced by Perl
@@ -28,7 +28,7 @@
- Several implementations, main is MRI
** What is Ruby?
:PROPERTIES:
:Effort: 05:00
:Effort: 00:05
:END:
- Perl-flavored, C-like syntax (semicolons optional)
- Purely object-oriented
@@ -36,7 +36,7 @@
- Garbage collected
** Ruby Syntax
:PROPERTIES:
:Effort: 08:00
:Effort: 00:08
:END:
- Example Ruby code
- Naming scheme
@@ -44,21 +44,21 @@
- Implicit return value
** Everything is an Object
:PROPERTIES:
:Effort: 05:00
:Effort: 00:08
:END:
- Open IRB:
- Play with numeric literals
- String literals: double quotes
** Everything is an Expression
:PROPERTIES:
:Effort: 10:00
:Effort: 00:07
:END:
- Open IRB:
- Play with `if`
- String interpolation
** Object-Oriented Programming
:PROPERTIES:
:Effort: 05:00
:Effort: 00:05
:END:
- Example code
- Single inheritance
@@ -67,12 +67,12 @@
- Variable names indicate their scope.
** Everything is an Object (Part 2)
:PROPERTIES:
:Effort: 05:00
:Effort: 00:05
:END:
- `Array.class`
** Blocks
:PROPERTIES:
:Effort: 15:00
:Effort: 00:15
:END:
- Example code (`Array#each`)
- Example code (`File::open`)
@@ -83,6 +83,6 @@
- You can write methods which accept blocks
** Closing
:PROPERTIES:
:Effort: 03:00
:Effort: 00:03
:END:
- Open Q&A in IRB

+ 4
- 2
script/irb.sh View File

@@ -1,4 +1,6 @@
#!/bin/sh

irb -f --noreadline -Isrc --prompt-mode=default $@ \
| sed -e '1d' -e '$d' # Damn IRB includes 2 extra lines!
# Damn IRB includes 2 extra lines!
irb -f --noreadline -Isrc --prompt-mode=simple $@ \
| sed -e '1d' -e '$d' \
| sed 's/;[ ]*nil//g' | grep -v -- '=> nil'

+ 98
- 4
slides.md View File

@@ -1,5 +1,5 @@
% Introduction to Ruby
% Peter Jones <pjones@devalot.com>
% Peter Jones \<pjones@devalot.com\>
%

# Introduction
@@ -9,6 +9,7 @@

<div class="notes">

* 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.
@@ -26,6 +27,7 @@

<div class="notes">

* 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.
@@ -41,6 +43,7 @@

<div class="notes">

* Slide Time: 00:05 (00:09)
* Purely object-oriented.
* General purpose (not just web apps!)
* Dynamically typed.
@@ -57,6 +60,7 @@

<div class="notes">

* Slide Time: 00:08 (00:17)
* Perl-flavored, C-like syntax
* Semicolons are optional (line-based)
* Whitespace insignificant.
@@ -71,6 +75,8 @@

# Everything is an Object

Consider this `irb` session:

~~~ {.ruby exec="script/irb.sh"}
1.class
"Hello World".class
@@ -78,26 +84,65 @@

<div class="notes">

* Slide Time: 00:02 (00:19)
* Numeric literals are objects.
* String literals are objects.

</div>

# Everything is an Object: Assignment

Open `irb` and experiment with the following:

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

<div class="notes">

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

</div>

# Everything is an Expression

~~~ {.ruby include="src/everything.rb" token="create_logger"}
~~~

<div class="notes">

* 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.

</div>

# Everything is an Expression: Assignment

Open `irb` and let's play with some expressions together.

<div class="notes">

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

</div>

# Object-oriented Programming

~~~ {.ruby include="src/oop.rb" token="customer"}
~~~
~~~ {.ruby include="src/oop.rb" token="unique"}
~~~

<div class="notes">

* Slide Time: 00:05 (00:37)
* Ruby only allows single inheritance.
* Multiple inheritance is emulated with modules.
* Modules *are not* interfaces.
@@ -106,21 +151,68 @@

# Everything is an Object (Part 2)

Consider this `irb` session:

~~~ {.ruby exec="script/irb.sh"}
String.class
anon = Class.new do
def hello; "hi"; end
end;nil
object = anon.new;nil
object.hello
~~~

<div class="notes">

* Slide Time: 00:05 (00:42)
* Even classes are objects!
* Creating classes at runtime.

</div>

# Blocks
# Blocks and Iterators

~~~ {.ruby include="src/blocks.rb" token="it-long"}
~~~

<div class="notes">

* Slide Time: 00:03 (00:45)
* Blocks are borrowed from Smalltalk.
* Blocks can be used as iterators (`Array#each`).
* And also for resource management (`File::open`).

</div>

# Blocks and Iterators: Assignment

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

~~~ {.ruby include="src/blocks.rb" token="it-long"}
~~~

~~~ {.ruby include="src/blocks.rb" token="it-short"}
~~~

<div class="notes">

* Slide Notes: 00:08:00 (00:53:00)
* Long form: `do |x| ... end`.
* Short form: `{|x| ... }`.

</div>

# Blocks and Resource Management

~~~ {.ruby include="src/blocks.rb" token="resource"}
~~~

~~~ {.ruby include="src/blocks.rb" token="trans"}
~~~

<div class="notes">

* 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.

@@ -128,9 +220,11 @@

# Closing

Open `irb` and ask some questions!

<div class="notes">

* Open IRB and ask questions.
* Slide Time: 00:03 (01:00)

</div>


+ 77
- 0
src/blocks.rb View File

@@ -0,0 +1,77 @@
#!ruby -w

################################################################################
require('minitest/autorun')

################################################################################
class BlocksTest < MiniTest::Unit::TestCase

##############################################################################
def block_iterator
# <<: it-long
list = ["Jasmine", "Lavender", "Lilac"]

# Long-form block.
list.each do |item|
$stdout.puts("I like #{item}!")
end
# :>>

# <<: it-short
# Short-form block.
list.each {|item| puts(item)}
# :>>

list
end

##############################################################################
def test_it
list = block_iterator
assert(list.is_a?(Array))
assert_equal(3, list.size)
end

##############################################################################
def block_resource
# <<: resource
# Exception safe file closing.
File.open("/dev/urandom") do |file|
bytes = file.read(10)
# ...
end
# :>>
end

##############################################################################
def test_resource
bytes = block_resource
assert(bytes.is_a?(String))
end

##############################################################################
# Fake.
def transaction
yield
end

##############################################################################
def test_trans
record_a = MiniTest::Mock.new
record_a.expect(:save!, nil, [])

record_b = MiniTest::Mock.new
record_b.expect(:save!, nil, [])

# <<: trans
# Exception safe database transactions.
transaction do
record_a.save!
record_b.save!
end
# :>>

record_a.verify
record_b.verify
end
end

+ 26
- 0
src/everything.rb View File

@@ -0,0 +1,26 @@
#!ruby -w

################################################################################
require('minitest/autorun')
require('logger')

################################################################################
class EverythingTest < MiniTest::Unit::TestCase

##############################################################################
# <<: create_logger
def create_logger
logger = if $stdout.tty?
Logger.new($stdout)
else
Logger.new("events.log")
end
# ...
end
# :>>

##############################################################################
def test_create_logger
assert(create_logger.is_a?(Logger))
end
end

+ 35
- 0
src/oop.rb View File

@@ -0,0 +1,35 @@
#!ruby -w

################################################################################
require('minitest/autorun')

################################################################################
class OOPTest < MiniTest::Unit::TestCase

##############################################################################
class Person; end

##############################################################################
# <<: unique
module UniqueID
def make_unique_id
# ...
end
end
# :>>

##############################################################################
# <<: customer
class Customer < Person
include(UniqueID)
private(:make_unique_id)
end
# :>>

##############################################################################
def test_customer
c = Customer.new
assert(c.is_a?(Person))
assert(c.private_methods(false).include?(:make_unique_id))
end
end

+ 1
- 1
src/syntax.rb View File

@@ -15,7 +15,7 @@ class SyntaxTest < MiniTest::Unit::TestCase
end

def name
@first_name + ' ' + @last_name
@first_name + " " + @last_name
end
end
# :>>

Loading…
Cancel
Save