Justin Mui

Justin Mui

Modules

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

Lecture Comments (1)

0 answers

Post by Emanuel Fonseca on May 8, 2017

Im doing turning machines and I could not find anything on this website to help me could you please help me with this problem

Unless otherwise stated, M and N are TM’s, G and H are CFG’s; The alphabet ? is {0.1}. Decide whether each of the following sets is (A) computable, (B) c.e. but not computable or (C) not c.e.

I think its C not C.E

{?G?: L(G) is infinite.}

Modules

  • A module contains a named group of methods, constants, and class variables
  • a Module object is an instance of the Module class
  • It cannot be instantiated or subclasses
  • It is commonly used for namespaces and mix-ins
  • RDoc: http://www.ruby-doc.org/core-1.9.3/Module.html
  • A "mix-in" allows methods from the included module to become instance methods in the class
  • A namespace is a set of names like method names and constants that have scope or context
  • A module is one form of namespace
  • It prevents namespace collisions with other common methods and allows you to group related methods together
  • To create a module, you must start with an uppercase letter for the module name
  • Modules can be included in a class or inherited from another module
  • To load your module, use 'require' or 'load' keyword

Modules

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
  • Modules 0:04
    • What is Modules?
    • Modules Examples
  • Modules: Mix-Ins 3:31
    • What is a Mix-in?
  • Modules: Namespace 4:07
    • What is a Namespace?
    • Why Use a Namespace?
  • Example of a Namespace Module 5:59
    • Example of Mixing in The Module Into the Global Scope
  • Modules: Creation 7:04
    • How to Create a New Module?
  • Modules: Usage 8:19
    • How to Use It?
    • class Poker & class Bridge
  • Creating Our Module as a Mix-In 9:41
    • Example of a Module Using Instance Methods
    • Coding Example
  • Creating Our Module as a Namespace 12:11
    • Implement Class Methods for the Module
    • Coding Example
  • Loading Our Module 19:46
    • Loading Our Module Overview
    • Require & Load
    • Coding Example: Loading Module
  • Lesson Summary 23:36

Transcription: Modules

Welcome back to Educator.com.0000

Today's lesson is on modules.0002

What are modules? They contain a named group of methods, constants, and class variables--it's kind of like a class.0006

A module object is an instance of the Module class; it's similar to a class, but it cannot be instantiated or subclassed.0021

So, here is one difference we can see between those two.0030

It is used for namespaces, and mixins.0035

Let's get a little more into what this Module is; let's look at some of the RDoc for it.0040

It says here, "a Module is a collection of methods and constants. The methods in a module may be instance methods or module methods."0048

"Instance methods appear as methods in a class when the module is included, module methods do not."0059

"Conversely, module methods may be called without creating an encapsulating object, while instance methods may not."0066

They give us an example with Module; it's called; it's including another module here, and we're ending it.0075

Then you can call some of the class methods: class, constants, instance methods...0090

So, if I call constants, it returns Module here; you can look it up; it turns out an Array--constants.0095

You can call nesting; it "returns the list of Modules nested at the point of call."0106

There is new; that has its own instance methods, too.0110

What's interesting is this ancestors; in this module, if I include other modules, I can call ancestors and see what they are.0121

You can look at more of this RDoc.0130

One important thing I want to show you here, also...let's look at the Class RDoc, because they are very similar.0134

Classes in Ruby are first-class objects--each is an instance of Class Class.0142

If I change Class to Module, I've made a module, essentially.0149

They have this chart here; everything starts at the top, at BasicObject; subclass is that Object; and you will notice that the superclass of Module is Object.0156

What is very interesting is that Class is actually a subclass of Module.0171

They do look very similar, so it makes sense.0176

Class actually holds all these exact same properties of this Module, and it defines the new ones.0179

If you have gone through our Class lesson, a lot of the same things you did there, you can do with these modules.0190

Module is the ancestor of Class, so you don't have all the same functionality there.0197

I did mention mixins; I mentioned namespace; let's talk about that.0211

First, what is a mixin? This is a method from an included module.0216

They become instance methods in the class.0223

If I have this class, I can include this module in, and all of these methods that get included--now they're just like instance methods in the class itself.0228

These methods are known as being mixed in to the class; that is why it's called a mixin.0241

Next, let's look at namespace.0248

What is a namespace? Namespace is a set of names, like method names, and constants that have scope or context.0252

We know that it's a bunch of names, essentially; a module is one form of namespace in Ruby.0268

We have this module; a module is a namespace.0277

A Ruby module associates a single name with a set of methods and constant names.0283

Again, it's very similar to classes; we have this Class that does it, but we have this Module that I can include these methods and constant names--include them in the classes--mix them in to these classes.0288

A Ruby class is another form of a namespace.0301

We have a module; it's a mixin; it's a namespace; it allows both of these.0306

Module namespace; why use a namespace?0314

It avoids adding methods to the global scope, and it prevents collisions with other common methods, and it can be utilized to group other methods together.0320

This is perfect; with this namespace, I can group all these methods together.0331

They're not going to be in the global scope; they're not going to conflict with a bunch of methods; I can scope it all in one namespace, and then I for mixing in to other classes; I can use it to scope in that module itself.0339

This avoids adding those methods to the global scope; so this is nice to have.0353

We'll do an example of a namespace module, an example of mixing in the module into the global scope; let's look at this example.0360

We're going to look at the Math module.0368

First, if I call Math and call the class method cos(0), I'm in the Math namespace.0379

But now, I can call include Math; this actually should just be lowercase: include Math...so I do include Math and bring that Math into our global scope.0389

Now, I can just call cosine...this should be lowercase, too...cos(0); and now, I can just call this; I don't have to include Math, that namespace, in.0403

I mixed it in the global scope; not only can you mix in things in classes; you can mix it in the scope of your code, too.0414

Next, let's look at modules and how I create this module.0424

I'm getting excited over what this is; how do I create it?0429

The module name must start with an uppercase letter; so, to create it, I have to add module, and then the module name.0432

On the example, module, then I can just do Foobar...or module Jar...it needs to be uppercase.0445

That first letter is uppercase; that is important.0460

If you replace class with module, you would have created a module, essentially: very simple.0463

It can contain methods, constants, other modules, and even classes.0472

Example module here: I call module--I have that uppercase letter for Cards--I can put my code here; end it.0477

There!--I just created a module; very simple, but you can make it very complex, depending on how many methods you put in, if you include another module...even classes in there--you can add nested classes.0486

Let's look at the usage.0501

How do I use it?0506

Classes can include a module, and modules can inherit from another module.0514

This is interesting; classes can include a module--we already talked about that; that's the mixin.0523

But also, modules can inherit from another module; so I actually can use this module, include another module, and instead of duplicating that code, I can reuse it in two modules; that's interesting.0533

For this example, we could use it as a mixin; I have a class called Poker, a class called Bridge...I just include Cards; they have the same functionality here.0553

Both of their usage...I'm using it the same way, and I don't even have to repeat myself; I just do include Cards, and all that code that was required for it is in this module, so I just do that.0569

Let's create our module now; now that we're excited, we need to actually create some code here.0583

Example of a module using instance methods: we're including it, and it's going to provide all of these instance methods to our class.0590

I have this module that has Cards; three methods: 1--it's going to retrieve the top card in that card deck, and it's going to return that card.0599

The second one is going to return the card back to the deck; so we have one argument that is going to take a card...0609

And the last one...we're going to just shuffle all of those cards together, so there is going to be code for that.0615

Let's go ahead and create some...look at the code for that.0620

Let's make it more concrete...so I call module Cards, do retrieve.card, and then I'm just going to do some output here--retrieve.card, and then I have another method called return_card_to_deck, one card...0629

I'm just doing output; I'm not actually implementing deep methods...just to get the concept across.0649

Card...whatever argument is returned to deck...last one is shuffle, and I'm just going to say shuffle_cards...it's shuffling the cards.0655

We have our module; now let's go ahead and include it in one of our classes.0669

We have this class Bridge; we're going to include Cards in there, and--OK--we created a class; we created a module; now, let's see it in action.0674

I call Bridge.new; it's going to create the new Bridge object; and then, I'm just going to say, "OK, I've included this mixin; let's see if I can use it."0684

I can do retrieve_top_card--notice that the instance method is part of it--output is retrieve_top_card.0695

I can also do return_card_to_deck, and I'm going to say it's an ace of spades, so it says ace of spades is returned to deck.0703

Then, I can just B.shuffle, shuffle_cards; all those module methods are part of that instance method in that class.0715

Now, let's look at creating our module as a namespace.0737

Here, we're going to implement those class methods for the module.0743

You can use nested classes to it, also.0752

For this example, we're going to get deeper and actually define some code.0756

I'm going to create this module; it's called Randomizer; it's going to have two class methods.0762

For this example, we're not mixing it in; we're not doing mixins for this namespace; it's essentially just a namespace; we're just scoping into this namespace these class methods.0771

We have this self.generate, and this is going to take one argument; whatever length you give it, it's going to generate some random string of that length.0781

That is why I have this 0..length; it takes the letters a through z, makes it an Array, and randomly chooses one of those letters for each one; and then it joins it all as a String.0796

I'll show it to you in action, too, so you understand.0814

Next, we have this self.scramble token; for this one, I pass in a phrase or word, and it's going to scramble those letters together, and it's going to put them in some random order.0818

It takes this argument 'token'; it's going to break it up; and then it's going to sort them randomly, and then it will just join them together to be random.0829

Before I do that, I want to show you how to nest classes.0843

All I would do is...let's have this module Foobar...I could just do class Bar, and put whatever code I have here...with squigglies...and I can make another class...anyway, you're going to end it here.0847

I can have nested classes like that; it's a good way to organize it, so if I want this namespace, but it's really complex, all this code I'm putting into this module, it's nice to have nested classes to help organize it.0874

That is why you have that there.0890

Let's go ahead and do an example of that.0893

First, let's look at that module Randomizer.0899

We're going to define the code.0905

I have this length; it takes an argument; I'm going to default it to 10, so if you don't pass an argument, it's going to generate a random string of 10.0907

I pass length, I call map, so it's a to z; it's going to convert that to Array, and then I'm going to do join.0916

Let's make sure we have everything here; we have: it's going to make a length--0 to whatever length they give it--it's going to take an Array; randomly choose one of the letters in that Array; and then the last part is going to join it all together.0937

Next, we're going to look at this scramble.0956

It takes a token, whatever string you want to put; it's going to split that up; it's going to sort by ran, and it's going to join that all together.0959

What I want to do here is just show you what this generate does first.0977

I'm going to go here and change length to 10, so it's going to be 0,1,2,3,4,5,6,7,8,9,10; and then, first, I call this map.0983

It does this to_a; I'm not sure what it does here; it's putting a weird statement out.1002

Anyway, let's go back to that; I do rand, and I include join.1017

Let me change that length and define it.1027

Notice how I do 10; it makes them all an Array; and when I call join, it's going to put them together.1032

Also, I want to show you: when I call that a to z, I do to_ary; it's going to get all of those letters and then, using this rand(26), it's going to randomly choose a number from 0 to 25; so it's going to choose a random letter for each one.1042

The next one I want to show you is that scramble.1065

Let's say I have a token=abc; then I do token.split; it splits it and stores them in an Array.1071

Then I call sort_by!{rand}; it's going to randomize it, put different areas--and then, I just call join, and it's going to bring them together.1084

Now that we have our Randomizer module, let's see it in action--this module as a namespace.1098

I call Randomizer; I call generate; and, when I do generate, notice it's going to get me a random string--whatever--I can put 15 in there; get a bigger one--25; 5; whatever length I want, it's going to give that to me.1105

Now, let's look at the other Randomizer; let's look at scramble.1123

I run abcdef; scramble those letters up; I run it again--notice they are different every time.1130

That is our implementation of the Randomizer module.1143

You see that we have two methods that have different things, but they are all under the scope Randomizer, and it does random stuff, so that is why we call it that.1147

We have our module there; you see our different namespace; we didn't have to include anything in the global scope--we didn't even have to make a function called scramble, one called generate...we put it all in one namespace to keep the scoping clean and easy for reference.1160

Now, let's look at loading our module.1190

Now that we understand modules--we've created them--we have a bunch of modules; we organized them to different files.1196

They're all separate files; their code is really clean; how do I use them?--how do we load this module?1205

The module is complete; you've organized them into multiple files; how do you use them?1212

There are two ways to load them: you can load them, first, using require; the second one is to do load.1215

We have to remember, for Ruby's load path, the file needs to be in Ruby's load path--so I'll show you that.1226

Both of these require different arguments: when I use require, it expects the library name, and if I use load, it expects a complete filename, including extension.1234

Let's see it in action here.1249

I have some code; I have this module, Randomizer; we've already talked about what it does--it has a generate and a scramble.1255

Now, I've included that as a file called Randomizer.rb, and I want to include that into my code.1270

It's in this directory here--you see it there--Randomizer.1277

I'm going to run IRB; first, let me show you load.1283

We're going to show load first; I'm just going to say load(Randomizer.rb); it says 'true'--it was a success.1288

Now I can use it; it's in my namespace, so I just run Randomizer.generate(10), and there you go!1297

Twenty, scramble, abc...Now, let's show you require; I'm going to exit out, so I start with a clean slate--nothing is in memory.1306

IRB; now when I use require for this code, I make sure I'm using it in the same directory, so I'm going to use relative directory path; so I need to load that into my Ruby path.1321

I do load(path), and then I say, "Load the current directory--the one I'm currently at in there."1334

It's going to actually show me in this Array...this last one is the one I'm using--that relative path there.1342

Now that I did that, I just say, "OK, it's in; I just want to require Randomizer--just the library name"; it says true.1351

I do Randomizer.generate(10)...(15), and notice, when I call generate, it makes a bigger string; and it's in my memory; it's working.1359

I can do a Randomizer.shuffle, and there it...oh, no!--what happened here?--undefined method 'shuffle'.1373

Interesting...did I call it something else?--let's see.1386

Oh, scramble! OK.1393

OK, there we go--scramble; and notice, the first time I called scramble, it actually returned the same, but other times it did scramble the letters up.1405

There you have it.1416

Now you know how to create a module, why it's used, what it's used for, how to implement it, and, when you're all done and complete, how to load that module.1422

Other than that, I suggest you go ahead and look at that RDoc and create your own modules, and see how those work, and use them for your own benefit.1436

It will definitely clean up your code, using them as mixins and as namespaces.1448

Other than that, that is the end of our lesson today at Educator.com; thank you, and see you next time!1452

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.