Justin Mui

Justin Mui

Strings

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!

Strings

  • Strings are a sequence of one or more characters representing human language
  • A string object holds and manipulates an arbitrary sequence of bytes, typically characters
  • You will often work with manipulating strings so it's important to understand the basic methods to update, delete, and insert characters in a string
  • The square brackets or [] can be used to find tokens, retrieve characters, retrieve substrings, and even match regular expressions
  • chop or chomp method allow you to remove characters at the end of the string
  • The gsub method allows you to match regular expression patterns and replace it with a string
  • The split method will divide a string into substrings using a delimiter and return them as an array of substrings.
  • upcase will replace all lowercase letters with uppercase counterparts while downcase will do the reverse.

Strings

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
  • Strings 0:08
    • Why do you want to get familiar with strings?
  • String Creation 1:16
    • new
    • empty?
    • length or size
    • Example
  • String Manipulation 4:40
    • slice
    • square brackets [ ]
    • token
    • [fixnum]
    • offset and length
    • chaining
  • String Insertion 12:56
    • insert
    • positive or negative index
  • String Updates 15:24
    • [token]
    • Examples
    • chop or chop!
    • chomp!
    • gsub
  • String Deletion 21:38
    • delete
  • String Reversal 22:46
    • reverse
  • String Manipulation 23:16
    • split(pattern=$, limit)
    • pattern
    • limit
    • upcase or upcase!
    • downcase or downcase!
    • swapcase
  • Incrementing Strings 27:26
    • next or next!
  • Check Out the Other Lessons 28:00
    • Ruby Data Types Part 1
    • Regular Expressions

Transcription: Strings

Welcome back to Educator.com.0000

Today's course is on strings.0001

What are strings? They are a sequence of one or more characters representing human language.0007

A string object holds and manipulates an arbitrary sequence of bytes, typically characters.0022

You can think of a string as not essentially holding just a single character; these bytes could be a Unicode format that uses a different size; that is why...think of it as a sequence of bytes.0034

Why do you want to get familiar with strings? You will work with strings often in your development, especially in programming, and manipulating strings should be your top priority.0057

The first thing we are going to talk about is string creation.0074

There is the constructor new; we have gone through it in an earlier lesson, but let's do a quick review.0077

We have this method new; it's part of the string object; it will create a new string object.0087

It's very simple: you call string.new, and it will make this double-quoted string.0094

It will save that...I mean, it will substantiate that into the string as an object reference.0101

The next thing we're looking at is this empty method.0108

This returns true if a string path has a length of 0.0111

Here we have the string.empty?; because there are no digits in here--it's a length of zero--it returns true.0117

The next two methods we are looking at are length and size.0129

Both of them do the exact same thing; they are just different ways to say it.0135

I have a string.size and a string.length, but both of them, for our case with the string, equals, quote, quote...has a size of zero.0140

It returns an integer that indicates how many characters are in it.0158

If I have two characters in it, like...I'll show you...a, b...when I call string.size on this, this would be 2.0166

A,b,c...this would return 3.0184

Let's look at some examples.0190

I call string.new here, and here is my new string.0193

I'm using the constructor method here.0202

If I call the .empty?, it's going to return false, because it does have characters in here; it is not a size of 0, so it's not equal to zero.0206

You can call string.size, too; it has a size of 21 characters in here.0225

Another way you can create your string is through the Kernel method.0232

We went through it before: again, you just call, with a capital S, String, and in parentheses, you put in your string to create.0239

This will create the same string object that we did with the constructor.0251

Another way is probably the most basic, the easiest...the most popular way you will do it, too: it's with the assignment operator.0258

Here it is: string=" and string=', and here is my new string.0268

The next thing we're going to look at is string manipulation.0278

There are a lot of different methods to manipulate strings; let's try to get to the most popular ones, the ones you will be using.0283

The first one we are looking at is the method slice; it also can be called with the square brackets here.0292

It might be hard to see on your screen, but these are square brackets.0303

So, this looks like this...0314

These square brackets allow you to do a lot of different things, depending on what type of object you pass in it and how many arguments you pass.0320

It will allow you to do a lot of different string manipulations.0329

The first one we are going to look at is finding tokens.0334

Here, you use the square brackets, and you put in a token value.0339

For our case, we're going to use a string as our token, so we're going to look for words in it.0346

First, it will return nil if that token can't be found.0355

But, it will return the token if it does find it.0363

First, I have a string: it has a quote, "A fool thinks himself to be wise, but a wise man knows himself to be a fool."0368

My token is 'fool'; it's going to return that token once it sees it one time.0376

So, when I call this string equals [fool]--here is my token--it's going to find it at the beginning here, and so it's going to return.0382

There you go: we found our needle in our haystack--our word in our sentence.0404

The next thing we're going to look at is getting characters.0411

To do this, you just use square brackets again, and this time we are passing a Fixnum object.0417

What this will do is, it will return the character for the Fixnum.0426

Fixnum is an indexed value, so I would pass in, for a string, what index I want to pass in these square brackets, and it will return me the character in that string for it.0432

For our example here, we have string="abcdef".0449

If we do the index, a is 0, b is 1, c is 2, d is 3, e is 4, and, of course, f is 5.0457

I have my value 4 here; that is my indexed value.0472

So, if I pass the index value, it's going to return me e, which is the fourth value here.0486

When I call string.slice for it, it also returns me e.0497

Remember, slice is the equivalent of the square brackets there.0503

Anything you pass in those square brackets, you can use slice and pass in the same arguments.0512

The next thing we're going to look at is finding substrings.0521

To do this, you pass two arguments.0524

Let's see...you need two arguments...what you are going to pass here is the offset and length.0531

First, offset and length are Fixnum objects, Integers, so don't try to get tricky here.0547

This returns a substring using that offset and that length.0561

Let's go through an example here.0568

Again, we use the same sentence, but what I want to do here is return this last portion, because it's an interesting line.0570

I'm not returning that period, or the comma, or 'but a', just that phrase.0588

If I were to take the offset, 40 would be where it starts here.0596

And then the length of 35--35 is the actual size from where it starts, here, to where it ends, here.0607

That is a total of 35 characters.0620

Now, for that...it starts from that offset, you go 35 characters in, and then when I call it using the string with the square brackets, it's going to return to me that substring.0633

Not only can you do substrings with the offset and length; you can also use a range to get it.0663

Well, it looks like a range; it's actually this begin index..end index.0672

It has the same syntax; what this does is, it retrieves the substring, using a range, from the begin index to end index.0681

Make sure you use those two dots there.0694

If you wanted to do three dots, you know it chops off the last one.0698

Again, we use the same sentence, but Ruby is giving us the flexibility to say, "Hey, you know, not only can you get the substring this way; you can do it this way, too--whatever works best for you."0703

For this case, the beginning index starts at 40, but if I go through all these characters--41, 42, 43,...all the way down here, the l is at the 74th indexed value.0718

So, when I call this square brackets, it's going to return me that exact same phrase that we looked at on the last slide.0739

Also, that phrase itself doesn't look that good without the first letter being capitalized, so there is a method called .capitalize, and that will uppercase the first letter of the string.0753

The next thing we're looking at is the string insertion.0772

This is an insert method, and it takes two arguments: it takes an index and a string.0778

That string is typically your double quotes...in your string in there.0789

Of course, you can store that in the object reference and pass that in, too.0795

What this method does is, it allows you to enter the string at a given index.0800

This index can be positive, to start at the beginning index, or negative, to start at the last index.0807

I have two examples: one using the positive index, and the negative index.0818

Here is the positive index.0825

Over here, I have abcdef; it does an insert to the beginning of the string, so the 0 is going to be this letter a.0835

It says, "I'm going to insert this string into you."0849

Notice that our new string is put in the front position--the 0 position.0853

There is our string that we started with.0870

At the same time, I can use a negative index to put it in the back.0875

This just makes it easier, so I don't have to count through the whole string set; I can just say, "Hey, if I want to pass it to the back, I can just call it -1."0886

See here, we have our string "abcdef", and it passes 123456 at the end, which correlates with -1 here.0898

Remember, if you keep going through the negative index...this is -2, -3, -4, -5,...you can go all the way to the front, -6.0911

The next thing we're going to look at is string updates.0925

This allows you to change part of a string.0929

In this case, we continue using our square brackets with the token, but you can also set equal and put the value you want it to replace it with.0934

This will replace the token with the new string.0948

Replace with new token...0958

Here I have a string--it says, "King Henry III, the ruler of all the ocean lands."0961

First, I'm replacing III with IV, and then I'm changing ocean to desert.0969

Now, after I do these two changes--it's going to change this and this--when I call string, it's going to have it already updated to itself.0975

It has "IV" and "the ruler of all the desert lands."0989

Again, we can use the range and update it with a new token, also.1000

To do this update, you have a start index and an end index.1011

What you do is, you put the token you want to replace it with.1017

Here we have a string, "Phoenix is an excellent city."1024

0 through 6--you start at 0, through 6, and what it's going to do now is, we take this substring and we say, "We want to replace it with this text."1028

And it doesn't have to be the same size, remember: look, "Los Angeles" has many more characters, and it will actually remove this and put this in its place.1042

At the bottom, you have the end result after printing out the string.1057

The next thing we're going to look at is the chop, and the chop! method that works on itself.1067

This one will actually return on itself.1075

This returns the string with the last character removed.1080

This becomes useful if you're working with files, you're parsing different things, and you get new lines, carriage returns, and tabs--things you don't want.1085

Here is my sentence, and notice, at the end I have this ugly carriage return.1096

Instead, I can just call this .chop, and it's going to remove it.1104

Remember, this chop method is actually going to create a new object with that last character removed.1109

You can also use the self one, and then if you call sentence on the next line, you will see it actually removed in your existing object.1117

The next method we're looking at is the chomp, or the chomp! that calls itself, too.1130

The chomp takes one argument, but it's not required, so here what I would do is...it will return the string with the given record separator removed from the end of the string.1140

Now, if the separator is not specified, it will remove by default the new line and the carriage return.1156

Here is our example; in this example, we're actually passing an argument in.1165

I have abc123, and like I say here, I want to chomp that 123, so the end result of the string is abc.1171

Remember, I could pass anything into chomp--so if I pass 1 in there, it would have removed 1.1183

This would have been .chomp, and if I passed 1 in there, I would get abc23.1190

The next method we're looking at is gsub.1205

This is a very powerful method; this allows you to take a string, pass in a pattern, and then you can also pass in what you're going to replace it with.1209

It doesn't just have to be a string that I'm passing; I can use regular expressions in here--so it's really powerful.1220

This gsub method takes two arguments: a pattern and a replacement.1228

The pattern can be a substring or a regular expression.1234

We're going to go over regular expression in another lesson, so we definitely want to look at that.1239

This replaces a pattern with a replacement string.1244

For my example here, "I like sunny weather." I have this gsub method, and this is actually a regular expression.1250

It's a very simple one, though--this is just taking a literal, saying vcsunny.1260

We will see more complex ones in the regular expression lesson, but...1268

The replacement is this "rainy", and you will notice, after I do the gsub, it returns a new object: in this case, it says, "I like rainy weather."1274

I believe there is also a self for this, that you could also use--gsub! with an exclamation--to update the existing object.1283

The next thing we're going to look at is string deletion.1293

This method takes the arguments of the thing you are deleting.1297

All characters in the intersection of the arguments are deleted.1306

For this one, I have this sentence, "Hello, welcome to Dave's presentation."--it has a bunch of carriage returns, and it's hard to read, so what I do is, I put the argument I want to delete.1311

It's this carriage return, so I have one there...I have one there...I have a third one there.1329

It's going to remove these, and then, after I do the delete, it will actually update the existing string.1336

You will see it's much cleaner now; I can read that.1344

"Hello, welcome to Dave's presentation. Please turn off your cell phones. We will start soon. Thank you."1349

This actually has a period, here.1356

The next method we're looking at is the reverse.1361

This is a very simple method; it will take the string and reverse it.1366

It returns a new string with the characters in reverse order.1372

Since I didn't want to type out a through z, I used this range, and if you call reverse on it, it will just reverse all the characters, so it goes back from z to a.1377

The next string manipulation we're looking at is the split method.1394

The split takes a limit, which is optional.1400

This argument is optional, and I don't use it all the time...and it takes a pattern.1407

This method divides a string into substrings using a delimiter.1419

More importantly, it will return an Array of substrings.1431

This pattern will default to white space, and the limit will give the maximum number of fields to be returned.1440

Let's say I call split without any arguments: it's going to see any spaces I have, and it's going to use that to break it up and put it as different pieces of substrings in the Array.1454

For our example, we're going to look at split with the comma.1471

Here, I have my example with item prices.1476

Notice, they all have these commas--10, 20 dollars, 14.99, 3.99, 5.99...1480

Using this split method is pretty handy.1487

After I call this split, it's going to break this string up to substrings and store it as an Array for me.1491

My first element is 10 dollars, 20, 14.99, 3.99, 5.99...and this is all in one Array.1501

Remember, with this split, I can pass in whatever arguments I want.1514

I could even use this period, and it's going to give me some weird substrings that it's going to pass, too.1518

The next one we're going to look at is upcase.1528

This returns a string with all the lowercase letters replaced with uppercase counterparts.1530

For example, I have a string that says, "I'm going to the zoo today to check out the pandas."1540

Call upcase, and now it's really shouting: "I'M GOING TO THE ZOO TODAY TO CHECK OUT THE PANDAS."1546

That's all upcase does--it takes the letters and uppercases all of them.1552

Again, there is another one called downcase; it does the exact opposite of upcase.1559

It returns a string with all the uppercase letters replaced with lowercase counterparts.1564

I have the string, "I HATE FRUIT.", and then the downcase version is just, "i hate fruit.", all in lowercase.1570

There is also a swapcase that does a combination of the upcase and downcase.1581

It...let's see here...does both upcase and downcase.1587

Now, what do I mean by that? Anything uppercase will become lowercase, and anything lowercase will become uppercase.1601

Swapcase returns a copy of a string with uppercase characters converted to lowercase and lowercase characters converted to uppercase.1608

Our example here: we have the lowercase abc, capital ABC; swapcase; it will return the lowercase letters as upper and the uppercase letters as lower.1623

Incrementing strings: this one uses the next--we're using the next method.1642

You could also use succ, which does the same thing.1652

This will increment the string using the rightmost character.1660

For example, we have 2012; if I call next on it, you will see it says 2013 now, and if I call next again it's 2014.1665

That is the end of this lesson for strings.1679

There was a lot of stuff that we covered in strings already, in the Ruby Datatypes Part 1.1684

I suggest you look at that, and look at the regular expressions lesson.1692

There are a lot of good things there to do more string manipulation.1699

That is the end of this Educator.com lesson.1703

See you at the next one.1709

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.