Justin Mui

Justin Mui

Ruby Data Types (Part 2)

Slide Duration:

Table of Contents

Section 1: Introduction to Ruby
Setting Up Your Environment

22m 8s

Intro
0:00
Installing Ruby
0:06
Ruby-lan.org
0:07
Three Ways of Installing Ruby
2:26
Compiling Ruby-Source Code
3:02
Third Party Tools
3:28
Other Implementations of Ruby
4:48
Windows Installation
5:21
RubyInstaller.org
5:22
Mac OSX and Linux Installation
6:13
Mac OSX and Linux Installation
6:14
Setting Up Debian/Linux
6:42
Setting Up Debian/Linux
6:43
Installing HomeBrew
6:56
HomeBrew for MAC OSX
6:57
HomeBrew Wiki
9:44
Installing HomeBrew
10:02
Setting Up Mac OSX
11:46
HomeBrew, RVM, OSX-GCC Installer, and Install Ruby 1.9.3
11:47
Ruby Version Manager (RVM)
12:11
Ruby Version Manager (RVM) Overview
12:12
Installing Ruby Version Manager (RVM): http://rvm.io
12:35
Install RVM with Ruby
14:20
Install RVM with Ruby
14:21
Install OSX-GCC-Installer
16:18
Download and Install Package for Your OSX
16:19
Install Ruby 1.9.3
17:28
Install Ruby 1.9.3
17:29
Test It Out!
18:09
rvm-help & ruby-v
18:10
Example: rvm gemset create educator
18:52
Set It As Default!
20:47
rvm Use 1.9.3@educator--default
20:48
Intro to Ruby

22m 20s

Intro
0:00
What is Ruby?
0:06
What is Ruby?
0:07
Ruby Standard Library
0:52
Who Created Ruby?
1:22
Yukihiro Matsumoto
1:23
History
2:45
The Name 'Ruby'
2:46
Ruby v0.95
3:10
Ruby v1.0
3:56
English Language Mailing List Rubytalk
4:08
ruby-forum.com & the Mailing Lists
4:27
Ruby In The West
9:51
Ruby on Rails
10:39
The Pragmatic Programmer's Guide to Ruby
11:30
rubyonrails.org
13:34
Current Ruby
14:42
Ruby 1.8.7, Ruby 1.9.3, and Ruby 2.0
14:43
Why Programmers Enjoy Ruby?
15:40
Why Programmers Enjoy Ruby?
15:41
Ruby Is An Interpreted Language
16:21
Ruby Is An Interpreted Language
16:22
What Is It Used For?
16:50
What Is It Used For?
16:51
Ruby is Object-Oriented
18:17
Example: 5.class
18:18
Example: 0.0.class
18:54
Example: true.class
19:03
Example: nil.class
19:12
Object Class
19:19
BasicObject
19:20
Example
19:52
Superclass
20:50
Fixnum → Integer → Numeric → Object
21:32
Basic Tools for Using Ruby

27m 44s

Intro
0:00
Interactive Ruby
0:08
irb: Interactive Command-Line Environment
0:09
Example
0:49
irb-v
0:50
irb-executes terminal
1:02
1.9.3-p125 > 'hi'
1:09
Live Demonstration
1:31
Why Use Interactive Ruby?
2:21
Why Use Interactive Ruby?
2:22
RDoc
3:05
RDoc
3:06
Ruby Core Documentation
3:32
Ruby Core Documentation: Example
5:30
Ruby Core Documentation: Markup
6:12
Ruby Core Documentation: Headings
7:44
Coding Example: RDoc
9:30
Why Use RDoc?
13:02
Learning Core Ruby Functions
13:03
Generating RDoc
15:31
rdoc-help # usage
15:32
Ruby Interpreter
15:57
ruby -- help
15:58
ruby [switches] [-] program [arguments]
16:16
Example: How to Run a Ruby Script
16:28
Rake
18:38
Rake Overview
18:39
Ruby Core Documentation: Rake
19:46
Coding Example: Rake
23:14
Why Was It Created?
24:30
Why Was It Created?
24:31
Lesson Summary
25:13
Lesson Summary
25:14
IDE/script Editors: MacVIM
26:24
Ruby Specifics

20m 45s

Intro
0:00
Ruby Specifics
0:06
Comments
0:51
Hashtags
1:00
Example
1:23
Multi-Line Comment
2:04
Example
3:10
RDoc Comments
4:02
When do you generate an Rdoc?
4:10
Headings and subheadings
4:24
Examples
4:48
Generating an Rdoc - example
4:50
Common Code Conventions
6:28
For every tab use two spaces indentation
7:38
Never use tabs
7:42
Common Code Conventions (Cont.)
8:18
Camel case
8:20
Snake case
9:18
Identifiers
9:44
Constants begin with CAP letter
10:00
Examples
10:10
Identifiers with Different Scoping
10:26
Global
10:34
Instance Variable
10:40
Class Variable
10:46
Examples
10:56
Reserved Keywords
12:22
Do not use reserved keywords in code
12:25
Parentheses are Sometimes Optional
13:04
Functions do not require parentheses
13:16
When in doubt, use parentheses
13:54
Examples
14:10
Newlines Are Statement Terminators
14:20
Examples
15:10
Continuation with a Period
16:20
Period means continue to next line
16:46
Multiple Statements Allowed on a Single Line
17:38
Try not to use semi-colons
17:58
Code Blocks
18:20
Use code blocks for one liners
18:28
Examples
18:40
Recommended for multiple lines
20:16
Ruby Data Types (Part 1)

29m 37s

Intro
0:00
Overview
0:10
Ruby Data Types
0:10
Numbers
0:16
Strings
0:18
Symbols
0:24
Numbers
0:30
Numeric
0:44
Float
0:50
Complex
0:56
BigDecimal
0:58
Rational
1:00
Integer (most popular) - Fixnum and Bignum
1:06
Fixnum stores 31 bits
1:18
Bignum stores larger bits
1:24
All number objects are instances of Numeric
1:28
Integer Literals
2:28
Represent whole-numbers
2:40
Examples - Different bases
2:42
Binary
3:04
Octal
3:30
Hexadecimal
3:44
Examples
4:06
Floating Point Literals
4:45
Examples
4:58
e-value can be capital or lowercase
5:30
Example
5:44
Strings
6:16
Mutable objects
6:18
Used for inserting and deleting text, searching, and replacing
6:26
String Rdoc
6:46
Definition
7:00
String Literals
8:20
Single-Quoted
8:28
Double-Quoted (most used)
8:50
Example
9:32
Escape Sequences
11:10
Newline
11:16
Tab
11:22
Double quote
11:28
Blackslash
11:36
Interpolation
11:50
Sprintf
13:48
Unicode Escaping
14:38
Example
15:50
Delimiters
16:18
Here Documents
17:18
Example
17:25
String Operators
19:58
Concatenation
20:03
Appending
20:40
String Equality
21:04
Example
21:24
Substrings
22:00
Range object (inclusive)
22:22
String Encoding
24:52
Differences between Ruby 1.8 and 1.9
24:56
Symbols
26:02
Definitions
26:04
Examples
26:46
When to use symbols
26:54
Symbols and Strings
27:42
Symbols Rdoc
28:22
Ruby Gems

25m 50s

Intro
0:00
RubyGems
0:08
What are RubyGems?
0:24
RubyGems.org
0:44
How RubyGems are used
2:06
Java's jar utility
2:50
Unix/Linux's tar utility
3:06
What is a Gem?
3:16
Definition of Gem
3:20
Version
3:34
Date
3:44
Author
3:50
Description
5:58
What Are the Uses?
4:18
Uses for Gems
4:22
Installation
5:06
How to install RubyGems
5:30
Updating to the Latest Ruby Gems
5:54
Testing
6:22
Example
6:34
Installing Rake
7:24
Example
7:46
Verifying
9:22
Example
9:56
Structure
10:56
gem.gemspec
11:30
Specification
13:40
What is in the gem?
13:42
Who made it?
13:50
Update gem version
13:58
Example
14:10
Create Our First Gem
17:20
Steps involved
17:28
RubyGems Guides
17:36
Example
20:02
Steps Review
18:56
Create Our First Gem (Cont.)
23:08
Building the gem
19:38
Example
20:00
Installing the gem
22:32
Run it
22:52
Publish it
23:04
Get Some Gems!
25:06
rake
25:14
rails
25:19
fastercsv
25:25
koala
25:37
Ruby Data Types (Part 2)

40m 24s

Intro
0:00
Ruby Data Types
0:15
Boolean
0:21
Arrays
0:27
Hashes
0:33
Range
0:37
Boolean Types
0:42
TrueClass
0:56
FalseClass
1:12
NilClass
1:18
TrueClass Examples
2:48
FalseClass Examples
3:22
Arrays
4:16
Ordered collection of objects
4:22
Can hold different objects
4:32
Starts at index 0
4:50
Array of Strings
5:50
Example
5:52
Arrays (Cont.)
6:20
Can be created using literals
6:22
Can be created using constructors
6:54
Position and indexed value
8:04
Negative Indexed Values
8:56
Shift and Unshift
10:18
Push and Pop
11:38
.delete method
12:38
Addition and Subtraction
13:32
Union and Intersection
14:48
Insert
15:52
Iteration
16:52
Arrays Rdoc
17:48
Hashes
22:08
Maps and Associative Arrays
22:44
Created using the constructor
22:56
Created using a hash literal
24:02
Stored in a hash table
25:26
Example
25:50
Accessing Key-Values
27:46
Deletion
29:48
Iteration
31:04
Hashes Rdoc
32:04
Ranges
36:40
Two dots are inclusive
36:57
Three dots are exclusive
37:16
Example
37:50
Ranges Rdoc
38:24
Objects

1h 5m 46s

Intro
0:00
Objects
0:10
Object References
1:36
Ruby Core
2:16
Example
4:30
Creating New Objects
6:00
New Method
6:08
Initialize Method
6:31
Example
7:18
Garbage Collection
9:54
Global values always reachable
10:25
Object Identity
11:08
Every object has an object identifier
11:20
Object identifier is constant and unique
11:30
Example
11:54
Object Class
12:58
Class method
13:10
Superclass method
13:28
Object Testing
14:46
is_a?
15:49
respond_to?
16:26
String and Regexp
18:10
Comparing two object instances
20:06
Example
23:30
Object Equality
25:48
Comparing objects
25:54
equal?
25:58
Popular way to test for equality
27:16
Opposite way to test for equality
27:25
Arrays
28:30
Hash
29:42
Case equality operator
30:47
Class tests
31:16
Range tests
31:48
Symbol tests
32:32
Object Conversion
33:14
Explicit conversion
33:54
Implicit conversion
35:00
Example
36:12
Object Conversion: Kernel Module
38:22
Array
38:38
Float
39:26
Integer
39:58
String
40:10
Example
40:34
Object Conversion: Coerce
42:00
Used for mixed type numeric operations
42:08
Example
43:40
Object Conversion: Boolean
47:42
Every object has a boolean value
47:44
Example
48:54
Object Copying
50:52
dup
50:58
clone
51:03
Example
51:42
Object Freezing
57:36
Object Marshaling
58:38
Save state
59:04
Load state
59:27
Example
59:32
Tainted Objects
1:01:50
taint
1:02:08
farm field
1:02:12
Untrusted Objects
1:04:06
trust
1:04:24
untrust
1:04:34
untrusted?
1:04:42
Loops

38m 54s

Intro
0:00
Loops
0:12
while and until
0:48
for and in
0:54
iterators
1:04
enumerable in objects
1:06
While-loop
1:14
Will keep going is condition is true
1:18
Until-loop
2:58
Will keep going until condition becomes true
3:06
Single Expression Loops
4:20
Compact form
4:30
Expressed as a modifier
4:42
Do-While Loop
5:52
Executes body first
6:06
Do-Until Loop
7:54
Similar to do-while loop
7:58
Using Break Inside Loops
8:54
break
8:58
For-In Loop
11:56
for-loop
12:06
var
12:34
collection
12:54
body
13:00
Examples
13:08
Examples (Cont.)
15:54
Nested loops
16:40
Numeric Iterators
18:32
upto
18:40
downto
18:42
times
18:48
Examples
20:28
External Iterators
21:00
Enumerator class
21:04
Rdoc
21:16
Enumerables in Objects
24:35
Enumerable is a mix-in
24:41
RDoc
25:24
Commonly Used Enumerables in Objects
27:01
Array
27:19
Hash
27:51
Range
28:47
Examples
29:29
Enumerables in Objects (Cont.)
31:13
File Processing
31:15
Example
31:45
Enumerables in Objects (Cont.)
33:07
collect
33:23
select
34:11
reject
34:59
inject
35:29
Strings

28m 30s

Intro
0:00
Strings
0:08
Why do you want to get familiar with strings?
1:00
String Creation
1:16
new
1:28
empty?
1:50
length or size
2:10
Example
3:12
String Manipulation
4:40
slice
4:56
square brackets [ ]
5:02
token
5:40
[fixnum]
6:52
offset and length
8:40
chaining
12:42
String Insertion
12:56
insert
12:58
positive or negative index
13:46
String Updates
15:24
[token]
15:36
Examples
16:40
chop or chop!
17:54
chomp!
18:56
gsub
20:28
String Deletion
21:38
delete
21:38
String Reversal
22:46
reverse
22:52
String Manipulation
23:16
split(pattern=$, limit)
23:22
pattern
24:10
limit
24:15
upcase or upcase!
25:28
downcase or downcase!
26:02
swapcase
26:24
Incrementing Strings
27:26
next or next!
27:32
Check Out the Other Lessons
28:00
Ruby Data Types Part 1
28:12
Regular Expressions
28:18
Regular Expressions

33m 27s

Intro
0:00
Regular Expressions
0:10
How to create a regular expression
0:48
What goes inside
1:36
Metacharacters
3:10
Bracket expressions
3:14
Quantifiers
3:18
Anchors
3:20
Metacharacters
3:30
word and non-word characters
4:04
digit and non-digit characters
4:44
hexdigit and non-hexdigit characters
4:56
whitespace and non-whitespace characters
5:08
Examples
5:24
POSIX Bracket Expressions
7:48
Non-POSIX Bracket Expressions
9:48
Bracket Expression Examples
10:58
Quantifiers
12:34
Examples
13:30
Character Properties
17:24
Similar to POSIX bracket classes
18:22
More Character Properties
18:48
Examples
19:32
Anchors
20:08
Examples
21:14
Regular Expression Matching: Regexp Object
22:40
match
22:51
Regular Expression Matching: String Object
24:14
match
24:26
Regular Expression Modifier Characters
25:14
pat
25:38
Example
26:42
Regular Expression Modifier Objects
27:14
Example
28:38
Regexp Rdoc
30:40
Arrays

14m 35s

Intro
0:00
Arrays
0:12
Creating an Array with a Block
0:50
Alternative Ways to Create an Array
3:52
Checking the Class
5:14
Iterate through the array
5:26
Call the class method
5:28
Array Shortcuts
6:38
at(index)
6:44
delete_at(index)
7:28
first(n)
8:28
last(n)
9:28
Removing Duplicates
9:58
uniq or uniq!
10:04
Sorting the Array
10:48
sort or sort!
10:54
Getting the Index
11:35
index
11:56
rindex
12:38
Multidimensional Arrays
12:56
flatten
13:33
Check Out the Earlier Lesson
14:16
Ruby Data Types Part 2
14:26
Hashes

27m 48s

Intro
0:00
Hashes
0:12
Creating Hashes
1:18
Setting a Default Value
2:24
Accessing Hashes
4:16
Accessible by keys or by values
4:28
Keys must be unique
4:36
Creating Hashes
5:16
Comma-separated list
5:42
Hash rocket
8:28
Examples
10:16
Iterating Keys and Values
11:43
each_key
12:04
each_value
14:04
Merging Hashes
16:10
merge(other_hash)
16:20
Sorting Hashes
18:46
Replacing Hashes
20:57
replace(other_hash)
21:18
Converting Hashes to Other Classes
22:04
to_a
22:22
to_s
23:22
Example
24:34
Check Out the Earlier Lesson
27:22
Ruby Data Types Part 2
27:32
Math Operations, Part 1

28m 47s

Intro
0:00
Math Objects
0:12
Numeric
0:26
Integer
0:38
Float
1:02
Fixnum
1:14
Bignum
1:56
Rational
2:04
Math
2:24
Math Operations
2:36
Example
3:14
div(numeric)
4:54
divmod(numeric)
6:30
modulo(numeric)
7:23
quo(numeric)
8:18
remainder(numeric)
9:35
Operation Precedence 1 of 3
10:35
Operation Precedence 2 of 3
13:18
Operation Precedence 3 of 3
14:28
Abbreviated Math Operations
14:54
Move the operator in front of the equal sign
15:52
Numbers
16:36
Numeric Class
17:06
Numeric Methods
18:41
ceil
18:52
floor
19:32
round
19:50
Example with Numbers
20:20
Numeric Methods (Cont.)
22:20
truncate
22:28
num.step(limit, step)
23:02
Numeric Rdoc
25:26
Math Operations, Part 2

28m 51s

Intro
0:00
Math Operations
0:12
Math Module
0:24
Rational Numbers
0:44
Complex Numbers
0:52
Prime Numbers
0:58
Matrices
1:06
Math Module
1:12
PI and E
1:32
Math Module Methods
2:47
atan2(x,y)
2:56
cos(x)
3:14
exp(x)
3:44
Examples
4:38
log(x)
5:44
log(num, base)
6:34
log10(x)
7:04
sin(x)
7:34
sqrt(x)
7:52
tan(x)
8:06
Math Functions: Part 1 of 3
8:12
Math Functions: Part 2 of 3
9:32
Math Functions: Part 3 of 3
10:19
Math Module Rdoc
11:25
Rational Numbers
13:23
How to use
14:06
Example
15:02
Mathematical Ruby Scripts (Mathn)
16:25
Example
17:28
Complex Numbers
18:26
polar
18:56
rect
19:10
Complex Number Examples
19:18
Prime Numbers
20:14
each(ubound=nil)
20:44
prime?
21:22
Example
21:58
Matrices
23:15
build(row_size, column_size=row_size)
23:44
Example
24:44
Matrix Rdoc
24:58
Dates and Times

26m 1s

Intro
0:00
Dates and Times
0:12
Time Class
0:38
Methods of the Time Class
1:43
now
1:44
at(time)
2:10
Epoch & Unix Timestamp Conversion Tools
3:19
Components of a Time
5:07
Convert Time to an Array
5:54
to_a
6:08
Creating a New Time
6:48
Time.local
7:08
Year is required
7:22
Time.utc
8:12
What should be specified
9:30
More Methods of the Time Class
10:16
strftime(string)
11:26
RDoc
12:50
Date Library
16:46
Initializing a New Date
17:08
Parsing Dates
18:28
parse(string)
18:42
Today's Date
19:19
Date.today
19:22
Tomorrow's Date
20:22
Next
20:28
Next week
21:22
Count Down
21:26
Count Up
22:37
Components of a Date
23:20
Converting to Datetime
23:48
to_datetime
24:00
Initializing a Datetime
24:24
Converting to Time
25:23
self.to_time
25:32
Methods: Part 1

31m 24s

Intro
0:00
What is a Method?
0:12
Basic Method
0:58
Return Value
4:37
return
4:46
Factorial Example
6:18
Example
8:46
Return Two Values
10:06
Set the return keyword
10:14
Collected and returned as an array
10:28
Undefining Methods
11:22
undef method_to_undefine
11:44
Example
12:32
Method Names
13:02
Begin with lowercase letter
13:16
Separate longer words with underscores
13:26
Can end with equal sign, question mark, or exclamation point
14:03
Equal sign
14:26
Method Names with Question Mark
14:44
empty?
15:24
Method Names with Exclamation Point
16:01
mutators
16:12
! means use with caution
16:46
Method Aliases
18:05
alias new_method existing_method
18:42
Operator Methods
20:00
Operators
20:02
Array Operators
20:10
Unary Operators
20:32
Binary Operators
20:40
Example
21:28
Methods and Parentheses
25:00
Optional in most cases
25:20
Required in other cases
27:13
Methods and Blocks
27:54
Associated with blocks
28:18
block_given?
28:26
yield
28:36
Example
29:24
Methods: Part 2

20m 11s

Intro
0:00
Methods with the Unary Ampersand Operator
0:14
&
0:34
Block to a Proc
0:56
Example
2:02
Proc object
3:58
Example
5:04
Methods with Default Values
5:54
Example
7:12
Methods with variable-Length Arguments
8:05
How to create it
8:36
Example
11:06
Using Hashes with Arguments
13:02
Multiple arguments
13:08
Solution
13:30
Example
14:56
Rdoc
18:12
Classes: Part I

26m 51s

Intro
0:00
Classes
0:10
Definition of a class
0:14
Class represents a container
0:32
Can be reused
0:46
Creating our First Class
1:00
Keyword class will create new class
1:06
Name must begin with capital letter
1:30
Instantiating Our First Class
2:46
New will create a new instance of class
2:58
Initializing Values
3:58
Definition of def
4:14
Instance method
5:08
Example
7:02
Defining the to_s Method
8:24
Creating a string representation class
8:34
Example
10:54
Self in the Class
12:16
Definition of self
12:26
Example
13:54
Accessor Methods
15:52
getter methods
16:22
Example
17:00
Setter Methods
18:00
Mutator methods
18:02
Example
19:46
Automating Getter and Setter Methods
21:10
Defined in the module class
21:30
attr_reader
21:54
attr_writer
22:48
attr creates getter and setter methods
23:50
Example
24:28
Notes on Ruby's Accessor Methods
25:32
Classes: Part II

26m 42s

Intro
0:00
Defining Operators
0:10
You can define arithmetic operators
0:32
Unary Operators
0:46
Let's define operators in our class!
0:56
Example
2:52
Class Methods
6:24
Examples
6:56
Opening Up the Class
9:38
Adds an additional method
9:54
Examples
11:04
Array and Hash Access Method
15:40
Use square brackets
16:02
Define your own has access method
16:08
Example
16:56
Enumerating The Values
18:40
Define the each iterator
18:40
Testing for Equality
19:36
Class Triplex
19:50
Examples
20:54
Constants
25:00
Usually defined at the top of class
25:24
Classes: Part III

53m 36s

Intro
0:00
Class Variables
0:14
Example
2:16
Ruby Glass Jar Example
8:50
Class Instance Variables
10:20
Instance variables of class objects
10:46
Advantage of class instance variables
11:18
Examples
11:30
Method Visibility
16:16
Three types of method visibility
16:26
Public methods
17:34
Private methods
17:38
Protected methods
18:04
Invoking Method Visibility
19:21
Public , Protected, and Private Visibility
19:22
Invoking Method Visibility With Arguments
21:39
Example: Invoking Method Visibility
22:12
Class Visibility
23:31
Instance and Class Variables are Private
23:32
Constants are Public
24:00
Makes Existing Class Methods Private
24:27
Makes Existing Class Methods Public
25:08
Example: Class Visibility and class GlassJar
25:43
Subclassing
27:08
Subclassing: Subclass and Superclass
27:09
Example: Subclassing
29:43
Inheritance
30:05
Inheritance
30:06
Example: Inheritance
31:25
Subclassing and Inheritance
31:34
Descendants
31:41
Ancestors
31:56
More On Descendants and Ancestors
32:08
Extending a Class
33:27
Extending a Class
33:28
Coding Example: Extending a Class
34:24
Overriding a Method
36:41
Overriding a Method
36:42
Coding Example: Overriding a Method
37:18
Modifying Methods with Chaining
38:52
Modifying Methods with Chaining
38:53
Super
39:25
Coding Example: Modifying Methods with Chaining
39:51
The Singleton Pattern
44:52
Introduction to The Singleton Pattern
44:53
Setting Up Singleton
45:28
The Instance Method
45:58
Rdoc for Singleton: Usage
46:23
Rdoc for Singleton: Implementation
47:45
Coding Example: Singleton
49:38
Modules

24m 19s

Intro
0:00
Modules
0:04
What is Modules?
0:05
Modules Examples
0:40
Modules: Mix-Ins
3:31
What is a Mix-in?
3:32
Modules: Namespace
4:07
What is a Namespace?
4:08
Why Use a Namespace?
5:13
Example of a Namespace Module
5:59
Example of Mixing in The Module Into the Global Scope
6:00
Modules: Creation
7:04
How to Create a New Module?
7:05
Modules: Usage
8:19
How to Use It?
8:20
class Poker & class Bridge
9:13
Creating Our Module as a Mix-In
9:41
Example of a Module Using Instance Methods
9:42
Coding Example
10:20
Creating Our Module as a Namespace
12:11
Implement Class Methods for the Module
12:12
Coding Example
14:56
Loading Our Module
19:46
Loading Our Module Overview
19:47
Require & Load
20:15
Coding Example: Loading Module
20:48
Lesson Summary
23:36
Loading...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Ruby
Bookmark & Share Embed

Share this knowledge with your friends!

Copy & Paste this embed code into your website’s HTML

Please ensure that your website editor is in text mode when you paste the code.
(In Wordpress, the mode button is on the top right corner.)
  ×
  • - Allow users to view the embedded video in full-size.
Since this lesson is not free, only the preview will appear on your website.
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Ruby Data Types (Part 2)

Ruby Data Types (Part 2)

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Ruby Data Types 0:15
    • Boolean
    • Arrays
    • Hashes
    • Range
  • Boolean Types 0:42
    • TrueClass
    • FalseClass
    • NilClass
    • TrueClass Examples
    • FalseClass Examples
  • Arrays 4:16
    • Ordered collection of objects
    • Can hold different objects
    • Starts at index 0
  • Array of Strings 5:50
    • Example
  • Arrays (Cont.) 6:20
    • Can be created using literals
    • Can be created using constructors
    • Position and indexed value
    • Negative Indexed Values
    • Shift and Unshift
    • Push and Pop
    • .delete method
    • Addition and Subtraction
    • Union and Intersection
    • Insert
    • Iteration
    • Arrays Rdoc
  • Hashes 22:08
    • Maps and Associative Arrays
    • Created using the constructor
    • Created using a hash literal
    • Stored in a hash table
    • Example
    • Accessing Key-Values
    • Deletion
    • Iteration
    • Hashes Rdoc
  • Ranges 36:40
    • Two dots are inclusive
    • Three dots are exclusive
    • Example
    • Ranges Rdoc

Transcription: Ruby Data Types (Part 2)

Welcome back to Educator.com.0000

Today's lesson will be on Ruby datatypes, the second part.0002

We are going over four Ruby datatypes.0009

The first one will be Boolean.0013

We are going to be looking at true and false.0019

Next, we will take a look at arrays, then hashes, and then something that is pretty custom with Ruby--an object called Range.0023

First, Boolean types.0041

It's very similar to other languages--you have your true/false--but the way Ruby does it is different.0045

First, you have this true value; what I call True is actually using a class called TrueClass.0053

Everything is an object, even the Boolean values.0064

When I call false, it's going to cause the FalseClass, and you have your nil, which causes the NilClass.0071

They all refer to objects.0080

They also have a tendency to lean toward true.0087

What I mean is, for example--first, false is not the same as a zero--so it's not equal to zero.0097

In other languages, you might commonly see zero as a false value; if you pass, in an if statement, a zero, it's going to say it's true.0109

And also, at that same reasoning, the true is not equal to the one value.0121

This is the same as nil; nil is also not equal to zero.0136

We can also look at some of the RDoc to research...see here, nil and false behave the same.0141

Any other value behaves like true.0157

Here is the RDoc for the TrueClass.0166

It says up here it is an instance of class True; also, with the instance methods, if I have an object and I put in true, it can be true or false.0170

The string representation is true, also.0186

If you do a pipe, true, pipe, object, it will return true.0192

Now let's look at the False class; false is an instance of the False class.0201

false & obj returns false.0207

to_s also returns false.0210

There is also a pipe command in here.0214

false|&obj can return true or false, depending on the object itself.0217

It returns false if the object is nil or false, true otherwise.0223

If this said false or some other object, it would return true.0234

If that was nil or false, it would return false, though.0242

That is Boolean types; the next thing we're looking over is arrays.0255

First, they are an ordered collection of objects.0260

I can use any type of object in Ruby, and it can be stored into an array.0264

You can put in String objects, Integers, Fixnum, hashes, symbols...and it doesn't have to be the same type; you can have multiple types in one array.0273

Each array starts at the index 0.0288

Every value that you put in afterwards increments that index by one.0295

So, I put an array of letters here.0303

A, B, C...the first letter in this array will go in the 0 index.0311

B will go in the first index, and C goes in the second--the 2--index value.0330

Now, we are going to show an array of strings.0346

I have Cities, and it's an array that has three values: Los Angeles, San Francisco, and Sacramento.0349

You can call this--it's a method called .empty, and it will return false because that array has elements in it.0359

Arrays can also be created using literals.0376

Example: I have this [Orange, Apple, Pineapple]--this is the correct, proper way to do an array, but I also can use it with the %w%W, and this [Orange, Apple, Pineapple] will also give you this result with the array.0380

Arrays can be created using constructors; not only can I create them using the square brackets, I can use this array.new.0412

This number that you pass in will tell it how much space it should declare for it.0427

This is optional, so I could also just do array.new.0435

When I pass in this value, 3, what it's saying is, "This array is going to have three elements."0445

But I haven't specified what they are yet.0452

So, it's going to create this array, but they are going to be empty; we are going to have nil values in there.0454

The next thing: I can specify them myself.0461

Value 0: I put orange; value 1: I put Apple; and index 2: I put Pineapple.0464

Arrays are accessible by possession and index value.0485

As you've already seen, we put the 0 value as the first value; the last value is actually size minus 1.0490

For this example--Cities has a size of 3, so the last value would be 2.0502

Again,the last Cities, 2, will get us Sacramento.0520

Here are index values, but they can also go negative value, and that is what we are looking at here: negative index values.0537

The first value in this place is -size.0548

That would be our size with the minus, so that's -3.0554

The last value is -1.0562

In this case, you would see -1, -2, and -3.0567

Here are more examples of arrays.0577

For this one, we are going to use the Cities example again.0581

Cities[0] gets us Los Angeles.0586

If I do cities.first, it also gets us Los Angeles.0590

But then, we're going to use negative values.0595

Cities[-1] gets us the last element, which is Sacramento.0598

Cities.last also gets us Sacramento.0603

-2 will go back from Sacramento to San Francisco, the middle element.0608

The next thing we want to look at is the shift and unshift command.0617

With arrays, you will mostly use push and pop, but for shift and unshift--this works on the front of the array.0623

For this example, I have Years [2000, 2001, 2002].0633

When I unshift 1999, this is going to go to the front, so now we have an array with 1999 at the beginning and all the other array elements after that.0637

After I unshift 1998, it's going to push that in the front, too.0656

And then it's going to be in front of 1999, and we have all of the elements there.0664

The shift command will actually pull it out of the first array, so we're going to get back to this element here.0671

It's going to return the 1998 value.0682

The next one we're looking at is the push and pop.0693

This works on the back of an array; this is probably the most common method you have seen in the past.0698

What this does...we have this Numbers=array here, and first we're going to just put two elements in here.0706

So, we're using this less than, less than sign; it's going to put in a 1; less than, less than, equals is going to put in the 2; that's actually pushing the values in it.0718

Another way you can do it is just to use the .push method.0728

Again, you will see it push it to the last element--so it's 1,2,3.0733

And I can also just "pop" that value--notice the array is now 1,2--but this will also actually return the value "3".0739

Next, we're going to look at the delete method.0756

This delete method actually works on any of the array, not just the front.0760

It works on all the values.0773

For this one, you have this array [1,2,3,4,5], and, whatever object you put in here, it's going to look in this array for that object.0775

If it finds it, it's going to delete it.0787

Notice, after I do numbers.delete3, not only did it delete the value, it didn't create a new object.0790

It actually deleted it from this Numbers--so, if I call that Numbers value again, it's just not going to be in that element.0798

Addition and subtraction: here we have two arrays.0810

One with two elements...the other with two elements...notice there is now another array--that [1,2,3,4]--now this is a new array it's creating.0818

It's going to take those two, and with the addition sign, it's going to create a new array with that.0831

Now, if I use the concat function, this is not going to make an array; this is just going to append to the existing one.0838

This existing array, with 1,2, that is actually going to get two new elements to it.0856

And then, for this last part, you can subtract elements, too.0863

This will take the first array and subtract elements in this array.0869

This one is 1 and 3...it finds those...and then it returns a new array, which has 2 in it.0875

Now, let's look at union and intersection.0887

Here we have a union: you have array [1,2,3,4]; you have [2,3,4,5,6]; it creates a new array with all of them in it.0891

I believe you already know this, but how the union works is, you have these two elements; let's say this is A and this is B; and this is going to get elements from both of them.0901

That is why you get this cool thing here.0919

Intersection is different, so, as you see here, it's going to get the same elements that are in both of them.0922

In this case, it gets 2,3,4; again, if this is...we have this example of A; we have this example of B; it's just going to get this part here; and that is the intersection.0929

Next, we're going to look at the insert method in arrays.0951

This one takes two parameters: one is the index of where you want it to push it in.0956

You will see here that it says, "inserts the object before the element with the given index."0967

And here would be your object that you are pushing in that array.0974

For this example, we're going to push 1.5, and we're going to put that on the first index.0979

1.5...we're going to put it right here.0989

After you do that, you do get a 1, 1.5 goes in the second piece, then 2,3,4.0993

Arrays do allow iteration.1009

Here you have a couple of numbers, 1 through 9.1015

We're just calling this H, and we're doing a block that is going to print out these numbers; so we're just going to put 1,2,3,4, all the way to 9.1022

Notice, this num is actually the object that is in the array.1038

You can manipulate it and modify it the way you need to for your code, but that is what num is: it will iterate through each one, one by one.1046

Let's look at the RDoc so we can get a better feel of some of the more advanced things with arrays.1065

Here is our Array RDoc.1086

Here is our new constructor, array.new.1095

Notice that it does allow different arguments: you can pass an array's size and object with it.1101

This is an interesting one: they passed array.new: it takes two elements, but it also declares a hash for each one.1114

You can make each from that empty hash, versus an r; so we just did array.new with 3--default to nil objects.1124

Again, you have your try_convert for implicit conversion.1135

They do include the slice method, too, here.1156

You can--like we used the slice for the strings to get the substring--you can use slices with arrays, also.1160

You can also use this at method; what you do here is to tell it the index value.1181

You say, "Hey, this array, I want to call this method 'at'; I'm going to tell you the index value, and I want it to return the object"--so for this one, it says, "Give me what's at index 0," and it returns me the A string.1188

Another thing you can do is, with this method clear, you can remove all the elements from this array.1205

After I call 'clear', you will see no more objects in there.1211

Another popular one is these collect and map methods.1225

If you run it with an exclamation point, it will do itself, so it will update whatever array you currently have.1231

If you use collect and map, it will create a new object with that array, with the new elements in there.1237

For this example, a.collect, notice they have added an exclamation point; it says a!,b,c,d.1245

But what I really want to show you is the other ways you can iterate.1259

We already went through the each, but another useful thing is this each index method.1267

If I call each index, I can just get the index value back, depending on what I'm coding.1275

So, this value will include 0,1,2, to the array LinkSize, and it will print out the index: it says "0,1,2".1282

Then, you can specify that you want that array object, depending on the index.1296

Again, you can still use empty--if you have an empty array, it will return true--and it has their own 'equal' method.1306

That is the Array RDoc, and you can continue looking at that.1319

Next, we are going to look at hashes.1324

More advanced than arrays, but it allows you to do a lot more cool things, too.1328

Hashes are an unordered collection of key-value pairs.1334

It maintains a set of objects known as keys, and it associates a value with each key.1340

What is nice about Hashes is that they create their own table.1349

So, if you have multiple values, they can be using the same Hash code, and it's a more efficient use of space.1353

They are also known as maps or associative arrays.1364

Let's create a Hash using the constructor.1371

We have our constructor here, hash.new, and these are our keys, so 1 is a key, 2 is a key, and 3 is a key.1375

And we have our values here: value 1, value 2, and value 3.1392

Our Hash now has three keys, three values...and Ruby doesn't make a very nice way, depending on how you code...how you like to use it...to just put key, value, key, value, key, value--just specify it like an array.1398

Here it is: your Hash: you have your key 1, your value 1, your key with 2 string, value, key, and value.1416

This will create the same as this Hash up here, with the constructor hash.new.1427

You can also create Hashes using Hash literals.1439

Here, we have our Numbers Hash; this is another way of the exact same thing we did in the past.1443

Here we have Numbers 1,2,3--that's using our Hash literal.1451

The recommended way to do it is to use symbols.1459

Symbols are only one object; it's not going to create a dynamic new object; it's a more efficient use of space; so this is the recommended way we prefer.1463

Instead of strings, use the symbols as keys.1475

You will notice that this method up here and this one down here--these are both equal.1483

You can do it one way or the other.1492

This one down here is a more popular way of doing it, but this is still including symbols.1497

This will still be 1, 2, and 31504

And again, if you were to call it, it would just be Numbers[1], just like that.1511

They are stored in a Hash table, and they are accessible and modifiable through the Hash method.1524

I actually want to show you this in action--this Hash--because these are actually using the same Hash value.1534

Let's get our terminal opened.1545

I do Numbers=hash.new...I have to get in my IRB.1552

Numbers=hash.new...then I create my 2; here is my key; so that is my value 2; and then I do Numbers, 2, duplicate...1564

Now, we've created these keys and values; let's see what the Hash came up with for us.1583

You see, there is our Hash code; then I can see what the duplicate one is.1593

Again, they are the same.1601

But let's try doing it with some strings.1604

I do Numbers [2] again, this being equal to 2, and with 2_dup, I get 2, also.1607

Let's see if that is the same.1623

We have one over Hash codes...they are still the same.1627

But watch this: if I were to make a=2, b=2, these will be different objects.1631

Not only does the Hash have different keys, it's still referencing the same value.1644

Again, there are some more methods you can do with Hashes that I want to show you--other ways to access the key value.1665

Still with our same example: there is has_key: you can pass in the key--if it has it, it returns true; if not, it returns false.1671

If I run the method keys, it will actually return to me an array with all the keys in it.1684

So, in this hash, there is 1, 2, and 3.1691

I can also get all of the values by calling the values method.1697

It gives me all the values.1699

And you can also use this values_at method; and that says, "Hey, get me a key, and I'll get you the value for it."1703

The value is a 1, and it returns 1.1716

This is...values_at, and this will take a key.1722

The next one we want to look at is the select method.1740

Notice, when I call it, it takes a block with a key and value.1744

What this select method does is, it's like a filter.1751

You can give it the conditions, and, if these conditions hold true, it will return that key and value as part of the Hash.1758

In this case, we're saying, "Give me all the ones with a value that is greater than or equal to 2," so it looks at my Hash and it generates a new one that doesn't have the 1--just 2 and 3.1767

Now let's look at deletion.1785

Deletion is going to be very similar to erase, but instead of objects, it takes the key value.1789

So, for this one, I called delete with the key of 2, and it creates a new Hash with 1 and 3.1797

Now, when I call this delete_if, for this one I have to declare my block as a key value, and for this I say, "I want to delete it if the key value is equal to 3."1810

Look there: the end result is 1.1825

Also, notice that it's still not a new object; I'm still working off of this same Numbers Hash.1829

And, if I want to remove them all, Numbers.clear, and this Numbers.empty will return true.1840

Ignore this...that doesn't belong there.1849

Next, let's look at iteration.1861

The good thing about these Hashes is that a lot of these methods are already in Arrays, so...you learn one, you can learn another one pretty quickly.1863

Let's look at this iteration...we have our Cities...ZIP codes with the Cities...1874

I can use .each again, but unlike the Array, I'm passing two elements.1881

And then, I can do a puts, say, "This value is located at this ZIP code," so for example, for the first one, it would be, "Los Angeles is located at ZIP code 90001."1890

Let's take a look at the RDoc.1924

Let's see what other things we can do with this method.1927

We already saw this type of declaration, using kind of an array, where we're using key-value, key-value, and you can continue creating new Hashes through it.1939

Notice that it's not using new, either; it's a hash, and then square brackets, to declare that.1954

Then, we have our hash.new constructor.1962

This is interesting--the "equal equal"--you can actually check it with other Hashes.1971

It's smart enough to say, "Hey, if they have the same number keys, and the key-value pairs are equal, I can return true for this."1975

We can also look at this example here.1988

h2=h3, it's 1,1,2,2,35,35...notice that we declared it in a different order, but the Hash...there is no order to it; so they're going to be true.1991

Let's see if there are any other interesting things.2012

You can set your default value, so whenever a value is returned, if it didn't exist in the Hash, it will return to that default.2016

Pretty powerful stuff.2029

We saw that delete...there's our iteration...2033

We can also use each_key, so it will iterate through them and do it by key.2036

You can also do each_pair, which is actually the same as each, but some people like using each_pair better; they call the exact same code.2043

You declare the key and value for that.2056

There is a function called flatten; it will take your keys and values, your key-value, key-value, key-value, and it will flatten that to one array.2065

We have it here; you will notice that, in this example they give, they have an array in array--so it will flatten it, and you can set the level of how much you want to flatten it to.2075

Again, you have your has_key, you have include...there are some interesting ones here, like this invert; it allows you to take the Hash and invert it the other way.2091

Your length and size exist for this.2104

This is an interesting one: you have this method called merge, and you can merge them together.2108

The merge has precedence; so, if they have the same key, the one that is getting merged in will take precedence over the what is already there.2119

And again, they have their 'bang' to merge it to the existing object, or it's going to...without calling it with an exclamation, notice, it just creates a new one.2131

This one is pretty interesting--you need to know--it's this rehash: you want to do this in cases where you have objects that you are using as key--if anything changes in it, it's still using the same Hash table, but something in that object will change; so you need to rehash it, rebuild it.2144

So, they have arrays; they have them declared; and then they call this rehash method.2165

It rebuilds the whole Hash on the current values, but something did change, but the Hash is still using the one that was stored when this key and value was created.2174

That is Hashes.2193

The last one that we want to talk about is Ranges.2197

This one is pretty custom for Ruby.2200

It allows you to get values from start and end, without specifying all of them.2206

The first one we want to look at is inclusive.2213

In this example, all of the elements that have two dots--it's going to include all of those elements and the ones at the end.2217

This will include a range 1 to 10.2229

Now, if we look at the exclusive, which uses three dots, it's not going to include the end elements, so this is just going to be 2 through 9.2235

Notice here, we created a Range, and we asked it, "Hey, for this Range, does it include 10?"2246

That is true if it is inclusive, but on this example, it won't, because it is only 2 through 9.2255

Again, you can use Ranges with a lot of different arguments; you can even...when you create your own classes, you can use them, because they are already built in Ruby.2267

For example, we have this array; instead of typing it all out, I can just do the Range 0..9.2276

And look at that--it will create a whole array with the elements already in it.2284

You can also do that with letters.2289

I do a..z, and it will print out all of the letters for me.2291

Let's take a look at the RDoc.2302

We have our Range object.2309

It uses start and end arguments.2315

And also notice, they do have a conversion method, to_a, to convert it to Array.2321

You still have your constructor, with new.2337

There is an equality method...2341

And you still have your each block.2347

So, it has a lot of the same functionality as Arrays and Hashes, which is nice.2349

There aren't necessarily too many new things with this one.2366

This step function is pretty specific for Range, though.2373

"Iterates over range"...you pass each element to the block...if the range contains a number, it's added for each iteration.2379

You have your to_s, too.2395

That's the Range RDoc.2397

Well, that is Ruby datatypes, the second part.2404

We've gone through Ranges, Hashes, Arrays, Boolean types...2412

This completes the datatypes, but there is still a lot to go over, so I hope to see you for the next Educator.com lesson.2418

Educator®

Please sign in to participate in this lecture discussion.

Resetting Your Password?
OR

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Membership Overview

  • Available 24/7. Unlimited Access to Our Entire Library.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lecture slides for taking notes.
  • Track your course viewing progress.
  • Accessible anytime, anywhere with our Android and iOS apps.