Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Commit

Permalink
Chapter Types
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Feb 25, 2011
1 parent e58b246 commit c89c3f4
Show file tree
Hide file tree
Showing 13 changed files with 381 additions and 12 deletions.
2 changes: 2 additions & 0 deletions types/00.md
@@ -0,0 +1,2 @@
!SLIDE subsection
# Data Types
28 changes: 27 additions & 1 deletion types/01_nil.md
@@ -1,2 +1,28 @@
!SLIDE subsection
# `nil`


!SLIDE
# nil
# `nil`

@@@ Ruby
# nil is the Ruby value for null
var = nil

# Check if the value is nil
var.nil?
# => true

# Alternative version, almost never used
var == nil
# => true


!SLIDE
# `nil`

@@@ Ruby

# nil evaluates to false
nil ? true : false
# => false
13 changes: 13 additions & 0 deletions types/02_boolean.md
@@ -1,2 +1,15 @@
!SLIDE subsection
# Boolean


!SLIDE
# Boolean

@@@ Ruby
var = true

if var
# var is true
else
# var is false
end
101 changes: 101 additions & 0 deletions types/03_string.md
@@ -1,2 +1,103 @@
!SLIDE subsection
# String


!SLIDE
# String

@@@ Ruby
var = 'string'
var = "string"

var = %q{string}
var = %Q{string}

var = <<-EOS
long
string
EOS


!SLIDE
# String
## The Ruby way

@@@ Ruby
# Rubyists do not use \ to escape
# because it makes the code hard to read
var = "<a href=\"\#hello\">I'm a tag</a>"

# They prefer a more natural syntax
var = %q{<a href="#hello">I'm a tag</a>}


!SLIDE
# String
## Interpolation

@@@ Ruby
who = "world"

var = 'hello #{who}'
# => hello #{who}

var = "hello #{who}"
# => hello world

var = %q{hello #{who}}
# => hello #{who}

var = %Q{hello #{who}}
# => hello world


!SLIDE
# String
## Escape sequences

@@@ Ruby

var = 'hello\nworld'
# => hello\nworld

var = "hello\nnworld"
# => hello
world

var = %q{hello\nnworld}
# => hello\nnworld

var = %Q{hello\nnworld}
# => hello
world


!SLIDE
# String
## % Notation

@@@ Ruby

"I love %s" % "Ruby"
# => "I love Ruby"

"I prefer %s over %s" % ["Ruby", "Java"]
# => "I prefer Ruby over Java"

vars = { :a => "Ruby", :b => "Java" }
"I prefer %{a} over %{b}. I love %{a}!" % vars
# => "I prefer Ruby over Java. I love Ruby!"


!SLIDE
# String

@@@ Ruby

# empty string evaluates to true
"" ? true : false
# => true

# not-empty string evaluates to true
"string" ? true : false
# => true
2 changes: 0 additions & 2 deletions types/04_fixnum.md

This file was deleted.

42 changes: 42 additions & 0 deletions types/04_symbol.md
@@ -0,0 +1,42 @@
!SLIDE subsection
# Sumbol


!SLIDE
# Symbol

@@@ Ruby
var = :foo

var
# => :foo

var.to_s
# => "foo"


!SLIDE
# Symbol

@@@ Ruby
:foo == :"foo"
# => true

:foo == "foo".intern
# => true

:foo == "foo".to_sym
# => true


!SLIDE
# Symbol

@@@ Ruby
var = "Simone"

var.respond_to?(:upcase)
# => true

var.send(:upcase)
# => "SIMONE"
5 changes: 0 additions & 5 deletions types/05_array.md

This file was deleted.

32 changes: 32 additions & 0 deletions types/05_number.md
@@ -0,0 +1,32 @@
!SLIDE subsection
# Number


!SLIDE
# Number

@@@ Ruby
# Fixnum
1
-1

# Float
1.0

# The following two objects are equal
1000000
1_000_000


!SLIDE
# Number

@@@ Ruby

# 0 evaluates to true
0 ? true : false
# => true

# not-0 evaluates to true
10 ? true : false
# => true
67 changes: 67 additions & 0 deletions types/06_array.md
@@ -0,0 +1,67 @@
!SLIDE subsection
# Array


!SLIDE
# Array

@@@ Ruby
var = Array.new
# => []

var = ["foo", "bar", "baz"]
# => ["foo", "bar", "baz"]

var = [
"foo",
"bar",
"baz",
]
# => ["foo", "bar", "baz"]

var = %w( foo bar baz )
# => ["foo", "bar", "baz"]


!SLIDE
# Array

@@@ Ruby
var = ["foo", 0, "baz"]

var = ["foo", ["bar", "baz"]]


!SLIDE
# Array

@@@ Ruby
var1 = %w( yellow orange )
var2 = %w( red yellow )

var1.size
# => 2

var1[1] = "blue"
# => blue

var1 + var2
# => ["yellow", "blue", "red", "yellow"]


!SLIDE
# Enumerable

@@@ Ruby
var1 = %w( yellow orange red )

var1.each { |color| p(color) }
# => yellow
# => orange
# => red

var1.map { |color| color.upcase }
# => ["YELLOW", "ORANGE", "RED"]

var1.select { |color| color.size == 3 }
# => ["red"]
2 changes: 0 additions & 2 deletions types/06_hash.md

This file was deleted.

32 changes: 32 additions & 0 deletions types/07_hash.md
@@ -0,0 +1,32 @@
!SLIDE subsection
# Hash


!SLIDE
# Hash

@@@ Ruby
var = {}
# => {}

var = { "foo" => 1, "bar" => 2 }
var = { :foo => 1, :bar => 2 }


!SLIDE
# Hash

@@@ Ruby
var = { :foo => 1, :bar => 2 }

var[:baz]
# => nil

var[:foo]
# => 1

var[:foo] = 2
# => 2

var["foo"]
# => nil
2 changes: 0 additions & 2 deletions types/07_symbol.md

This file was deleted.

0 comments on commit c89c3f4

Please sign in to comment.