Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Chapter Types

  • Loading branch information...
commit c89c3f4da19f66478921957e4eb4b80d215d9aff 1 parent e58b246
@weppos authored
View
2  types/00.md
@@ -0,0 +1,2 @@
+!SLIDE subsection
+# Data Types
View
28 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
View
13 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
View
101 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
View
2  types/04_fixnum.md
@@ -1,2 +0,0 @@
-!SLIDE
-# Fixnum
View
42 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"
View
5 types/05_array.md
@@ -1,5 +0,0 @@
-!SLIDE
-# Array
-
-!SLIDE
-# Enumerable
View
32 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
View
67 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"]
View
2  types/06_hash.md
@@ -1,2 +0,0 @@
-!SLIDE
-# Hash
View
32 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
View
2  types/07_symbol.md
@@ -1,2 +0,0 @@
-!SLIDE
-# Symbol
View
65 types/08_range.md
@@ -0,0 +1,65 @@
+!SLIDE subsection
+# Range
+
+
+!SLIDE
+# Range
+
+ @@@ Ruby
+ 2..4
+ # => 2..4
+
+ 2...4
+ # => 2...4
+
+
+ (2..4).to_a
+ # => [2, 3, 4]
+
+ (2...4).to_a
+ # => [2, 3]
+
+
+!SLIDE
+# Range
+
+ @@@ Ruby
+ (1..3).to_a
+ # => [1, 2, 3]
+
+ ("1".."3").to_a
+ # => ["1", "2", "3"]
+
+ ("A".."C").to_a
+ # => ["A", "B", "C"]
+
+ ("a".."c").to_a
+ # => ["a", "b", "c"]
+
+
+!SLIDE
+# Range
+
+ @@@ Ruby
+ (1..3).include?(2)
+ # => true
+
+ (1..3).include?(5)
+ # => false
+
+
+!SLIDE
+# Range
+
+ @@@ Ruby
+ (1..3).each do |counter|
+ p(counter)
+ end
+ # 1
+ # 2
+ # 3
+
+ (1..3).map do |counter|
+ Post.new(:id => counter)
+ end
+ # => [Post, Post, Post]
Please sign in to comment.
Something went wrong with that request. Please try again.