Enter your Sign on user name and password.

Forgot password?
  • Follow us on:

Professor Justin Mui guides you through everything Ruby as well as various frameworks such as the popular Ruby on Rails and Ruby Motion for iOS applications. Justin's method of teaching is very hands-on and interactive where you become deeply involved with your own Ruby code. Justin first goes over installing Ruby on a variety of OS's and then important programming concepts and incorporating them with how they are utilized in Ruby. Other topics include Data Types, Operations, and Methods, through Classes and Modules. Professor Justin Mui received his B.S. in Computer Science and has been in the web development industry for over 10 years. He is now a senior software engineer at a premier firm specializing in Ruby coding. Downloadable code examples are included in most lessons so you can follow along with Justin.

Loading video...
expand all   collapse all
I. Introduction to Ruby
  Setting Up Your Environment 22:08
   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 22:20
   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 27:44
   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 20:45
   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) 29:37
   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 25:50
   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) 40:24
   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 65:46
   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 61:50 
    taint 62:08 
    farm field 62:12 
   Untrusted Objects 64:06 
    trust 64:24 
    untrust 64:34 
    untrusted? 64:42 
  Loops 38:54
   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 28:30
   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 33:27
   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 14:35
   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 27:48
   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 28:47
   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 28:51
   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 26:01
   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 31:24
   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 20:11
   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 26:51
   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 26:42
   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 53:36
   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 24:19
   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 

Welcome to educator.com. Today's lesson is Intro to Ruby.0000

So, this is the first lesson, and this will be a very general overview. What is Ruby?0010

It's a dynamic programming language.0017

It's very expressive, and it's complex, but it's very simplified for all the things it can do.0025

As we go through many of the other lessons, you'll see why it's called complex.0032

But all these different functionalites built in it make it really easy--many different types of apps and features that you're making your application in.0040

Ruby does come with a core class library, and it has a very rich, powerful API.0052

It's also called the Ruby standard library.0061

So who created Ruby? It was a guy named Yukihiro Matsumoto.0083

He's better known as "Matz."0090

Before he used Ruby, he actually developed multiple other languages, but he was never satisfied with any of them.0094

When he created it, his philosophy for Ruby was that he wanted to design a language to make programmers happy.0106

That's a nice thing to have, I believe.0114

What he wanted to do was...he actually said that he wanted a scripting language that was more powerful than Perl, more object-oriented than Python--and that's why he decided to design his own language.0120

So, he's used in the past: Perl, C, Java... but he's never satisfied.0134

He has influences using Smalltalk list and Python. So he has a lot of influences, multiple influences for many languages.0146

So it's nice to know that the guy that's influencing it...made this really good language.0155

Let's talk about the history of Ruby.0163

The name itself, Ruby, was established on February 24th, 1993, in a chat session, actually. 0170

He had a chat with one of his cohorts, and they're discussing, "Maybe we should make a language!" and "What should it do?"0182

So, after they established a name and got the ball rolling, they actually released version 0.9.5 on December 1st, 1995.0190

At that time, in development, it was a very basic language.0203

They were still in progress, doing object-pointed design, adding classes with inheritance, mixins, iterators...0210

and remember, all these things that I'm saying, we will go over in further courses.0222

...closures, exception handling, and garbage collection.0227

About a year later, on December 25th, 1996, he actually released Ruby 1.0.0233

It was popular in Japan, but in the U.S., there had not been much influence of Ruby.0246

"What was it?" But it just so happened that, in 1999, a mailing list was created.0255

It was an English mailing list called ruby-talk that was established.0263

Right now, you can actually see that mailing list: it's mirrored at ruby-forum.com.0267

This domain actually mirrors the ruby-talk mailing list...so it's nice.0277

What I'd like to do is...let's open a browser and take a look at that website.0287

Let's go to ruby-forum and check it out.0299

First--you will notice I'm already in the Ruby Forum, but--I wanted to show you the actual main site.0302

As you go to ruby-forum.com, you will notice that it actually has quite a list of topics there.0311

The main one you will be using is just the first one, with "Ruby."0320

But you will notice that there are a ton of other forums here.0326

You will see, right below it..."Ruby on Rails" is a very popular framework.0330

It is used for web development, and that is the second forum.0335

The third one is Rails internationalization mailing list.0341

You will notice, if you go down this list, that a lot of them are all based on Rails.0349

So you have Rails deployment, Rails Engines, Rails Spinoffs...0353

...Rails France...and there are a lot of different forums, mailing lists--this is just a hodgepodge on this site.0362

Another popular one is one called Radiant CMS. That is kind of like a Wordpress for Ruby.0371

It's all built in Ruby, and it's a pretty clean CMS--very simple.0376

And you notice, if you go down here, another important one: Ruby-core. 0384

You can see what the core team is doing and what they're discussing,0388

and what they're going to do with the Ruby language.0395

Plus, if you go down even further, you will see there are actually different frameworks of Ruby.0400

There is JRuby--some integration thing to integrate it with Java.0407

IronRuby--you can use Ruby to implement a .net application.0411

And then they also have some back-end processes, like Mongrel and NGINX, to do back-end handling for web services.0418

They are not just used for Ruby, but those are used for other languages, too.0431

And there are other things there. You could sign up.0435

There is a "user" and a "register" link..."log in"..."sign up"..."show forum post"...0443

But let's just go ahead and click "Ruby" here.0451

You will notice that it lists the posts from newest to oldest.0453

You can see a lot of different topics here that are very...from the most recent to the oldest, but...0463

...if you have any questions in Ruby, a lot of them are already answered.0477

If you look at this list, you might have a question that you want to post, but maybe the best thing is to do a search to see what is over there.0481

Let's say I'm going to look up something with Ruby strings.0490

I can just do the "search subject" here, and you will notice it will give me all the posts from the latest one to the oldest one.0493

And I'm like, "Oh, what is this ruby_parser 3.0.1?" so I want to check it out.0503

So I can click on it and dive into it: it will tell me who posted it and give some examples of the code and some changes, bugs, examples from that.0508

I recommend you check that out, and you can log in and register to sign up for the Ruby Forum.0520

Next, I will also--I know this mirrors that mailing list for ruby-talk--0527

But also, you can go to ruby-lang.org,0537

and they have a mailing list URL. You can sign up, too.0542

So let's say you just want to get it straight to your email box.0547

You can actually subscribe here. You notice they have ruby-talk;0551

they have Ruby-Core--that mailing list deals with the core and implementation talks about Ruby.0557

Usually there are patches there. Ruby-Doc, Ruby-CVS...you can subscribe.0565

You can choose Ruby-Talk here, enter your name, your email address, subscribe, and click "Submit Form."0572

And it will add you to the mailing list.0578

So that is the Ruby-Talk mailing list forum. Let's continue on.0584

Like I just said, Ruby was built in Japan.0593

It got very popular there. It started getting popular in the West around 2000.0602

The first English book that was published was one called Programming Ruby.0607

With that, they translated a lot of things that were in Japanese, and made it so we can see that syntax in English and get a sense of how we can utilize it in our own programming...and see the flexibility and the expressions and the richness of it.0613

And then, what really got it going was in 2005.0635

The interest skyrocketed with Ruby on Rails.0639

This Ruby on Rails is a web framework.0643

It makes it very easy to get a website up and going.0649

They give you a lot of cool tricks and key things, like MVC-- that is a model-view-controller to separate and organize your code.0657

It's really made to be an all-in-one package for everything to build a website.0669

The creator of that is David Heinemeier Hansson, better known as DHH.0679

After that, Ruby really took off.0686

So, let's go ahead and take a look at some of these materials.0690

Definitely this Programming Ruby book, you probably want to use that as a reference even when you are doing your own Ruby development.0696

Let's go back here in the browser; what I'm bringing up is that URL...0701

This is the Programming Ruby book on the Internet.0709

And if you go here, the first two things, just like a normal book--0712

they have a frame here on the left, and he will give you a foreword of why he created this book and some references he used to create it.0722

And there you go--Yukihiro Matsumoto.0731

He wants Ruby to serve you, to make your programming easy, enjoyable, and have fun!0734

Don't you love that? It's great. So let's go ahead and move deeper in here.0741

You notice they have a preface; you can go ahead and read about this.0747

They give you a lot of introduction--more about Ruby and why it was created0755

and why you should learn it. 0763

"What kind of language is Ruby?" You can go ahead and read it...there are a lot of things. "Is Ruby for you?"0766

Read it, check it out...and why they wrote this book.0773

The instructions for installation are pretty old, so we do have a lesson to set up your Ruby environment0777

that I'll go over, that should be a lot more up-to-date.0788

I don't even know if that FTP link works.0792

Go ahead and try it out. They still use CBS.0797

So...it starts with building Ruby, running Ruby, IRB... and we'll go through those in our own lesson.0804

So you won't be missing out. 0810

The other thing is...talking about Ruby on Rails very quickly...rubyonrails.org.0815

This is an open source web framework, all built in Ruby. 0821

The Rails framework makes your websites very easy.0827

If you go here, you can see a lot of information about it.0831

It tells you about some sites that are using it; it tells you some information of who created it, and who is developing it right now.0835

They also have this link that says "Screencasts and Presentations."0849

If you click on that, you can see videos to get started on the Rails framework,0852

which is very nice. There is a lot of information there.0857

Basically, if you have a good foundation in Ruby, it will make learning these frameworks a ton easier.0863

That is what I hope we can do in this course for you.0876

Currently, Ruby is 1.9.3. 0883

That is considered stable right now.0892

1.8.7 is the latest one in the 1.8 world.0897

You might see that on your laptops, if you are in Mac OS and you have one of the latest operating systems-- it might default to the

We're going to be using 1.9.3 for these lessons, and you will use a lot of the new objects and things in it to get your environment running.0914

Ruby 2.0 is in development, and there iss a plan to release in 2013.0928

It will be quite exciting, seeing what they get done there.0934

So why do programmers enjoy using Ruby?0940

It's portable. It's lightweight.0946

It's easy to learn, and a lot of people say it's a real pleasure to program.0952

There aren't any weird symbols in it. It's very friendly; it reads like a book.0958

And it's flexible. You'll see it used for many different types of programming methodologies.0963

...and used for many different types of applications, not just websites or web apps.0969

Ruby lets you decide how you want to do things.0975

Ruby is an interpreted language, but it is also a scripting language.0980

You will see a lot of key concepts in procedural programming built in.0990

Functional programming...a lot of key concepts there, too. So there are many different styles you could use it for.0997

But, really, Ruby is more of a general-purpose programming language with a system that you can use for many different things.1002

So, where can you see it being used? What is it being used for?1009

You will see it in graphical user interface applications.1017

If you are ever looking at a website, it might be being used for some middle-tier server process.1021

That is something behind the scenes, after the front end...some process getting sent to do some transaction for an ecommerce site...or doing some search. 1028

It is also used for managing server machines and databases.1040

...Rails--serving webpages using Ruby on Rails...1047

You are definitely using it to serve your webpages.1054

And generating dynamic content, again...doing metaprogramming and serving up different content on the Web.1061

You can do that with Ruby. Writing AI and machine language programming--Ruby is able to do that, too.1071

You will also see frameworks to go to games using Ruby.1079

Plus you have mobile development: you have frameworks that allow you to create iOS apps, and you can also make mobile websites using Ruby...so, mobile development, as well.1083

Ruby is object-oriented.1097

Every value is an object.1104

If you look right here, you will notice that even if I have a number, that number itself is an object.1109

Where else in code can you see a number called .class?1123

When I do that, it's going to tell me what object it is. So it says this object, 5, is a Fixnum object.1127

If I call 0.0...it doesn't end it; that is still an object. I can call it .class, and it will tell me that it's a Float.1134

And even Boolean statements have true or false: there is a class for that.1144

So we call up true.class. You get TrueClass.1149

And even nil is an object itself--so, nil.class--you will get NilClass.1152

So, you see, everything in Ruby is an object.1158

Every object has an underlying object that is a parent, except this class called the Basic Object.1166

The parent of all objects is this one that's called "Basic Object."1176

But the child of that is "Object."1184

And, when you see all of these other ones--Fixnum, Integer, Numeric--they all inherit from Object.1191

So you often see this Fixnum, Integer, Numeric...that is its parent.1198

Or, I should say, its ancestor--you'll see it ahead of it.1206

So going back, this Object class we were talking about...1209

it's a method that is accessible to all the different classes.1219

And it's the parent of all classes, except that Basic Object. Basic Object is the top.1222

If you ask the code, "What's the parent of this object?" it's going to be null, because there is nothing above that.1228

So, we go back and look at these classes.1234

If I just call up 5.class, I get Fixnum.1240

But I can ask Ruby, "Who is the parent of this class?"1244

And the parent of the class is called the superclass.1249

When I say, "Where is the superclass," what I really want to say is, "Who is the parent?"1253

And so I do Fixnum.superclass, and it will give me Integer.1265

Integer.superclass--it will say it's a Numeric, and Numeric.superclass is Object.1277

Notice how we started with the Fixnum from the number 5, and we kept going up the ranks, all the way to Object.1282

At the end, we found out that Object is the parent, and all these different classes inherit from it--1292

Numeric, Integer, and Fixnum.1306

Basically, that is what I want you to know.1314

Everything is an object, and all of them lead to Object at the end result.1320

Well, that is the end of this lesson. I hope you enjoyed it and look at our next lessons so we can get deeper into the Ruby code and setting up your system to use it.1327

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

Welcome back to Educator.com.0000

Today's lesson is on regular expressions.0002

What are regular expressions? They are used to match patterns against strings.0008

In Ruby, you will see the object that is called RegExp, and it is the main object to create regular expressions.0016

It is used to match a pattern against strings, as I just said.0036

How do you create a regular expression?0044

There are three ways: one is using the forward slashes--this is the most popular way, and it follows a similar syntax as in other languages, so if you are coming from another language, you will probably see this the most often; it's the most familiar to you.0046

Another way is the literal syntax.0067

This one is more well-known in Ruby; it uses the percentage, r, and the curly braces.0070

You have it through the constructor; the Regexp.new creates it through the constructor.0083

You saw on that slide, it has the dot, dot, dot; what goes in there...you can put a different type of things in there.0097

For example, for this one, I say it's two words, and for that I have this regular expression--what is says here is, "I'm going to take a word character--one or more word characters, a space, and then one or more word characters again."0112

That is what that regular expression is matching.0135

Another one I have here is a range of digits--so it takes one or more digit characters, which is 0 through 9.0140

The other one I have here is just dot, dot, dot...and this one is for you to be creative.0150

If you were just to put ... , it would match three wildcard characters.0160

So, this is just...be creative, and update that to make your own regular expression; that is what I want you to do.0166

I'm going to write that down: Make your own.0176

To create regular expressions, we need to look at four different key elements.0185

One is metacharacters, bracket expressions, quantifiers, and anchors.0192

First, we are going to look at metacharacters.0207

For this one, we are going to look at this dot; this will match any character except a new line.0211

The next one we are looking at is this multiline mode. 0224

It just adds this m at the end, so it can match new lines.0228

Here, with the slash and the lowercase w...this matches a word character.0236

A word character is a lowercase a through z, capital A through Z, 0 through 9, or this underscore character.0245

Another one we can do is, with a capital W, we can match a non-word character.0256

This is not a through z, not A through Z capital, 0 through 9, or the underscore.0264

The slash d matches a digit character; it's 0 through 9.0280

A capital D does a non-digit character.0288

You can match hex digit characters, 0 through 9, a through f...0293

Capital H--a non-hex digit character...0301

The small s matches a white space character--you have your new lines, your tabs...0308

The capital S matches a non-white-space character--so not those.0316

Here, we have a few examples.0325

For the example on top, I have this sentence here; what this regular expression is going to do is, I'm calling a literal, and it's going to get two words.0331

It says, "I'm going to match the first two words I see."0346

So, it matched this first part of the sentence.0350

On this next section here, I have another string, and this one is going to match a range of one or more digits, a space, and then a word, which is one or more word characters.0357

Of course, it's matching the beginning again, so it's the "1000 bottles".0381

Notice, for this one, we are using the token method by string, and we are passing a regular expression, versus passing the actual quoted value for the string.0387

Here is another way you can do regular expressions.0405

This is defining the regular expression through a constructor method; what it's going to do is, it's going to create that object, and you can pass that in.0406

If I do Regexp.new, it's going to match a word, a space, another word, a space, and another word.0423

I'm just going to call this regular expression 'Three words.'0433

I'm telling the string, "Match me a pattern that has three words," and it finds, "1000 bottles of" and matches that.0436

We can also do this same thing using the regular expression literal.0451

You will notice that it has the same value, also.0458

The next thing we are looking at is bracket expressions.0466

First, we have the POSIX bracket expressions; this one gives you a more refined, specific case of things you can match against.0474

The first one, alnum, is alphabetic and numeric character.0487

Notice, to do this match, it has two square brackets.0492

The second one after that is alphabetic characters; it uses this alpha.0497

The third one is this blank, and that matches a space or tab.0503

This one here, cntrl, matches the control character.0515

For the fifth, we're matching a digit: remember, that is our 0 through 9--so it is matching our digit character.0520

Our sixth one matches a non-blank character--this graph--it excludes spaces, control characters, and similar...0531

The next one matches lowercase alphabetical characters--it uses lower.0543

As you can see, this is just using tags, but it is matching these different characters using them.0550

It's not scoped as deeply as the metacharacters were, but you can refine...look at these different patterns and match more specifically to it.0557

The next one is print: it's like graph, but it includes a space character.0575

The last one can match punctuation characters.0581

Also, there are more bracket expressions than this, but I'm just showing you some of the more popular ones.0590

Here is the space--it's very easy to understand--it's a white space character.0596

And then, this upper matches the uppercase alphabetical characters, and xdigit is a digit allowed in hexadecimal--a hexadecimal number--0 through 9, a through f, and capital A through F.0604

Then, there are also non-POSIX bracket expressions, which are allowed in Ruby, too.0623

If I pass in word, it's going to match "a character in one of the following Unicode...categories: Letter, Mark, Number, Connector_Punctuation."0629

Then, if you pass ascii, it's a character in the ASCII character set, so that one is probably more of a popular one you would be using.0644

Let's look at some examples of bracket expressions.0654

We can do it without bracket expressions, but this allows us the flexibility to do both; Ruby allows us to have this open scope to match using the ways we find most popular.0659

Here, we have our string: "A fool thinks himself to be wise, but a wise man knows himself to be a fool."0676

Here, we are matching three words using the bracket expression.0683

One word, one word, one word...also, I know I haven't told you yet, but this plus is a quantifier.0687

We will go over that...but that is the thing that says "one or more"--so it says "one or more word characters."0697

Notice, for this next example, I take that regular expression, and I save it to a variable, so I can pass it in here; and I get the same value.0712

And then this last example: what I'm telling it is to match a digit from the string, but since there are no digits in it, it's going to just return nil.0731

If there is any character that doesn't exist, it will return nil, just like that.0745

Now, looking at the quantifiers: you have already seen this one with the plus that says "one or more times."0753

Another popular one you will use is the star; it says "zero or more times."0760

Question mark says "zero or one time."0766

Then, we have these four bracketed lines: the curly n--exactly n amount of times: so if I pass a 10 there, exactly 10 times--that is how it is going to match.0772

If I do n, comma, it's going to match--for 10, it would be 10 or more times.0785

If I pass a comma, and then pass a number in there, I'm going to match m or less times.0792

I can pass both arguments in it, and it's going to say "at least n and at most m times" for the quantifiers.0799

Let's go through some examples.0809

Here, I've made this regular expression--it's called the namefinder: what it does is, it looks for two words, one word with the uppercase and then the lowercase characters after that; then it's going to look for a space; and then it's going to look for another uppercase character and a range of lowercase characters from there.0812

So, I pass this namefinder to the string: you will notice it finds that pattern right there.0839

For our next example, I am making a regular expression; it will say...it's going to look for the letter a.0849

Before that, it could be a word character of 0 or more, the same as afterwards.0860

I pass that in here, in this pattern, and it finds the first word that contains the letter a, which is name.0868

This regular expression says, "OK, a could be at the beginning of this word; it could also be in the middle; of course, it could also be at the end."0882

Either way...even if it's by itself, it will match that as a word, too...so if it's just a single letter a, this expression will also match that.0896

So, one of these scenarios, if they pop up, it's going to return that.0909

The first one it sees is the a here--it's the third word--so it returns that as the matching pattern, and it returns that as what it gets from this regular expression.0915

The next one we are looking at is those curly brackets.0934

We have our string; it has three a's, four b's, five c's.0942

For this one, I have this a with the curly brackets; it's one, comma, four.0949

It matches with the three letters there.0957

This says that a is shown at least one time and at most four times.0965

That is what this one and the four is.1000

Here, we have the same for b, but the b is shown at least one time and at most two times, and here you go--it returns two, which is the most.1004

And then, we have this one with the five, comma, which is "if c is shown five or more times," and it returns ccc.1026

The next thing we're looking at is the character properties.1043

This is another way to do a matching with the regular expression.1050

It uses the slash, p, and the curly brackets.1057

You will notice it has a lot of the similar classes as POSIX.1063

You still have your alnum for alphabetical/numeric; you have alpha for alphabetical characters, blank for spaces or tabs, cntrl for a control character, digit, and graph.1069

For graph, that is your non-blank character again.1088

Notice--it's interesting how I'm still passing my regular expression pattern, but I am using this new p with curly brackets, and I'm passing the actual thing I want to find into this pattern.1093

It's...p...you enter a pattern in here...1115

Here are some more examples.1126

You can pass in lower for lowercase alphabetical characters, punct for punctuation characters, space for a white space character, and upper for uppercase alphabetical.1136

This p--you can also match a character's Unicode script.1152

If you look at the RDoc, there are other character properties in there.1162

We will look at that, too.1167

Here is an example with digit.1170

I'm using my quantifier "one or more."1173

I have my digit regular expression match.1177

It's going to take the first digit--one or more--it's going to match the whole thing, so it sees 5000 and that is what it returns here.1182

The next one we are looking at is alphanumeric characters, and then it's going to look for a space and another alphanumeric character.1191

The first one that comes up is this "apples in," so that is what it matches, and it returns that--that is the token it finds.1199

The next thing we're looking at is anchors.1209

This will be pretty important, too, because you want to anchor your regular expression--not always at the beginning: you might want to do the end.1214

These are metacharacters that match the zero-width position between characters.1224

It is used to anchor the match to a specific position.1230

Here we have our ^; it matches the beginning of a line; a $ matches the end of a line.1235

The A matches the beginning of a string; the Z matches the end of string; if the string ends with a new line, it matches just before the new line.1245

Lowercase z matches the end of string.1260

There are a lot of other anchors in the RDoc, but these are the popular ones.1263

Let's look at some examples of anchors.1272

For this one, I'm using the dollar sign that will match at the end of the string.1276

Instead of starting at the beginning, it's going to match from the end, look for patterns from that, and if it finds it, it will return me that substring that is part of it.1281

Here, what I want to do with this string is, I want to get the last sentence.1293

There are two sentences here; I want to get this last one.1298

In my regular expression, what I'm looking for is, first, it's going to anchor that to the end to do a match.1302

It's going to be looking for a period or a question mark, and then it's going to look for a group of words.1310

That will match my last sentence.1322

When I pass that in into my string, it does get, "What is your name?"1324

The problem is, it also matched this space.1332

What I'll do is, I'll just chain another method here; I call this method called strip--this is one of the string methods--and that is going to remove that blank space that was in front.1337

It's going to get rid of that, and then I get this string here.1351

The next thing we will look at is regular expression matching with the Regexp object.1361

For this, we are looking at the match method.1368

This is part of the Regexp object, and it allows matches with strings.1373

For this one, I have the string, "Hello, my name is Mr. Smith. What is your name?" 1380

This object is a regular expression.1388

I call the match method that is part of the regular expression, and I pass in the string, and what it will do is, it's going to look for Mr. in this string.1394

It's going to find it here, and then it will return...it says, "Hey, I found the match data, and I found your token."1419

Now, if I pass in there a pattern--like I pass in the w+, it's going to match the "Hello" at the beginning.1428

Again, if I also pass in...if I start with a regular expression and do a match with that string, and that token doesn't exist, it's going to just say nil.1442

The next regular expression matching we're going to look at is the String object; this does the exact same thing, but we're starting with the String object.1455

We have this match method that is part of the String object; it allows regular expression matches and the reverse of the regular expression .match method.1463

Here I have a string: "Hello, my name is Mr. Smith. What is your name?"1477

At the beginning, we start with a string, and then we call this match method.1481

It does the exact same thing: it returns the match data.1488

It's flexible; we could start with the regular expression or the string, depending on how your code is going.1493

I have the string.match, and we pass a regular expression here, and it says nil.1502

The next thing we're going to look at is regular expression modifier characters.1514

This is located at the end of a delimiter, and it has one or more single-letter options.1520

This will control how the pattern can match.1527

For example, let's say pat is my regular expression pattern; this is whatever you put in those parentheses; this could be...let's try a digit character, space, another digit character.1532

That is our regular expression.1554

I'm just saying pat is just some pattern.1557

What is important here is these modifiers.1562

This i says you can ignore case--it doesn't matter if it's uppercase or lowercase; it will match it because I put that modifier there.1566

If I put an m, it makes the dot match newline, so if you put a period in a regular expression, it will match newlines.1578

If I put an x, it says to ignore the comments and white space in the pattern.1588

If I put a 0, it says you can perform interpolation--only once, though.1592

Let's take a look at this...we have our example here again, but notice with my match I used a lowercase n.1598

Notice, it still matches it: the reason it does is because of this i here, which says to ignore that case.1607

Again, for that same setup, if I go using the regular expression object, and I have that modifier i, it will get the same result with that match data.1616

The last thing I want to talk about is the regular expression modifier objects.1635

We have Regexp::IGNORECASE, Regexp::EXTENDED, and Regexp::MULTILINE.1640

You can pass these in as options in your constructor.1646

We have our Regexp.new; you have your pattern here; and then you can pass the options as a second argument--and this is optional.1651

For example, I use that same sentence--we've been really pummeling this sentence with regular expression matches--and I say IGNORECASE.1677

It's another way to do the modifier in the constructor; this is the way you would do it.1690

And then, that will match that string, even though this m is lowercase.1694

Let me show you, in the terminal, how you can do this.1702

You can pass an array as an argument, so let's go ahead and use the example again: for options, I'm going to just pass an array...Regexp...IGNORECASE...1716

These are constants that are part of the Regexp object...Regexp::MULITLINE...notice, I've passed in all three options.1732

Also, notice that these values--these constants--are just flags as part of that object.1744

Then, let's create my string that I want to work with.1754

"Hello, my name is Mr. Smith. What is your name?"1759

Then, I'm just going to create my regular expression, and then pass in options...let's do a match string.1766

I'm creating a constructor--it's calling new--and the token I'm looking for is the Mr.1782

I can also choose it as a regular expression pattern.1796

These options...that array, I'm just going to match it with that string.1800

It actually didn't find it...let's try putting back those quotes...and it does find it there.1808

That is the reason--because we're using a constructor here; we are not using the literal sense.1818

That is how to pass those arguments in.1824

Let me show you the RDoc for regular expressions.1831

We have gone through a lot of these examples already.1838

Here is the one with the forward slash and the Ruby regular expression literal.1843

Notice--we talked about this before--it uses the equal tilde sign, and you can pass a regular expression with the equal tilde and what you are looking up.1852

It will tell you the position it starts with; this one starts with 0.1863

You can also use the .match that we talked about.1867

We already went through some of this: metacharacters, we talked about...1877

Escapes--we didn't actually go over that, but you can escape the special characters, like the plus and the question mark.1882

If you're matching those, make sure to escape those out.1891

Also, notice that you can use a Unicode; here is an example of some Unicode.1895

That gets passed as some international characters.1900

We talked about how the character class uses those square brackets.1911

Over here, notice "aeiou"--if any of the second characters matches these ones, the second letter matches these ones...word matches, ward matches...it will create a match with that regular expression, too.1916

With the same character class, with 0-9, a through f, if it's one of those that falls in that range, it will match it.1937

Here, 9 does, and 9f.1945

We already saw the carat--this one says it's not a through e, not g through z...and it will match f, and it matches f there, because it doesn't fall in that character set.1949

We already went through these metacharacters.1965

You will see that there is actually quite a bit more here.1968

We went through the repetition, and there is also grouping that is allowed--so you notice these parentheses.1974

I will let you look at this RDoc so you can get more details on these regular expressions.1991

Otherwise, this is the end of the regular expression lesson, and thank you for watching Educator.com!1999

Welcome back to Educator.com.0000

Today's lesson is on arrays.0002

I know we have gone through some arrays with Ruby Datatypes Part 1, if you had it--be sure to review that--but this gives you a little scope on some more methods, and ways you can manipulate and change arrays and create arrays and delete them.0008

What are arrays? They are ordered, integer-indexed collections of objects.0027

They hold objects of different types; so in one array, it can hold an Integer...it can also hold a String...multiple types in one piece.0036

Let's look at creating an array with a block.0049

We already went over, in a past lesson, to just do .new with the size, but this time we're actually going to initialize it with this block.0055

What this does is, I can actually customize this block to tell it, whatever it returns at the last piece of information, that is what is going to initialize as the object, and it uses this index value.0067

To do this, it requires the parameter size to instantiate the number of elements; the size is an Integer value.0081

It requires a code block to define the value to set.0097

This block starts with the value 0 to n-1; this actually means the index.0104

Starts with 0 to n-1, which is this index value...where n is the size.0117

For this example, we have this array.new; I'm trying to get all the odd values, so I'm calling this array.new: it has a size of 10, and here is the index that it starts with--that is 0 to 9.0128

First, it's going to do 0 times 2, which is 0, plus 1, and that's 1--it's here.0148

Next, it's going to start the value 1--so that is 1 times 2 plus 1, equals 3.0162

It's going to do that for the size of 10.0172

So, we have 1,3,5,7,9,11, all the way to 19, and we have our odd values.0177

Remember that this block...you can put what you want to put there, so if you are going to instantiate a new object to fill in this array, you just pass in the constructor to that.0188

Again, I have my odd values here from this last example.0202

But, notice, I call float with that x value.0207

What this is going to do is, it's going to return me that array again, but in Float type.0212

So, I'm going to get 1.0, 3.00, and that is all going to go to 19.00.0217

In this next part, we're going to look at alternative ways to create an array.0232

The first one--I've rarely seen this before--you call array, put a period, and then you put your square brackets, and then you pass these parentheses with your elements.0242

When you do that, it's going to create you this array right here.0257

Now, the returns for these three different ways to create an array are all equal.0260

In this second part, we are using the Kernel; it's going to call array with the square brackets--[abcde]--oops, I meant to put an f here, too...0270

Then, it will return [abcdef].0287

This last one is probably the easiest and the most popular way.0292

You will be using it, and you will notice it makes the exact same array as these ones up here, but I didn't have to call array.0297

Just take the literal array for that.0308

Next, we are going to look at checking the classes in the array.0314

We are going to go over iteration and class types.0321

This will iterate through the array--we're going to iterate through the array--and then we're going to call the class method.0325

We're actually calling the class method on the objects in it.0332

If I have this array object--here is my array object--this could be anything defined in here--and I call .each, it's going to give me each element in that array.0337

I'm going to print out that class.0356

When I do that for A, B, and C, and then I call .each, it's going to print me String, String, String--which is the class for it.0358

If I had a different...an Integer, it would print out Integer there, too.0373

That way, I can know all the different classes that are being used in the array.0376

When I put in my own code, I'll probably not just print it out; I will probably look at these classes and do something with it--manipulate it or store it somewhere else, depending on the class--or convert it to a different class.0381

The next one we are going to look at is array shortcuts.0397

Here you have this method at, and it has the index you want to look at.0401

When I call the at method with that index, it is going to return me the element at that index.0410

Here is an example: I have a = [a,b,c,d,e]; for the index values, 0,1,2,3,4.0418

So, if I call a.at(0), it's going to return me the element a.0431

Ask for 2: it returns me c; ask for 4: it returns me e.0438

Now that you know the at method, you can move on to the delete_at method, which will probably be equally useful, I assume.0445

Same parameter: you pass the index; what it will do is, it will remove the element at the specified index.0455

It returns the element deleted; so it is not going to return you the array after you delete; it's going to just return you that element that it just deleted.0463

But, that array, it will update it itself.0472

Otherwise, it returns nil, if the index is out of range.0477

If it's out of range, it is going to just return nil; it's not going to delete anything.0481

Here I have our example, a=[abcde]; I'm going to delete_at index 1; it's going to remove this b; and then, I'm going to get [acde].0487

Next, we are going to look at some array shortcuts.0507

The first one is this first method.0512

I pass in n: what this does is, it returns the first element--so n here is optional.0515

I can just pass in first, and it will give me the first element in the array.0532

Now, what happens is, if n is passed in, it returns the first n elements of the array.0536

Notice here, for my example: I call a.first--it returns a; but if I call first with 3, it's going to get me the first n elements.0545

My n is equal to 3 here, so it's going to get me [a,b,c]--and notice, it also creates an array for that.0556

Again, we can do the same thing with last: this is another method--it returns the last method, and it is optional if n is passed.0568

It returns the last n elements of the array.0577

So, when I call a.last, it gives me e, but if I do 3, it gives me the last n elements--in this case, n=3, so [cde].0580

Now, let's look at removing duplicates.0596

I use a method called uniq here-U-N-I-Q; you can also call it on itself with a bang.0602

This removes the duplicate values; no changes if no duplicates are found.0609

For example, we have this string--it's a basket full of fruits: Apple, banana, apple, grapes, pear, banana.0618

Notice, we have a couple of duplicates--banana, apple--and I'm going to call uniq!, so it's going to update itself.0627

Now we have no more duplicates: when I call basket again, it's going to have apple, banana, grapes, and pear.0640

Let's look at sorting the array with the sort method--there is sort and sort!.0650

This sorts the elements using this less than, equal to, greater than operator.0657

So, if I have a group of numbers that are unsorted right now, I call the sort method, and now it's going to order them from lowest to highest into [5,6,7,9]--this is after I call Numbers.0665

This .sort! will sort it and update right on that line.0683

Now, we're going to look at getting the index.0694

There is also a method called index, and it returns the index of the first object it sees with that.0699

So, you call index, and then you pass in the element that you want it to find.0710

For this example, I have a=[abbcd].0721

I'm looking for the element b; it tells me the index is 1, because that is the first one it found.0727

Even though there are two b's here, it is going to go from the 0 place and move down the ranks.0734

It's going to transverse like this, all the way down, to find it; and since it found it here, it's going to stop, and so it gets the value 1.0743

But, we can also do the rindex; that does the reverse--it starts from the back of the array, to the front.0753

Here, I call rindex(b); it's going to start from d, and notice, it finds it on the second place--the second index value.0763

Next, we are going to look at multidimensional arrays: an array that holds an array of arrays.0776

This is essentially...you can hold arrays in arrays, too--so that is what a multidimensional array is.0783

For our example here, we have Numbers--we have an array, and then we have our level 2 array right here, and that is our array in an array.0791

The reason I want to bring up this multidimensional array is, now we are going to look at this method called flatten.0806

What flatten does is, it returns an array that is a one-dimensional flattening of itself.0812

So, when you call flatten, it will update itself with that.0819

It flattens recursively, so if I have an array of an array in an array, it's going to flatten the whole thing.0822

Let's take this example with Numbers; I call Numbers.flatten, and this is what I'm going to get. It's going to just be one level of array, [1,2,3,4,5].0832

Other than that, that is the end of the array lesson.0856

Be sure to go back and check out the Ruby Datatypes Part 2, where we have more information on arrays.0861

That is it, so thank you for watching, and see you at the next lesson!0871

Welcome back to Educator.com.0000

Today's lesson is on hashes.0002

What are hashes? Hashes are unordered collections of key-value pairs.0009

They maintain a set of objects known as keys, and each key is associated with a value.0020

Think of it this way: think of a hash as kind of like a table.0033

In this table, you have two ways to look up the values: one way is through keys, and another way is through values.0046

This pretty much sums up how the hash is created.0061

How do we create a hash? You can create a hash in many different ways.0077

The first way is through the constructor--just using hash.new.0084

This is using the normal .new method.0089

The other way, which is more commonly used, is through the curly brackets, which you will see in other languages, too.0098

This is method 1...number 1 and number 2.0108

This is probably the preferred way you will actually see it being created.0112

With the hash, you could also pass in an argument, and if you pass in an argument, it will actually set a default value to your hash.0122

So, if I create a new key, and I were to access it, you would actually see it as a default value if there isn't one that already exists.0130

For example, I have this hash.new, and my default value I'm passing in is the default value 0.0140

Notice: I have this new hash; I haven't declared any keys yet; but then, I'm actually assessing new_key, and it says, "I'm going to give you the default value of 0."0152

You can also...notice that this is optional, but this is our default value here.0170

Now that we have created our hash, and we know how to make a default value for it, we can also run some common hash methods to say, "Is this a new hash?"0194

"Is this hash new? Are there elements in this hash?"0208

One thing we can use is this method called empty?.0212

If you call newhash.empty?, it's going to return true, because we haven't actually added any keys or any values to it.0219

This returns true if there are zero keys.0230

Another method you could look at is this size method.0237

It also returns 0, because there are no keys or values in this hash yet.0244

Now, we are going to look at accessing the hashes.0256

There are many methods and ways to do it.0260

You can access the hash by the key or by the value.0266

These keys must be unique.0272

You can't have a hash with two different types of keys; if you do that, one is going to take precedence over the other, and, in this lookup table of keys, you are only going to have one.0278

It's going to overwrite what is currently existing there if you try to add a second key.0291

By default, keys that do not exist will return nil.0299

You will notice that, in our last slide, we set a default value so it wasn't returning nil--it was returning 0.0305

To create this hash, another way I could do it is using these square brackets.0315

This is kind of an interesting thing just in Ruby; it allows me to use the square brackets and create a value order, and it will create a hash from it.0327

To do this, essentially, I'm going to call a code piece...I call hash--key word--and then, in the square brackets, I'm going to give it an order of the key, the value, the key, the value...and I'm going to continue down that whole list.0340

Notice that this will be an array that has an even number of elements, because of this ordering.0375

When I create the hash using these square brackets, then it's going to convert that to a real hash.0389

For example, I'm using the exact same syntax you see here with the hash square brackets: my first key value here is 2016, and then my value is Brazil.0397

I'm creating a hash of all the Summer Olympics, from newest to oldest.0414

The next one is 2012--that is my key--and my value is London.0420

Let me put a little 1 here, so we know where they are going to.0430

And then 2008--that is a key again--Beijing is a value...this was actually 3; then key--2004: Athens is a value.0438

Notice, after I do this, I push Return; it is going to convert this square in square brackets--the array--and it will make that a Hash.0457

So, we have our hash here.0477

Notice that it automatically converted it to these key-value pairs to follow our hash syntax.0481

Another way you can create this hash is using the curly brackets.0503

We already saw how to do it using the constructor.0510

Now, we can also create the hash by defining the values in there.0513

This is going to make exactly the same end result as the square brackets, but this is a more common way that hashes are built.0518

Let me add here "commonly used"...because it's commonly used in Ruby.0530

Again, we use the exact same thing; notice here, instead, we are using this hash rocket here; this is my hash rocket: it says...I have this key, 2016, but it is pointing to its value, Brazil.0548

I'm using this hash rocket continuously with these key-value pairs: 2012 to London, 2008 to Beijing, 2004 to Athens...and after you press Return, it is going to return you the exact same thing, because you have already defined the hash.0569

It's going to say, "OK, your Summer Olympics points to a hash reference, and here are the values to it."0585

You can also add spaces to this, so you don't have to have all of this in one line.0595

Your coding might be more organized, also.0605

Let me show you how you do that.0608

I have my Summer Olympics, and notice, in my code, I'm going to start off with the first curly brace.0612

I press Return; it doesn't actually end the code--the code will continue--and now I can make it more easily readable.0628

So, I can put here...2016...have my hash rocket...Brazil.0637

Come down to 2012--London.0644

I continue down to Beijing...and 2004, Athens.0649

As you see, the end result: it's going to return the end hash that I created.0662

Also, if I do Summer Olympics, and I just do Return, it also returns the same hash reference.0670

As you can see, Ruby allows me that flexibility; I can add those newlines, those break lines, and I'm still defining the same hash.0680

It just makes it easier for me to code this up--and it makes it easier for people to read, too, which is nice.0690

OK--we went through a lot of hash creation here; let's go back to accessing these hashes.0704

We're going to look at iterating these keys and values.0712

I have my hash already; I've defined it; I have my values; now I'm going to start accessing these keys and values.0716

The first thing I want to do is look at a method called each_key.0723

This goes through my hash, and I can go through all of the different keys in it, and I can do my own code manipulation to what I want to do with it.0729

To do this, you call your hash, hash method each_key, and you pass in these curly braces, and in there, one argument you will get is key, and then block is your actual code.0739

This is going to go through each of your keys and pass in that key value with it, and then, in your code block, you can manipulate it, store it somewhere else...do whatever changes you need to do.0757

It will iterate through your keys, passing key as the parameter--and this is your key as your parameter, right here.0773

For example, we are going through a hash of colors.0783

Again, I have my hash rocket; 1 is defined 'blue', 2 points to red, 3 to yellow, 4 to green, 5 to black.0791

If I run the BestColors, and I just call method each_key, it's going to say that I want to print the keys, one by one.0803

It's first going to print the first key, 1; then it's going to print 2, 3, 4, and 5.0816

After I return, you will notice that the end result is that it's going to show the output 1, 2, 3, 4, and 5.0828

In the same way you have each_key, you also have a method called each_value.0844

With the each_value...it goes through each value, one by one.0849

You have your list of values, and it ignores the key; you're just going to go through all your values.0853

Same thing: here is your method; your parameter is val; and the block it's defined to.0862

Now, the val is something I've defined; you could actually call it something else, but that parameter, val, will be your value.0873

Let's go through an example with the BestColors again--the exact same example I just showed you.0884

For our code block here--let's put this as our code block...this is our val, and each value--notice how this, down here, matches up with the syntax up there.0890

Our code block says, "I want you to print the value, and then append a space there."0913

I press return; the first thing it's going to do is to print out the first value, blue; add the space; red; space, yellow, green, and black.0922

You take this code; you put it in your IRB; press Return; the result will be blue, red, yellow, green, and black.0938

Pretty easy, huh? It's pretty simple, but it's nice to know that you have these methods to handle these things and get things out of the hash that it wouldn't normally do.0953

Next, let's look at merging hashes.0966

We have this method called merge: it takes in a parameter called other_hash.0970

I call it other_hash; it just takes in another hash, OK?0978

This method will return a new hash that contains the contents of your other hash and your self.0982

It's going to merge them together.0992

Duplicate keys will return the other_hash entry.0994

So, I have my hash here in a, and I'm merging hash b: b takes precedence--that is what it means.1000

Let's look at an example.1012

I have a hash called NorCal and one called SoCal.1015

In my NorCal hash, I have the key value 1 going to San Francisco, 2 to Sacramento, and 3 to San José.1019

For SoCal, I have 4 going to Los Angeles, 1 to San Diego, 6 to Anaheim, and 7 to Irvine.1030

Notice one thing: San Diego has a key 1, and NorCal has a key 1, so there is a conflict here: they have the same key.1039

Now, here in my code, I'm going to call NorCal.merge(SoCal), and I'm creating a new hash called California.1052

I press Return on that: the end result: 1 is going to point to San Diego, 2 to Sacramento, 3 to San José, 4 to Los Angeles, 6 to Anaheim, and 7 to Irvine.1063

There is no SF anymore; SF was removed.1080

Since we merged the SoCal key with San Diego in, that has overridden SF; so it removed that key-value pair altogether.1088

There is no more of this key-value pair here.1102

But notice that, now, we have a bigger hash that combined the two.1109

Next, let's look at sorting hashes.1123

To do this, I have a method called sort: it has two parameters--I'm just calling a and b here, and then you have your normal code block--whatever you want to sort it with.1126

This sort method is extended from Enumerable, and this allows you to sort elements using the Enumerables' sort method here--the less than, equals, greater than sign.1138

For this, we have NorCal: I have my example here again--I have three cities in NorCal: 3 points to San Francisco; 2 to Sacramento; 1 to San José.1155

I do puts; it's going to output that; I'm going to have my hash.1169

But now I want to sort it, but I want to sort it by value.1173

So, I take this structure here, and I'm going to follow that down here--notice, here I have the exact same structure.1179

I have my key-value pairs, a, b, and when I put this [1], that's going to say, "Look at the value, because I want to sort by that value."1194

The end result is: if I press Return on that, Sacramento is going to be first, San Francisco next, and then San José.1208

Also, if I were to use a, zero, b, zero, I could also use that to sort by key.1222

So, depending on your code, that might be more significant.1243

Let's now look at a new method called replace.1255

This is for replacing your hash; it takes your original contents, and it replaces them with the new contents of this other hash.1262

So--overwrites the contents of self with the contents of other_hash...1272

For our example here, I have a hash of three key-value pairs, a, b, c, and then I call the .replace method.1277

I have: a points to 1, c to 500, d to 550--and you will notice that the end result has...the value hash has...only these new elements in there.1291

These two hashes are the same, and the contents of the original one are gone.1308

That is what this method, replace, does.1320

Let's go ahead and look at converting the hash to other classes.1325

First, let's look at the Array class.1332

You have this to_a; if I call this on a hash, it will convert that hash to an Array.1336

We have an example here: I have my hash, a hash of fruits; the a key goes to apple, b to banana, c to carrots.1344

I call .to_a, and it will actually create a multidimensional Array.1355

I have an Array of an Array.1361

Notice, I have a, apple; b, banana; c, carrots; these are my original key-value pairs in the hash, but now they are Arrays of two elements.1373

The next one we will look at is converting to a string.1394

To do that, we have this method called to_s.1402

This is the same method as inspect; if I were to call inspect or .to_s, it would do the identical, same thing.1406

This converts it to a string.1413

For this example, what I wanted to do is take this hash and convert it to a string, but make it really clean, so I actually chained a bunch of methods here...so I'm using some chaining...1416

I have my original hash; then I call a bunch of different methods: I call to_s first to convert it to a string; then I call this gsub method, which is going to remove a lot of stuff that I don't need--that I wouldn't visually be able to see.1435

Then, I call this split method to break it down by the comma and the space.1454

Now, I do want to show you this example to show you how it's doing this, so let's go to our terminal.1463

First, let's go ahead and create our Array.1475

Let's make it even cleaner here; I'm going to use some newlines.1482

OK, so I first created my hash; I'm going to do Foods.to_s.1496

You will notice that I have a lot of stuff here: you have your backward slashes to help escape out those quotes, and you have actual quotes in front of that, and I don't need that, because visually, this does not look clean.1503

I want to remove some of those weird characters.1520

I'm going to create a new variable called nomoreweirdcharacters, and then I'm going to call that again with a string; then I'm going to call my gsub method, and with my gsub, what I want to do is just remove those out of the way.1524

Let's see here--first, I'm going to remove that quote; then I'm going to remove that curly brace, the other curly brace...and let's see if this does it.1541

So, I said to remove the quote, the left curly brace, the right one, and it's a regular expression literal...let's see if that does it.1558

Yes! So, now I have my variable without those characters anymore, but then I want to break it down into an Array so it breaks it out more.1569

OK, call my split...there you go--that will remove that.1597

The end result: we have an Array of three elements; it still has my hash rocket in there, and it looks nice now.1609

That ends up this chaining method, and you got to see that to_s in effect, plus some more methods to massage it even further.1629

Otherwise, that is the end of today's lesson on hashes.1644

Be sure to check out Ruby Datatypes Part 2, where we do go over a lot of the basics of hashes, and it will get you up to speed even further in the hash realm.1649

Thank you for watching Educator.com, and see you next time!1665

Welcome back to Educator.com.0000

Today's lesson is on math operations, part 1.0002

First, let's go over a review of some of the math objects--first, the Numeric class, and then some of the math modules.0009

First, you have your Numeric class: this is the base class for all of your numbers.0023

There are quite a bit of numbers: first, you have your Integer class--this is the basis for Fixnum and your Bignum class: it's the basic Integer values for that.0034

Then, you have your Float: Float is a class for real or floating-point numbers.0057

You then have your Fixnum, which is the most common one--which is your Integers.0065

There is a max bound it holds: it holds integer values represented by your native machine word -1; this is based on your local environment that you are coding in.0071

This is the bound--max bound.0090

If it flows outside of that bound, it's going to be automatically upcased to a Bignum.0098

You have your Fixnum, your Bignum...and Bignum is a class of integers outside the range of Fixnum.0107

Then, we have our class called Rational, which is a paired Integer number.0118

The last thing we want to do is--we have this Math module; the Math module holds math-related functions.0128

Mostly, it holds more basic ones that are for trigonometry and transcendental functions.0137

First, let's go over some of the basic operations.0153

You have your normal add, subtract, multiply, divide; you have the modulo, which gets the remainder of the division.0158

That is with the percentage; your caret gets you the exponent; so this is equivalent to a to the power of b: it performs exponential calculations.0169

We can go ahead and go through some basic ones in the IRB.0185

I can show you the addition 10+2; it's 12; 2+10...it got 12, too; 10-5...2-7...does it not like my comma?0191

10-5; 2-7...it allows negative numbers or positive, no issue...10 times 10, 100...5 times -10, 50...I can still do division--20 divided by 4, 100, 10, 12, 10...0210

Notice, for 12 divided by 10, it is not a floating number; it's just a 1: there is a remainder, and it cuts that off.0231

Let's go ahead and look at the modulo: I can do 12 modulo(10), and then you see, there I get my remainder, 2.0242

If I do 15 modulo(5), since there is nothing, it's going to return 0...so 15modulo (5) returns 0; there is no remainder. 0252

If I want to do 2 to the power of 3, I just do 2^3, and the value is 8; and I can do 3^3, and the output is 27.0264

That is basic operations.0284

Next, not only do you have these operators you can use; you can still use the methods--like the divide.0292

We have this method called div: it takes an argument--you pass in a numeric value to divide by.0302

It will perform division, and then it is going to convert the result to an integer.0310

This is defined by the subclass of Numeric, so in this case, since we are used to doing Integer values, it's going to be defined in the Fixnum class or the Bignum class, depending on where it's being set; this would be the Integer class.0317

So out of the 24, I called the .div(2), and I'm going to return the result 12.0331

13.div(4)--it's going to return 3.0340

We do have that remainder, 1, and notice it doesn't worry about that.0346

This method says, "I'm just dividing and returning you what I'm able to divide by."0353

25.0--this is a Float--so notice, for these first two ones, we had Integers; for this one, we're doing a Float.0358

It's going to be 25.0 divided by 2.0, and it still returns an integer--it returns 12 still.0373

Next, we're looking at this method called divmod; this one gets us more specific to what we want.0387

Not only do we want what it divides by--we want the remainder, too.0391

This will return an Array that contains the quotient and the modulus.0395

Again, we go ahead with our 13 divided by 4, but this time, we call divmod; it's going to return an Array.0403

Here is our value 3, which is our quotient, and then we have our remainder, our modulus, which is 1.0413

15.divmod(5)--we have 3 and 0; 17.5.divmod(5)--we get 3 and 2.5.0425

Next, we are going to look at the method modulo.0440

This returns the modulus obtained by dividing the num by the Numeric.0444

This is where...we've talked about the quotient, we've talked about both, and now this will just return the modulus remainder.0453

13.modulo(5)--this allows us to divide by 2, and we have this modulus of 3, so that is the end result of 13.modulo(5).0461

Notice, if we do a Float--I do 11.0, a Float, and I do a modulo 5.0, which is another Float, it's going to return 1.0.0476

Let's look at some more methods that you might possibly use.0495

The next one: we have this method called quo, and again, it takes a Numeric value.0503

This returns a Rational or a Float, and it performs division.0510

This one--you can get a much more specific value, since we're getting Float types or Rational types with it.0517

If I do 10.quo(5.1), we get this huge number--very specific.0527

If I do 10.quo(2), it's going to return me a Rational value, which is 5 over 1, and then here we have our Float.0534

It will return whatever it finds best to get you the value you need it to be--Rational is more specific, but if it needs to be, it will return you that Float.0550

The next method we will look at is remainder: very similar to the modulus, but it does have a different way to calculate the remainder.0563

It returns a remainder, and how it does it is...let's say x is your self, that's the number--you call your remainder...Numeric value...to do that, it actually does a calculation using truncate.0571

It does the same thing as this here.0592

What it's doing is x-y times x/y; and it's doing the .truncate.0597

If you call this method--you know now the innerlyings--this 10.remainder(2), it will return 0; 10.remainder(3.3), it will return 0.1 and a bunch of zeros and 53.0609

Next, let's look at some of the operator-operation precedence; you've been looking at a lot of different code piece examples, but if you have this huge line that is all this code of math, there is a precedence it follows--some steps, some operators, versus equality pointing to references--they all have a scale of how the compiler is using it.0633

It looks at this line of code, and it says, "This piece has precedence over this piece in this line, so I'm going to do this action first, before I do any of the other pieces."0660

It is very important that you look at the operator precedence; you can also control it using parentheses to scope out which one you want to give higher priority.0669

But, it's always good to look at the precedence so you know what the compiler is doing and how it thinks to break down your code.0677

We are going to go through three slides of it, from the highest precedence to the lowest.0688

The first one that has precedence is just element reference; notice that you can get the element reference using just square brackets, equal.0693

The next one that, surprisingly, has second precedence is the exponential--the raised to a power with the ^.0707

Third is the unary plus and minus--you have your +, -, your !, and your ~.0718

Then, the operations--math operations are next: your multiply, divide, modulo...and right below that is addition and subtraction, so your plus and minus...0727

After those, your next precedence is the right and left bitwise shift, with the > > < <, and then you have your bitwise operations: bitwise and, bitwise or, and regular or.0744

After that, your next precedence is the comparison operators: you have your less than/equals, less than, greater, and greater than/equals signs.0763

Then equality and pattern match operators come next: notice our Enumerable sort method is here; we have our equality, equality by types, our not equal, approximately like for regular expressions...0771

We have gotten through our first slide.0794

Next, logical 'and' and 'or' come next on our list of precedence.0797

Then it's your range, your inclusive and exclusive, two dots and three dots, and then your ternary if...then...else: we haven't actually done any code with this, but it's kind of a shortcut that allows you to do if...else statements all on one line.0804

It would be like if I have a condition; I can do this and say, "If this condition holds true, do a; if it doesn't hold true, do b."--that is a ternary if...then...else.0823

The next precedence is assignment: you see you have your assignments here, with add things together...subtract...divide...do bitshifts...0844

Next is logical negation--not--and now we have gotten to our last three: logical composition--our or and our and; then, it's our expression modifiers--if, unless, while, until; and then the last one on our operation precedence is the block expression, which is begin and end.0856

Next, let's look at the abbreviated math operations.0890

A lot of times, when you code, you are going to see yourself calmly doing some things over and over again, so Ruby does allow some shortcuts.0897

One of these is...you don't have to repeat yourself and keep trying...so, in this case, with the addition/subtraction/multiplication/division, you can actually shorten it.0906

So, if I wanted to add 6 to this value, x, I can just do x+=6, and this is equivalent to x=x+6.0920

It's going to add the value in, and it's going to set it back to itself.0935

I can do that same thing with -=6, so it's x=x-6; same with multiplication and division.0940

The only thing you need to do to do this is, you just move the operator in front of the equals sign.0950

It would be x, operator, equals sign, and then whatever numeric value we have.0959

This can be taken for plus, minus, multiplication, division...and now, we just shorten our code a few characters to save us a little typing.0978

Next, let's look at numbers.0994

First, to do this, we will look at the Numeric class.0997

The Numeric class is the basis parent class for Bignum, Fixnum...all these classes...they all have an ancestral link, and they all are part of the Numeric class.1006

This Numeric class gives you many methods to describe different numbers.1022

We're going to look at five of them: I have this num--I've set it to value 100--and the first method we're looking at is, "Is this zero?"1029

And zero? is false; it isn't zero; it would return true if it were.1045

Also, there is this next method called nonzero?; nonzero says that if it is nonzero, it's to return self--in this case it would return 100--or else it's going to return nil, so if it is 0, it's going to return nil.1052

Notice, it returns 100 in this case.1068

The next method is real?--"Is it real?"--it returns true; 100 is real.1074

"Is 100 an integer?"--it returns true.1079

Next, we have this method called absolute value, abs; this will get you your absolute value.1085

Notice, I have my minus sign, -12, and I just call .abs, which will call that method, and it will return me the absolute value, which is 12.1101

Let's look at a few more Numeric methods.1121

Here, we have...first, we will look at ceil: it returns the smallest integer greater than or equal to num.1125

It does kind of a round up; so, for example, I have 1.5, and I call .ceil; it's going to go to the next highest integer--in this case, it's 2--the next highest integer, the smallest one.1139

Next, let's look at floor: floor returns the largest integer less than or equal to num.1167

So, if I call 1.5.floor, what's the lowest one from 1.5? That's 1, so that is what it is going to return to me.1174

Then, you have this method called round; round will round a number to the nearest integer.1185

For example, 1.5.round will round up, so it rounds up to 2, and if I call 1.4.round, it's going to round down to 1.1197

Let's look at another example with the numbers--bring all of these concepts in together.1218

First, I'm going to set x to a Float, 100.5.1225

Next, I'm going to say on my next line, "If this is not an integer, I want you to round that number."1231

So, what I'm doing is I'm saying, "I want to make this an integer if it isn't already."1241

100.5 is not an integer, so this statement is going to return true, total, because it's not an integer, and I'm using the exclamation point to negate it, so it's going to return true.1246

It says, "OK, if this is true, I want to round this number."1268

It's going to round that number, and it's going to get 101.1273

My next statement says, "Is it real?"--so, is 101 real?1277

It will return true for that, and so it's going to say, for this statement...remember to break it down; this is an abbreviated statement--this is doing x=x/10, and this is actually 101 divided by 10, and the end result is just going to be 10, which is this here.1286

x is real; it's going to divide this; and the end result is 10.1317

At the end of this code, it's going to do puts x, and that is going to output this value, 10.1325

Let's look at one more method, truncate: truncate returns num truncated to an integer.1339

We are not doing any rounding; we're not doing ceil or floor; it's just going to cut off whatever is at the end of that decimal point, and make it an integer.1349

If I do 1.1.truncate, it's going to truncate all of that out, so it's going to just be 1.1359

If I do math.PI, which is a constant, and do .truncate, it's just going to return 3.1369

The last one is this step method: it will take whatever integer you give it, and it will increment it one by one, depending on what step you want to do.1380

You can also pass in a limit, which says, "Continue incrementing until it hits this limit."1398

For example, we have our num and our method step; here is our limit: pass that first, then pass the step--how much is it incrementing it by each time?--then, you pass in your code block.1406

First, it's going to invoke the block, starting at the number, and then it's going to increment it by the step value.1424

Then, the loop is complete when the value is greater than the limit; once it goes over that limit, then it's going to stop.1434

An example is if I call 3.step, and I do...here is my limit: 10 is my limit...and then 3 is my step, and in my block, I just say, "Print which value I'm at now; what is my current step value?" and then add a space.1442

When I do that, it's going to start with the initial value 3, so it's going to print 3, 6, and then 9.1473

Afterwards, it stops, because then the value would go over the limit.1486

If I do that again, this time starting with the value 1, the limit is 10 and the step is 2 (again, we are doing the same with printing it out), it's going to be 1, 3, 5, 7, and 9.1493

That is all for the step method; let's go ahead and look at the RDoc for the Numeric class.1516

You will see that a lot of the methods here, we have covered.1527

Let's look at some of the methods: we talked about modulo...there is a unary plus and minus..."Returns the receiver's value"; "Returns the receiver's value, negated."1534

Abs: absolute value of num; if you run abs2, it returns a square of itself.1555

There are some other methods: phase, angle; we talked about ceil; we talked about coerce in an earlier Integer lesson.1565

Conjugate; denominator; here is our div--you can see it even has our same reference here--it "uses the / to perform division, then converts the result to an integer."1578

"This is left to subclasses."1596

Also, notice that it tells you the result: it returns an integer, which is interesting: when we cast a Float in, it will return an Integer, too--that is the reason it's defined here in the RDoc.1601

divmod returns an Array; it has a nice chart of how it does it, too, with the divmod, the modulo, and the remainder.1613

It has the eql? method--"returns true if num and numeric are the same type and have equal values"1630

It looks at the type and the value: don't let that throw you off--if you look at two different types, it's going to throw false at you.1639

There are floor; complex values; imaginary; integer...1649

Magnitude also does the absolute value; polar returns an Array.1659

Real "returns true if num is a Real" number--non-complex.1666

Remainder..."singleton_method_added" traps "attempts to add methods to Numeric objects"--you could add your own method to the Numeric class.1672

Step: we already talked about it: they have a very specific definition to how it works.1682

You can look at it there, if you want to dig into it.1687

They have methods to convert to a complex value, integer...and we already talked about truncate and zero?.1693

That is the Numeric RDoc.1702

That concludes our first part of our Math Operations.1706

We have had quite a bit of Numeric methods that we have gone over, and we will go over some more in the next lesson.1712

Thank you for watching Educator.com, and see you next time!1721

Welcome back to Educator.com.0000

Today's lesson is on math operations; this is the second part.0002

For today's lesson, we are going to go over five pieces that are part of math operations.0010

The first one we are going to look at is the Math module.0019

This has quite a bit of operations; most of the operations are related to the Math module, so I would give this a star.0023

After that, we are going to look at some of the math types.0038

One is the rational numbers, and after we look at rational numbers, we're going to look at complex numbers.0045

Then, prime numbers, and the last one we're going to look at is matrices, since they do have an object to create and manipulate matrices in Ruby.0053

First, let's look at the Math module.0072

This is a module that you can include in other objects; you can also use it by itself.0077

It has two constants as part of it: one is pi, and e.0089

To call it, you just call the Math::pi function, and the same with the e: you can just call it with Math::e.0095

The M is uppercase, and the three letters after it are lowercase.0129

That will get those two constant values.0138

The Math module contains a lot of methods for trigonometry and logarithms.0143

It can be invoked through the Math name space or included into scope.0152

Let's look at some of the methods.0164

The first one is atan2; it takes two arguments--in this case, we're giving it an x and a y.0167

This calculates the arc tangent, given the y and the x, which are these two values.0178

The next one is cosine; it uses the method cos; it takes one argument; and it calculates the cosine of x.0188

For our example here, all you do is to call the Math module directly; then, do the period, and call the method cos.0199

In this case, our argument is 0, and that comes up with the cosine 0, and the value is 0.0211

The next method we'll look at is this exponential function.0219

It uses exp, and this is e raised to the power of x, so it is equivalent to ex0224

For example, we have here the exponential to 1; e to the power of 1 is 2.7182, and it just keeps going down--81828459045.0241

We do call Math here, but you don't have to; you can include the Math module, and it will be part of your scope, and you can just call the function.0262

Let me show you how to do that.0274

For example, first, I'm going to do include math; and now I'm already in the Math scope, so I just need to pass the function in.0279

For example, before, I did math, log(1) and it gives me the value for the logarithm 1; I can just call log(1) now, and it does the exact same thing--since I included that Math module into my scope.0292

Also, I can call the new function we learned, the exponential function, to the power of 1, and I can call cosine and pass in 0, and it gives me a value of 1.0.0306

Actually, if that is the case, this is the wrong one; this is 1.0, as we just saw in that terminal.0323

The next one, as we just saw--we just looked at the logarithm function--you call log, and you pass the parameter x; this calculates the natural logarithm of x.0338

As we did in the terminal, math.log(1); you get 0.0.0351

You can also call the constant; in this case, we will do the constant for the e, math.e, and we pass the logarithm of that.0357

What is the natural logarithm of that value?--we get 2.7182, and it goes all the way down.0369

This logarithm function--we can actually pass two arguments in it; by default, it's going to do it as a base 10, but you can specify a second argument.0379

You can tell it specifically what base you want it to do.0396

This is the same as log, but the second argument specifies the base.0401

They also have another function that, if you are doing log10, it's going to default to, and you can actually specify that as a function.0410

I think this is made because it is so commonly used.0419

So, there is a function called log10; it takes one argument, x, and this calculates the base 10 logarithm of x.0422

Here, we can just call math.log10, pass in a value 1, and we get our value 0.0, our base 10 logarithm of x.0430

Again, the base 10 logarithm of 10 is 1.0.0441

There is also a function to calculate the sine of x.0450

Type sin, and pass in the parameter x.0455

You can also do square root and tangent: square root is sqrt, and it takes one argument of x; this returns a square root, and the value x must be positive.0464

You can also calculate the tangent with tan, and it takes one parameter.0481

There are quite a bit of functions; I have gathered a table, a list of them.0491

You can go through it and see all the different functions that you can use in this Math module.0498

I do have the bang value here; this bang value means that it will update the object it currently is and save it.0506

You also have the option to do the method directly, without the bang, which will create a new object with that new calculation.0519

There is arc cosine--you add an h; you have the hyperbolic arc cosine.0529

We already went through sine: you have arc sine, hyperbolic arc sine; arc tangent--this atan takes an x argument; atan2 takes two arguments, an x and a y argument.0538

You have atanh; this is the hyperbolic arc tangent.0556

This is the first page; there are a couple more here.0565

We already went over cosine; you can use the bang method with your cosine; if you add h, it will get you the hyperbolic cosine.0569

If you add sin, that calculates your sine.0581

There is an error function in math; do math.erf, and you can get the error function.0587

There is also a complementary error function with erfc.0594

You can calculate the base x of eular using method exp.0601

You can also calculate the normalized fraction exponent using frexp.0609

We are almost there; we are on our last page of functions.0618

You can calculate the hypotenuse using hypot; you can calculate the floating-point value that corresponds to the mantissa exponent using ldexp.0622

You have your sine calculations with sinh--you can calculate the hyperbolic sine; next is square root--sqrt does that--and your tangents--tan and tanh will do your hyperbolic tangent, with the addition of that h value.0641

Let's go ahead and look at the Math module.0666

If you look at the RDoc, you will notice that it has more examples about all these functions, too.0670

This is the Math module; notice that it has the two constants e and pi.0681

As we just went through that whole list, they do have it on here, too, with the cosine, cosh, sin, sinh...you can see the source for that, too, how it calculates it...0692

Notice also, here they tell you the arguments: some take one argument; some take two.0703

For the arc tangent, given x, it gives you some examples here.0710

You can take a look at that and see how they calculate that.0717

Actually, we didn't go over this one: there is a method for cube root: that is cbrt; it takes a parameter of numeric.0726

It returns a Float.0738

We already went through error: it "calculates the error function of x," and takes x--one parameter.0743

exp--e to the power of x--we went over that, and they give you some examples there, also.0750

There is a gamma function, and a logarithm gamma function, too.0761

As you see here, it's all our log functions that we went over: they overload it, where one has one argument, one has two arguments, one with a base value, and then there is one for log10...0769

There is actually one for base 2--log2, so that might be useful to you math calculators.0778

That is everything for that Math module.0791

That is the Math module; let's move on now to rational numbers.0800

Rational numbers: these are numbers that can be expressed as a fraction of integers.0806

Just integers; remember that; we are not doing Floats or decimal values; they are all like fractions, essentially; these are all fractions.0813

This is supported through the Rational class--remember that Rational class?--it will be useful in this case.0826

You must use Integers, not Floats.0835

To use this Rational number, you have to load in a couple of libraries.0840

The first library you are going to load is this require rational, and then you have to load require mathn.0846

We will go deeper into what this mathn library does; it's not required for rational numbers, but it's recommended, so I'm just going to say require that mathn library.0854

After you require those two libraries, then you can start using this Rational object.0865

For example, here I'm calling rational, and I'm putting my fraction in.0872

I put my fraction in: 4 out of 10; after I do that, I press Return, and it's going to automatically reduce down to 2 out of 5.0877

I can also do the same thing with 5 out of 10; pass that in, press Return, and it's going to reduce it down to 1 over 2.0887

Let's look at some more examples.0896

Here, I'm just going to do require.rational, and then I'm going to require mathn, and let's show you the 10 out of 20, and it reduces it down straight to 1 out of 2.0901

Rational 3 out of 4 gets me 3 out of 4; rational 1 out of 10--and let's do times 5--so we have 5 out of 10, and it reduces it to 1 out of 2.0922

Also, I can go over a whole number, and it does do 3 out of 2.0939

You can also do other functions; I can also do exponent, so I can do 1 out of 2 to the power of 2, and it will get me 1 out of 4.0946

I can also convert to Strings and Floats.0957

If I do Rational (1,4), and convert that to a string, it takes that whole value and puts quotes around it.0960

I can do the same thing with Float, and--look there!--it actually gives me the decimal value, .25.0969

That is rational numbers.0984

Let's look over that mathn library very quickly.0985

Mathn is a standard library; it's used for a lot of math functionalities; when used in conjunction with the Math module, it makes mathematical operations better.0990

That is just the gist of it; you use it when you are doing math operations; just require it.1004

It pulls in other standard libraries, and it integrates them with the rest of Ruby's numeric classes, so it's very useful, so definitely use this library.1011

I just gave you a very simple example here, but if I don't include this library and I did 1 out of 2, it will return 0.1022

That is not going to work for me; I want specific values.1030

If I require this mathn, 1 out of 2 will return 1 out of 2, because it's including these other standard libraries to help do calculations.1035

I can show you some examples: let's go ahead and start over and call IRB again--clean slate, nothing is loaded.1043

All we do here is, I just pass 1 out of 2, and it returns 0.1053

But if I do require.mathn, then I do 1 out of 2, and it gets me the correct answer.1059

I can also do calculations with the different functions: 1 out of 2 times 3 over 10--I get 3 over 20.1071

I can do a calculation with sqrt, and I get my complex value, with 0 and one imaginary number.1079

Next, let's look at complex numbers.1103

Complex numbers use the Complex class.1106

This is represented as a real number with an imaginary number; I just showed you a terminal view with that...so an example would be 0+1i.1112

There are two different types of complex numbers we can use: we can call this polar method, and it will return a complex object denoted in polar form, and I can also call rect, and that will return a complex object denoted in rectangular form.1126

You have polar and rectangular.1148

Here are some examples of complex numbers.1156

You just pass the Complex object, and you pass a parameter in.1159

I do Complex(1), and it's going to return me 1+0i.1164

You can also pass in a second parameter for imaginary numbers; so, if I call Complex(2,3), it's going to get 2+3i.1171

I can pass in these arguments into the rectangular or the polar method.1180

If I do Complex.rect and pass (2,3), it's going to get me 2+3i.1187

In the polar form, I pass (3,0); it's going to get 3.0+0.0i.1195

Here, it passes it in with the decimals; so it has some Float values there.1201

Next, let's look at the prime numbers.1210

This uses the Prime class.1214

It's included in the mathn library, and it starts generating prime numbers, starting from the value 2.1218

What is a prime number? It's a value that is divisible by 1 and itself.1232

Let's look at some methods.1239

One is this method called each: it takes one parameter--if you don't pass anything, it will default that parameter to nil.1241

I'm going to call ubound, which is an arbitrary positive number.1251

This is known as the upper bound of the enumeration.1258

With this method, you can output all the prime numbers to a certain bound, so once it goes over that, it will stop.1262

I'll show you an example of that.1275

The next one is this value called prime?, and this returns if the value is prime.1279

You call itself here, so this would be prime, and then you call the method prime?, and whatever number value you want to check.1287

This will return true or false.1308

Let's go through an example with the prime values.1312

I believe we already have mathn loaded.1318

Let's do an example: let's get all the prime numbers up to 10.1324

I do each, pass in value 10, and do prime, and then I'm going to output each one, and it starts from the value 2; the next prime number is 3; 5; and 7; it ends at 7--that is where we hit our bound of 10, so it doesn't go over that.1332

We can also show you the prime?--let's check: Is 2 prime? It says it is prime; 1 is false; 3 is prime; if I pass the value 4, it says it's false.1360

5 and 7 are true; they are prime.1377

OK, so those are prime numbers.1387

The last thing we want to look at is matrices.1391

Matrices are represented through the Matrix class.1395

This provides methods for creating matrices, plus manipulating and updating.1400

It requires loading the class beforehand.1408

For example, I'm going to show you the build method.1413

The build method for matrices has two arguments: you need to specify the row size and the column size.1418

If you don't specify the column size, it will default to the row size.1427

This creates a matrix of row size by column size.1432

For example, I call the build method, and I'm doing a row size of 2, and then I'm specifying a column size of 3.1437

I'm also passing a block in here; this block says what I'm going to initialize it to.1459

In this case, I'm saying to initialize it to 1.1465

It makes this new matrix that has values of 1 in it, row size 2, and column--3.1469

Let's go through the RDoc.1482

This is very basic, but let's go through the RDoc for it.1490

Here is the Matrix RDoc; there is quite a bit on methods here, actually.1495

"The Matrix class represents a mathematical matrix...provides methods for creating matrices, operating on them arithmetically and algebraically, and determining their mathematical properties."1503

I showed you the build method, but you will notice that there are quite a bit of methods to build matrices, using different methods: rows, columns, build, diagonal, scalar, identity, zero...1517

You can also access the properties of it with quite a bit of functions here: row_size, column_size, row, column, collect, map...1530

Properties of a matrix: you can check that, too--you can say, "Hey, is this matrix diagonal? Is it lower_triangular? Regular? Singular? Square?"1541

You can check these properties.1551

You can also do mathematical arithmetic with it, using multiplication, addition, subtraction, division, inverse, the exponent function...1556

You should have quite a bit of functions here to do whatever process you need to do.1567

You can use the eigen, the eigensystem, conjugate, imaginary, rectangular, complex arithmetic for the matrices...1572

You can also convert it to other types: you can use coerce, to_a, row_vectors, column_vectors.1581

You can do string representations with to_s and inspect.1588

There are attributes: you have your column_size, your rows...1596

If you go through here, you can see a lot of the methods: we went through build, and there are quite a bit of methods here.1600

You can create an empty matrix, just with the row_size and the column_size.1607

We already went over build, but notice, with build here, I can also pass in what row and column it's working on, so I can initialize values using that.1612

I can give it a dynamic value, as they do here: they say, "The value I'm going to pass in is column minus row."1623

Then, you have a bunch of instance methods to do matrix calculation.1639

You also have this each method as an enumerator, so you can grab elements by what part of the matrix you want.1652

You can get all the elements, the ones that are diagonal, strict_lower, strict_upper...1660

There are quite a bit of properties there.1667

You can also get the Hash code for that matrix...that is interesting.1673

You can also get the inverse of the matrix, using the inverse method.1679

As I said before, you can check "Is it lower_triangular?", and just say to the matrix, "Hey, are you lower_triangular?"--just call this method, and will say yes, it is, or it isn't--it's false.1683

There are quite a bit of functions here, so definitely look at the RDoc.1699

That will definitely go over a lot of different methods in the matrix, and help you manipulate, create new matrices, and update existing ones.1705

So, definitely go over that, and otherwise, that is the end of this lesson at Educator.com.1718

Hope to see you next time, and good luck on your Math module searches and processes!1724

Welcome back to Educator.com.0000

Today's lesson is on dates and times.0002

Today's lesson is over, "How do we represent a date and time in Ruby?"0010

We're going to go over the components of a date and time, and the three classes: Time, Date, and DateTime.0016

These are the main classes for the date and time functions.0032

The first thing we're going to look at is the Time class.0037

With the Time class, you can represent date and time.0041

This is a class that is a thin layer over the system date and time provided by the operating system.0045

Depending on your operating system, some platforms might have issues representing dates before 1970 or after 2038.0058

If you're on a 32-bit machine, it's more prone to having these issues.0067

The Time class--mostly you want to use this for current dates and times--for what time it is now and the day it is now.0079

This is the recommended usage for it.0094

What are the methods of the Time class? The most common one you will use is the now.0100

Now returns a time object initialized to the current system time.0106

To call this, all you do is use time.now.0112

Time.now will retrieve the current time from your system.0117

The next method we will look at is at.0124

What at does is, you pass in a value of time, and this creates a new Time object with the given time you pass in.0128

Time is the number of seconds from epoch, so if I do Time.at with 0, the time it's going to give me is 1969, December 31st, 4 o'clock, and here is your time zone.0137

If I pass in this huge number, say 1.2 billion seconds, it's going to get me 2008.0157

I can also do Time.at with...call Time.now, which will get me the current time, then to_i to convert it to number of seconds in integers, and then I can pass it, and it will get me the exact same object, essentially.0172

One question you might have is, "What is this number second from epoch?"0187

Let's take a look at that.0194

I'm loading up the epoch converter--epochconverter.com.0197

You will see that, if I reload it...actually, in real time, notice that it tells me the epoch time, and it's this huge, huge number--in the billions.0204

But, you can also convert this epoch to human-readable; you take this number, you can click this timestamp, and it will say...for my time zone right now, it is Monday, October 15th, from that timestamp.0217

Now, what is this number, essentially?--let's go down here--what is epoch time?0232

This is the UNIX epoch, or UNIX time, or POSIX time, or UNIX timestamp.0239

It's better known as the UNIX timestamp.0244

This is the number of seconds that has elapsed since January 1st, 1970, at midnight.0246

It's a very common timestamp that is used.0252

It doesn't count leap seconds.0257

Literally speaking, the epoch is UNIX time zero.0259

Epoch is often used as a synonym for UNIX time, and many UNIX systems store this as an assigned 32-bit integer, which might cause problems generating 1920 to 38.0263

There you have it; go down here; you can see it in Ruby: it's just called Time.now.0276

Convert to epoch: Time.now.to_i.0282

Let's put here...this is actually UNIX time...I like to call it the UNIX timestamp.0287

Let's look at some of the components of this Time class.0306

I call Time.now; that is going to give me the current time.0310

If I pass .year to this object, it will tell me the year I'm at; day will give me the day in numeric terms; month will give me the month in numeric terms; hour; min is minutes; sec is seconds.0315

I can also get the microseconds using this usec method.0334

When we call zone, it will get me the time zone of this Time object.0339

It says my system is in Pacific Zone time; it gets me Pacific Daylight Time.0346

Also, this Time object--you can convert it to an Array.0354

All you do is pass to_a; this converts the components to an Array.0360

There is quite a specific Array it gives back.0367

This Array: the first element will be seconds, then minute, hour, day, month, year, wday, yday, isdst, and the zone.0374

If you do t=Time.now, t.to_a, this will get you back that Array--the time Array of elements here.0387

You could also use a method to create a new time.0406

For this one, we are looking at Time.local.0412

This creates a new time in your local zone.0415

How does it get that local zone? It gets it from your operating system.0419

To do this, it does take parameters; you can pass in a year, a month, day, hour, minute, and second.0425

The year is required; it needs at least one argument; the other arguments are optional, and it will just pass in a default of 0 for that.0432

This will create a new time; year is required; other arguments are optional.0443

If I call Time.local, I'm going to get my first value, 2010, as my year.0450

That is what I'm passing in; that is my required value.0456

Notice that I actually pass in the month, but as a three-letter month; I can pass in a numeric month or a letter; so I'm passing in Jun, and then a day and hour.0459

I call Time.local, pass these parameters in, and it's going to create me a Time object, and it's going to use those parameters I passed: 2010 and June 15th, 12 o'clock.0473

You can also create a new time in the UTC time zone, or GMT--those are both the same.0490

This, again, takes the exact same parameters: year, month, day, hour, minute, second.0496

It creates a new time interpreted as UTC.0504

You can also use GM for GMT; for that, just do Time.gm, and go ahead and pass the time values in there.0507

For my example here, notice I used Time.utc; I passed in the year 2000; here is the month, which is January; day is the 1st; and hour is the first hour.0524

Here is our year...so it creates a new Time object, January 1st, 2000, at 1 o'clock.0540

Remember, the hour is from 0 to 23, so 0 will get me midnight; 23 will get me 11 p.m.0548

There are some specificities to this local and UTC--to the parameters you pass in.0568

As I said, year must be specified; month may be specified by numbers from 1 to 12, or the three-letter English month name.0576

That is just the lowercase, like jan or jun...or December, dec...0589

The last one--I just told you about this--hours are specified on a 24-hour clock, which is 0 to 23.0604

There are quite a bit of methods in this Time class.0615

If I create a new object, t=Time.new, I can start calling these.0620

One of these--I would call utc?; this will tell me true or false--is this part of the UTC time zone?0625

This will pass either a true or false value.0635

Then, if I call zone, it returns me the time zone of this Time object.0638

I can also call getutc, and it will convert this time to a UTC time and tell me what time that is.0646

I can also call getlocal, and it will convert the time to the local zone time.0652

If I were to call Time.now, and getlocal, it would get me the same time, because it is the same zone it's working in anyway.0657

I can also check what day it is; I can say, "Is it a Sunday?" by just calling sunday?; "Is this time Monday?"--monday?, and Tuesday, tuesday?.0665

One really popular method for time manipulation is this strftime method.0682

You might have seen it in other languages, too.0690

I think that is one of the reasons you see it here--because it is so popularly used in C languages and other languages.0693

This formats the time according to the directives in the string, and these directives begin with a percentage character sign.0699

There are quite a bit of directives; let's go through an example.0708

If I go ahead and do t=Time.now, and I'm just going to pass in the strftime, and I'm saying, "OK, I have this Time object; I have a certain way I want to output it."0712

I want to do year, month, day, and then show the hour, minute, seconds to it.0723

So, for my directive, I have my %Y for year, my %m for month, %d for day, and then I have my hour, my minute, and seconds.0728

With that output...it gets me my output for that Time object.0751

There are quite a bit of directives; let's look at them now.0759

Here we are at the RDoc for the Time class; we are looking at strftime.0766

Notice here, it says, "Formats time according to the directives in the given format string"--uses a percentage character...0775

After this percentage character, you do have quite a bit of modifiers here.0781

You have the flags, width, modifier, and conversion.0791

With the flags, it says you can pass the minus sign for "don't pad a numerical output."0798

Underscore: "use spaces for padding"; zero for "zeros for padding"; carat for "upcase the result string"; percentage: "change case"; colon for the %z.0806

What we want to look at is the format directives here.0820

We already looked at %Y; this is year with century; we can also do %c, which rounds it down, and %y, which will take them out of 100--so it just shows you the two numbers.0824

So, for 2012, it would just show 12.0841

Then, you have different directives with the lowercase m, which will be the month of the year, zero-padded with a zero.0844

You can also pass this underscore, which won't have it zero-padded, and -m will have no padding at all.0853

Then you can do capital B for the full month name; lowercase b will just be the three letters, and %h will also be an equivalent.0863

Then you have days of the month--you can do it non-padded, blank-padded (it would be d or e)--and then you can do the day of the year with the %j.0878

There are also some directives for the time.0890

Again, they have zero-padded and blank-padded: capital H would be zero-padded; lowercase k is blank.0894

Capital I will be zero-padded for the twelve-hour clock, and %l will be the twelve-hour clock that is blank-padded.0902

You can do the meridian indicators--pm/am, minutes of the hour, seconds of the minute (capital M, capital S)...0911

You can also get the milliseconds and the fractional seconds in digits.0919

There are also directives to get the weekday; you can get the full weekday name, the abbreviated name, and the numeric output--the day of the week.0928

There are also some things with the ISO 8601, so you can get some directives through that.0940

You can get the number of seconds from Epoch, which is the UNIX timestamp.0950

They also have combinations of it--a lot of common combinations; if I do %c, it will get my date and time; %D will get my date with month/day/year.0956

I can also run these combinations of 12-hour time, 24-hour time...and there are combinations that have already built that in there, since most commonly, you will use hour, minute, and second.0968

It's nice to have these combinations already built in there for you.0979

You will notice that they gave you a lot of common ones you might see, that you might want to use.0987

They have quite a bit of examples there, too.0994

That is the strftime method.0997

Next, we're going to look at the Date library.1005

To use it, you just require date, and this loads up two classes: one is Date, and one is DateTime.1008

These dates in Ruby are handled using the Date class.1019

First, let's look at initializing a new date.1026

To create a Date object, it is denoted with the given calendar date; so, by default, it does have some values if you don't pass anything; I can just do Date.new; it will get me a year that is -4712, a month of 1, and a day of 1.1030

So...you probably want to add in your own values.1047

I have an example here: all you do is call Date.new, and I'm passing in 2011, month is 10, day is 10; it's going to create a new Date object.1052

This is going to be a Date object.1064

That Date object is going to have defined October 10, 2011.1069

I can also pass in a Date object with just a year, and that will give me a Date object of 2012, January 1st.1075

Notice that fourth argument with new: it says "calendar"; I can actually use a specific calendar type.1084

Two popular ones are...I can specify one with the Date::Gregorian, or I can pass it with Date::Julian, and it will make sure that date follows that calendar setup.1092

Also, with the Date object, we can parse dates, and it's pretty smart on how it does it; it allows me to try different combinations.1108

I call Date.parse, pass in the string...this will parse the given representation of date and time, and it returns a Date object.1119

So, I call Date.parse('2001-01-01'); it will create my new Date object using that.1131

I can do the same without dashes; it will recognize it and be able to parse it.1138

I can also do month, day, year, or day, month, year, and both of these will be equivalent; so these are still equal.1142

Let's look at today's date.1158

They do give you a method that is called today--just Date.today--and that will create a Date object denoting the present date.1161

Just call t=Date.today, and then I can call the to_s, the string method conversion, and I get the string method of the current date.1170

So, if I call t.to_s, it will say, "OK, today is October 15, 2012."1182

Plus, there are some methods in the date that are components of it that you can use--just like the Time one.1187

If I do--say--today's date is, I can actually interpolate out some of these methods; so I can call month, day, and year, and that will actually get me the output--10 over 15, 2012.1194

There is also a method called next: this 'n' is lowercase, so it's next; and that creates a new Date object denoting the following day.1218

What I do is, I take my current object, call .next to that, and it will give me a new object.1230

If I do t=Date.today, create a new object called tomorrow equals my object t.next, and it will create a new object, which is the following day.1236

Also, with this and any other Date object, I can actually use the plus method and add on days, and it will create a new Date object to it.1251

It will automatically do all the typecasting and updating for me.1260

If I do tomorrow, let's say tomorrow is the 16th--and I add +7, it will create a new Date object for October 23rd, 2012.1265

Now, let's look at some of the countdown methods.1282

You don't see these in the Time method at all, but...1285

There is one method called downto, and I can count down, from the starting to the end; and it takes one parameter, which is the last day of the countdown.1289

Then, I can actually say, for each day it's counting down, to do some type of process.1303

This is a countdown of days, running block for each day--block being my code block.1309

For this, I do t=Date.today, get the current date, and then I say, "I want to go down two days," so I just pass t minus 2.1316

And then I tell it that I just want to output the day each time it does this.1327

For the first one, it's going to say today's date, October 15th.1331

Then, it's going to October 14th, October 13th, and this is the last day, so it's going to stop here, and that is the end of my countdown.1336

I can do this for each time; so I can do really complex stuff here; I don't have to just output the date.1343

I can output whatever I want in this code block.1349

There is also a method to count up: I can start at a certain date and count my way up.1354

Just like the downto, this one is called upto, but instead, it has a count up date, and again, I can pass in my code block for each date it's at.1360

For example, I just have Date.today, t; I say, "I want to go up to two days," so I take my current day and add 2 to that.1371

It's going to output October 15th for the first one; then it's going to go up to 16th; and then it's going to hit that countup date, that max: October 17th.1381

That is when it's going to stop counting up.1391

Let's look at some of the components of Date: there are not as many as Time.1397

It has year, month, and day: so year 2012, month 10, day 15.1402

It also has those common day methods: is it Sunday? Is it Thursday?1410

You just write sunday?, thursday?--it will tell you true or false--is it Sunday? is it Thursday?1415

Now, let's look at DateTime; this one allows us to pass in the time--the hour, minute, second--to it, also.1427

To do that, I take my Date object and call to_datetime.1436

This returns a DateTime object.1441

If I pass it from my current date today, since there isn't any time initially in it, it's going to create the new DateTime object, but the time elements will all just be 0 for now.1445

How do I initialize my own DateTime?--I want to specify the time to it.1462

You can just use the DateTime.new...this is actually date, time...DateTime.new.1467

You can pass your month, day, hour, minute, second, and the offset.1481

This will create a DateTime object denoting the given calendar date.1486

For example, I have here year 2001, month February, day--the 3rd, hours--4, minutes--5, and seconds is 6.1492

It creates this new DateTime object; February 3rd, 2001, at 4:05 and 6 seconds.1505

It has a 0 offset, since I didn't set one.1517

You can also convert this to a Time: take your object, and call to_time to that, and this will return a Time object denoting the datetime.1524

To do this, I just do d=DateTime.new--I could use the today method, too--and just do d.to_time, and that will convert your object to Time.1540

Other than that, that is the end of dates and times in Ruby.1553

I hope this is useful, and see you next time on Educator.com!1557

Hi! Welcome back to Educator.com.0000

Today's lesson is on Methods, Part 1.0002

What is a method? It provides a way to gather statements and expressions into one area.0009

It can be used repeatedly to execute a specific task.0020

Methods can be defined to do all sorts of tasks.0026

You will definitely be using methods quite often; you have seen a bunch of methods already in a lot of previous lessons we've done.0035

The goal of this lesson is to get you very, very understanding of all the basics of a method so, going forward, it clears a lot of questions you will have.0043

Let's first look at a very basic method that you will commonly see.0055

Parts of a basic method: you will see parameters--notice, for this one, it only has one parameter; in this case, there is just one parameter.0062

You will also notice, for this method, it takes one parameter, but it also has a default value; that default value for that parameter is 0.0079

Quite often, in other languages, they have to overload the function to do this type of behavior, but only Ruby has this default parameter set up.0096

There is a return value, and depending on what is in the parameter, it will return different things.0113

That is another thing you have--a return value.0129

Notice that this method doesn't have the word 'return' that you will often see; since this is the last statement, these will be returned, though.0136

If I pass hello_world without any parameters, it will default n to 0, and that means it will display "Hi!".0153

If n is greater than 1, it will print out "Hello worlds!"; if it's equal to 1, it will print out "Hello world!"; and else, it will do "Hi!".0170

You will see here, as I called the method, hello_world, there are 0 parameters, and it just prints out "Hi!".0181

You will notice that this is the return value, and that is a string.0195

Again, I could also pass hello_world, and if I pass in 2, it would give me "Hello worlds!".0204

If I could also--notice, I didn't put parentheses there--but if I also did hello_world, and I passed in 1, it would return to me "Hello world!".0227

You will also notice that the actual name uses an underscore; quite often in Ruby, you will see underscores for all the methods: you will not see CamelCase.0252

This is just normal convention in Ruby.0267

OK, let's go to the next one.0273

We just saw three different return values; let's look further into this return value.0276

You can actually explicitly write return; when you do that, this return keyword forces a return to the end of the method.0285

If you put anything below it, it's not going to get to it--it's just going to return after it sees this keyword--so that is a very important keyword.0297

Also, any value that is following that return value will also be returned.0307

If I pass in a String after that return, an Integer, whatever object I pass--it's going to be returned with it.0313

Return and whatever object this is...and this object can be anything.0323

It can be a String; it can be an Integer, a Float, an object you created...0330

Most often, you won't see any return value in the code.0346

Usually, it's omitted when it's not required, as you saw in that basic method--it didn't have any return values.0354

It's already the last statement at the end; it's already going to be returned, anyway; so there is no reason to explicitly tell it.0361

Let's go through some examples where you will see it.0370

Right here, I have a factorial example; this will give you the factorial, depending on the parameter put in.0375

See here, our method factorial takes one argument; it returns 1 if you pass y=0.0384

Notice that it just returns: that means that it ignores this next line; so if y=0, it's going to ignore this line over here, but if it is 1 or greater, then it's going to call this method here.0395

This method actually calls itself; so, if I did 3, it would do 3 times factorial...and 3-1, we have a 2, and after that, it calls factorial of 2...that will get you 2...times factorial 1.0413

Here is where we get that 1; this is going to break down, 1 times factorial 0, and notice, when it hits 0, it sees that over here: it says, "OK, that y value is equal to 0--I'm just going to return 1."--so that is 1.0453

Now that we have broken down what all the different factorials are, here is what it gets when I pass factorial 3--remember, this is for factorial 3: when I do that, it's going to do 3 times 2 times 1 times 1, and we get, at the end, 6.0476

Let's go through some code of this example; I want to show you what you can do without the return value, too.0511

We have our terminal here; let's first specify the method.0525

Again, I'm putting one parameter there: I'm going to return 1 if that y=0, but instead of having that second return value--it's not needed, since it's the last line of this method--I'm not going to do it.0531

I'm just going to put return, y times factorial, y minus 1, and then I'm just going to end it.0548

Let's see it in action: if I do factorial 0, I get 1; factorial 1, I get 1 also; 2--that is 2 times 1--I get 2; 3--which is 3 times 2 times 1--6; 4--we get 24; and 5--120.0562

Notice, we didn't even have to use that return value.0593

That is our factorial example.0601

Now, Ruby allows you to return two values; you must explicitly set that return value in this case.0604

If you are returning two values, you have to explicitly set the return.0617

That return value is collected, and it's returned as an Array.0624

We have an example here: I have this method called square_it; it takes two arguments, and those two arguments you pass in--it's going to take them to the power of 2 and just return it.0631

Notice, on my return value, I actually didn't put any parentheses, and it allows that.0646

I have the comma, and my 'to the power of 2' there...so if I call square_it and pass in my two values, my return is an Array of 4 and 25.0655

It's going to take 2 squared and 5 squared and put that into an Array.0671

The next thing I want to talk about is undefining methods.0682

You probably won't use this too often, but it's still useful to know, because there will come a time when you will need it, and it's good to know it.0688

To do this, the key action value is undef: you call that and the method you want to undefine, and it will make that disappear.0699

This takes one parameter, an identifier that specifies the method name.0716

It works in classes to undefine instance methods.0722

So, if you are in a class, and you are going to undefine these instance methods, you can also undefine the inherited methods.0727

For those previous examples, if I do undef(square_it), undef(factorial), it does not exist anymore; if I call those functions again, it's just going to give me an error.0735

I can actually go to the terminal...and we still have the factorial in memory, so if I do factorial(5), I get 120.0748

Now, if I call undef(factorial), and then I call it again, now it says "NameError: undefined local variable or method 'factorial' for main:Object" since I have created this method in the global scope.0757

The next thing we want to look at is method names.0781

Method names should begin with a lowercase letter, unlike classes that still use that CamelCasing.0787

When longer than a word, separate the words with an underscore: that is key.0802

Maybe in another language, you see this HelloWorld; it follows this CamelCase convention; but in Ruby, when you are creating methods, this is the way to go.0811

It's going to say hello_world; it spaces the two words out, and it uses underscores to do that.0830

The next thing I want to talk about is...you will see methods with an equals sign after them, a question mark, and an exclamation point.0843

We have our equals sign here, and our exclamation point, and our question mark.0852

First, let's just look at the equals sign.0860

Equals sign means the method is a setter and that it can be invoked using an assignment syntax.0863

Follow these rules: if you are creating a method with an equals sign, that means that method is a setter.0870

Now, let's look at methods with question marks.0883

When the method name has a question mark at the end, this means it answers the question posed by the method.0890

Usually, it returns a Boolean value, true or false; sometimes, instead of false, you will get nil.0900

For example, let's just look at the empty? method--it's 'empty?'--it has a question mark at the end.0911

You know that it is contained in a wide variety of objects: so, in Array, if an Array has no elements, it will return true.0920

It's also defined in the String object; so, if a String has 0 characters, it will return true.0934

And, of course, you can create your own empty? method for your own objects, and you can define them yourself with what you need it to do--what does empty mean in your object?0944

The next thing is to look at method names with exclamation points--an exclamation point at the end.0960

Usually, these methods are mutators; that is the common thing.0970

What is a mutator? A mutator alters the state of an object.0979

This exclamation point will actually change the existing object, in most cases.0987

You will also see other methods that don't use exclamation points, and they still change the object--like delete.0995

So, why the exclamation point--why do you put it in? The exclamation point means to use it with caution.1002

This is the significance of that exclamation point.1011

It's going to change the object; it might do something else, but an exclamation point means, "Be careful when you use this!"1019

For example, I have this Numbers--it has three elements in this array.1028

I'm calling this sort!--notice I have that exclamation point.1035

That will actually change this existing object here, and it's going to sort that.1040

This Numbers object now has been changed to be an Array of three elements, but sorted to be [1, 2, 3].1049

Remember, if I didn't have that exclamation point--I just called sort--it would create a new object; it wouldn't modify the existing object.1064

That one is just Numbers.sort.1071

Just remember, with the exclamation point, you have to be more cautious.1076

OK, let's look at method aliases next.1083

This defines a new name for an existing method.1089

With a method alias, you can give a method another name, and you can use both of them.1095

It's not uncommon to have more than one name, so it's not a problem, and Ruby makes it really easy to do this.1109

The key here is this keyword called alias.1117

What you do is...the first parameter you put in is the new method you want to give it.1124

What is the new name you want to give this method?1134

Then, you need to pass in the existing method's name.1139

It takes those three elements.1145

So, you have to pass alias, the new method name you want to give it, and the existing method name that it is right now.1149

Very simple example: we have this method called hi_world--it returns "Hi"--and I want to give it another method name, an alias.1159

I'm going to call alias and pass in the new method hello_world, and the existing method hi_world.1171

Now, when I call hello_world, it will return me that value "Hi"--just as if I called hi_world, it will return that value "Hi" to me.1180

Next, let's look at operator methods.1196

Operators can be defined in your own class.1200

The Array operators, the square bracket and the square bracket equal, can take any number of arguments.1206

These operators are quite different from other ones you will see.1221

If you look at unary operators, they take no arguments at all.1228

Binary operators are passed on argument, so they operate on self and the argument.1235

Examples of a binary operator are plus, minus, multiplication...1245

Example here: if I have the plus operator, I call define, put + and the other argument; and then I can go ahead and put my code here and end it.1252

That is very, very basic; let's get deeper with how this works, with this binary operator.1273

I'm going to create a class and have a couple of these binary methods in there.1283

Here is my class: let's call it SuperClass; I have a constructor that takes one String value...and now, I'm going to actually create my binary operator.1295

I do my addition; I say it's going to take a parameter called other_value; and this will actually update this value that is in this object.1311

It's going to append the other value to that string.1324

Then, I'm going to make another one with a minus sign.1333

This will do the exact opposite: whatever that other_value is, it's going to remove it from the string.1336

To do that in my code, I'm going to use a method called gsub; it's going to update the value in place.1342

I'm going to pass Regexp here, so it's going to first look for that other_value, look for any spaces around that value, remove any spaces around it...and then, it's going to return it as that value.1356

OK, it looks like I actually missed the multiplication sign...1381

Let's try that again...let's see if I can grab...no...OK, I'm going to define that again.1393

Again, I'm just creating my constructor, and I'm going to make my other_value.1411

Notice, I can also use interpolation here: since I'm not worrying about that right now--I'm just showing the method--I'm not going to do anything of that sort.1425

Here is my regular expression; it's going to remove other_value.1439

Make sure I got that right...1452

OK, so, I have my object, my SuperClass, done.1458

The first thing we're going to do is initialize it.1463

I've created my object; my constructor created a value called SuperString in this object.1471

Now, I'm going to show you, live, that binary operator in action.1478

If I take my object, and I do at...and I'm going to say it's SuperString, but it has 'goop' in it now...so SC plus that string value...it's going to use this operator here...and the return value is 'SuperString with goop', which is my new value in that object.1483

The same thing--I can subtract it out, so if I do SC minus 'goop', notice it removes that whole word.1510

So, now it's 'SuperString with'; that doesn't sound that good, so I want to remove that 'with', also.1519

Now, it says 'SuperString'; let's say 'with SuperOperators', since we have the new binary operators.1526

There you go: 'SuperString with SuperOperators'.1534

That is a case with all the different binary operators in it, and it works!1539

The next thing we're going to look at is methods and parentheses.1559

In most cases, parentheses are optional; it's more on your programming...what you like better: do you like parentheses or not?1566

Parentheses are optional in most cases.1580

If you have this puts method, you will notice that a lot of times I don't use parentheses with it, but with parentheses--they both work the same way.1588

They will both print out 'hello world' at the end.1597

You can also just print a string called 'hello world'.1603

I have this value 'Hi', and when I call the link method, which is part of this string, it will return me the number of characters.1611

But notice, I can return it with parentheses or none; both of them will still work.1620

There are cases where it is required, though, so let's go over those.1632

The main one is method invocations with more than one argument; this is a case where it is required.1637

For example, I have a method that takes two arguments; if I don't put these parentheses, it's going to show me a warning that says, "That's not proper; I don't know if it's being used for a method or something else."1647

So, you always want to add the parentheses in this case.1664

The last thing I want to go over is methods and blocks.1674

When you define a method, you can also pass a block in there.1681

You can use yield, and it will pass that value into the block you gave it.1687

Methods can be associated with blocks.1695

There is a value called block_given?; this returns true if a block is passed.1701

Using that, you can do different processes in this method.1711

Yield will call the block within the method.1715

So this block--now, using this yield command, you can actually tell it when to call it.1719

I have this method called processor: it takes one value, a number value.1725

Now, if a block is given, I'm going to say, "Process it using that block; I don't know what that is, but process it with it--you must know what you want to process it for if you have the block there; else, I'm going to process it myself."1735

I'm just going to multiply whatever that parameter is by itself; so it's num squared.1750

Let's go for a little example of how the block is used.1757

I'm going to create that processor that takes that parameter num; "If block is given, go ahead and process it using it; else, go ahead and just square it up."1764

If I just do processor(2), it returns me 4; I'm processing it now.1785

Now, let's go ahead and do it this time with 2 again, but this time let's pass in a value--let's say I want to do that argument and add 5 to it.1796

This returns 7 now; it uses the block I did, and we passed in 2, so it's 2+5.1814

I can do this with other values, too--I pass 10 in there, and then just do x times 2, and it says 20 instead of 10 times 10, which is 100.1822

I can change that to minus, and notice I return 8.1838

I can change the parameter, too, and it will return a different value.1846

That is the methods and blocks; that is actually the last thing I wanted to go over for this methods lesson, in Part 1.1857

Join me in the second lesson, where we go over deeper parts in the methods, and give you a better feel of all the different pieces to it.1867

Until then, see you next time at Educator.com!1880

Welcome back to Educator.com.0000

Today's lesson is on methods; this is the second part.0001

What I want to do for this second lesson is to go over a lot of common parameters that you will be using as part of the methods, and to give us a lot of hands-on, so you can see it in action.0011

For this one, we first want to go over the unary ampersand operator.0027

This is just this little & sign here, and it allows a block object to be treated as a Proc object.0035

So, when you have a parameter, and right before that parameter you put the & sign, it's going to treat whatever block object as a Proc object.0045

I like to call this block to proc.0056

In the previous lesson, you saw that we used this processor method; this processor method took one argument, and it passed a block.0062

This time, we're actually using that processor, but notice, it has two arguments: one is arg, and the next one is actually this unary & operator.0074

This is what is going to change that block to a Proc.0087

For this example, we call Process, and it calls this method call in it.0092

That method 'call' is going to take that block, and call it...plus pass this argument--this arg.0104

Let's do an example and see it in action.0115

Let me clear that terminal out; first, let me find it.0119

I'm going to take the exact same one from the slide, and then Process.call(arg)...0124

I have my method; it's done; and then, for this one, I'm just going to call the processor, pass in the value 10, and then I'm going to pass that block, x times 2.0132

Even in this example, my second parameter--I never actually pass it in; I put a block at the end.0152

It understands that that block is part of the parameter, and it's going to convert that to a Proc, so I can call it now.0165

At the same time, I can go ahead and change the block value, which is going to change the return result--change our parameter, and that is going to change the result, too.0179

Also, if I just do 10, notice I get a NoMethodError, because it's trying to call the method call, but it has no Proc object to call it in.0191

I could easily fix that by just putting an empty block, and it's going to have to return nil.0202

That is where I pass in my value, and you notice that whatever value I put in there, it's going to return that value back, but I'm not actually doing any processing for this example.0210

OK, so, at the same time, this unary & operator can go the other way, so you can treat a Proc object to be a block.0227

A Proc object can be treated as an ordinary block...so this is a Proc to a Block.0243

For this example, I create this Proc object using Proc.new, and this says, "Whatever this value is, I want you to square it; make that value x times x," so I call it square.0253

I'm going to use this with the Array object, using the map method.0272

I have this Array; it calls map; I pass in my Proc object with that unary & operator, and it takes these values and squares them, so I end up with 1, 4, and 9.0279

Let's go ahead and do an example; I can just do square.Proc.new(x^x), and then I can pass in an Array, take the map, and then pass in my value with that.0301

And it squares the values up.0325

The greatest thing is this dynamic: I can pass as many values as I want, and it will process all of them.0328

This is an example of our object--Proc to a block.0339

Next, we want to go over default values; I actually showed you that in the first lesson, with the basic method.0350

This default value creates a parameter, and you follow it with an equals sign and a value: that is how you create it.0360

That will create your default value.0370

If I don't pass any parameter in there, it's going to use that default value.0373

This allows a method that can take an optional number of arguments.0378

All these ones with default values--I don't need to specify what it is; it will just use a default value.0383

The example: I have this adder method; it takes two parameters, but notice, that second one has my default value.0391

It has a default value of one.0403

If I just did adder, and I just passed 2, it would return to me the value 3, because b=1 is the default value.0411

Let's go ahead and show you this in some code.0427

I'm just going to pass that same one in; it takes two parameters and adds the values up.0432

I just pass 1; it's going to do 1+1; but also, if I do adder(1,5), it's not going to use that default value.0443

Since I passed in 5 for b, it says, "OK, this has prior precedence," so it's going to do 1+5.0455

I can also just do 2--it would be 3; adder(2,2) does 4; (10,20)--it's going to add them up and be 30.0464

That is our default value.0478

Next, let's look at methods with variable-length arguments.0486

This is a very interesting parameter that allows us to put a lot of arguments in the method without specifying all of them and saying what they need to do.0494

This allows parameters that allow an arbitrary number of arguments.0508

How do I create it? You add this star before the method's parameter.0516

The arguments will be passed as an Array into the code.0524

All these arguments that you pass--when it sees that star, it's going to get that variable, and it's going to be returned as an Array.0533

Make sure to process it as an Array in that case.0541

What I do here is, I have this method can min_value; my parameter is that variable-length argument.0545

This is a variable-length argument...and it's called Numbers; it takes a length of numbers.0560

This method gets the lowest value, and it returns that.0578

Let's go ahead and just go through the code really quickly.0584

The first one does a Numbers.shift, so--this Numbers is an Array; it's going to take the first element in this Array and say, "This is the minimum value."0589

Then, it's going to go through each of the other values here, index 1+...to the end; if it finds a value that is lower than the minimum, then it's going to make that the minimum value.0601

It's going to continue on this loop until it goes through all of them, and then, now, we have found the minimum value.0620

This iterates through the Array and gets the minimum value.0628

Now, it goes through all this; min actually points to the minimum; and then, it returns that value here.0656

I want to show you this in action, so let's go ahead and do min_value; it takes a parameter of Numbers; go ahead and do that Numbers--it gets the first element in that Array.0667

Then, we're going to iterate through that Array of numbers, getting the lowest one.0685

To do that, I'm going to get min=n if whatever element that is less than what is the minimum.0693

Then, at the end, I want to return min; so that is my least value there.0701

This takes a variable length of arguments, and I don't have to worry about it as I pass; I just put a comma list in.0706

I'm going to call min_value, and I can call [1,2,3,4,5,6,7], and when I press Return--you know what that code is doing--it says 1 is the lowest.0716

I can also go through a different list, where the last value is maybe a negative sign, then put some other values in there, press Return...it says the minimum value is -5, so I know it's getting the last value and going through all of them.0730

And, it works with negative numbers, too.0751

I can also use parentheses, remember...it still works; it gets the minimum values from that.0758

That is our variable-length arguments.0771

The next one is...you will commonly say, "While I'm programming, I have so many parameters I'm passing--it's going to get disorganized; it's going to get confusing; I don't want to pass it in a certain order--I want more flexibility when I code."0778

That is the reason we should use Hashes--multiple arguments that get disorganized, that get difficult to remember in the proper order.0795

I just want to work the code and get it processed.0807

Solution: pass a Hash as a parameter.0809

Depending on what you're doing, you will want to have this flexibility.0815

You might want to force it to have a certain amount of order in the primers, but in this case, we're being flexible; we're just saying that we're going to use that Hash; we're going to process it as a Hash.0821

Here in my example, I have this method called multiplier; it takes Args, which is a Hash.0833

In my code, I actually didn't default it to a Hash, but I can make a default value a Hash here, too.0848

It's going to take four keys in the Hash and multiply them together; that is why it's called multiplier; it multiplies four elements.0857

Look at my code; notice, it actually looks for a symbol called key 1.0868

If that key doesn't exist in the Hash, it's going to default it to value 1.0874

The same for key 2; if it doesn't exist, it's going to default it to value 2; the same as 3 and 4.0879

At the end result, it's going to multiply them together and return that value.0888

Let's make this example in our code.0895

Let's go ahead and add the parentheses this time, since we haven't been using that.0901

I'm going to go ahead and put in all the key values; notice that these are not String keys; these are Symbols.0910

I'll make it easier, just substituting the number...so, I have all my values here, and then I just want to say, "Just multiply them all together."0924

It's going to get those four values, multiply them together, and return it.0943

Now, when I call multiplier, I just have an empty Hash; it says wrong number of arguments (0 for 1); let's see, if I put those parentheses, if that fixes it...it does!0949

With a Hash, make sure you have parentheses.0963

Notice, I pass an empty Hash in there; all that is going to do is say, "You passed in a Hash; you didn't give me the indication to define them in there; I'm just going to use the default I've already defined in the code: 1 times 2 times 3 times 4, and that's going to be 24."0968

I can also define a couple of those values; so that key, 1, I make that 2, and that key 2--I'm going to make that 3.0986

This is going to be 2 times 3 times 3 times 4, and I get 72.1003

I actually can define them all; I'm going to define them all as 1, so my value should be 1, too.1010

My value is 1: 1 times 1 times 1 times 1.1025

Notice, I'm passing a bunch of different values--some of them I don't even define, but it's allowing me to do it, because I'm using this Hash, and in my code, I tell it these default values that I can specify.1029

For this one, it's 5 times 2 times 3 times 4; it's 120.1051

Remember, I can still pass in other parameters to this.1062

I have this Hash, but I can also pass in other parameters after this, and I can still process it, also.1065

Depending on what you're doing...I just want you to get the concept down.1072

That is using Hashes with arguments.1080

Let's quickly go over the RDoc for the method.1083

It's very simple; it has method, and it has Proc, because they are so closely integrated.1093

Proc to block, block to Proc...we talked about that...1103

Notice here, it does have some methods to check for equality: "Two method objects are equal if they are bound to the same object and refer to the same method definition."1107

You already saw the call method in action--"Invokes the meth with specified arguments, returning the method's return value."1116

There is a method called arity--"Returns an indication of the number of arguments accepted by a method."1128

You already saw the call method...1135

You can also use inspect or to_s to return "the name of the underlying method."1140

This is interesting; you can also call parameters, and it "returns the parameter information" of that method.1148

Receiver "returns the bound receiver of the method object."1156

We already talked in a previous lesson about unbind--"Dissociates meth from its current receiver."1160

Let's see that in action--this parameters.1169

So, multiplier.parameters...wrong number of arguments...it's probably not being used in the right way...1176

Anyway, I will let you look deeper into that RDoc.1196

That is the end of the methods lesson, part 2 of 2.1202

See you again at Educator.com!1208

Welcome back to Educator.com.0000

Today's lesson is on classes; this is Part 1.0002

First, what is a class? A class defines a set of methods that an object responds to.0008

It represents a container that holds properties, such as variables and constants.0018

We do this to encapsulate a lot of different variables, objects, methods...and that is why we say it represents a container.0025

It can be reused; you can reuse it through inheritance, and you can extend it or subclass other classes.0038

Every object is an instance of a class.0048

First, we want to create our first class.0058

To do this, we use the class keyword; that will create a new class in Ruby.0063

Also, this creates a constant, too; this creates a new constant that refers to that class.0077

Whatever name you give after class is the constant name and the name of the class.0082

The name must begin with a capital letter; that is very important.0089

If you look at this class here--we have HelloWorld--notice that the first letter is capitalized.0093

After that, it doesn't matter what the next letters after it are: they could be lowercase or uppercase.0100

To end that class--we have our class here, HelloWorld--to end it, we just put the end at the end; that is the delimiter.0107

You can also use other things for HelloWorld; notice, we have our capital H here, but we also capitalize the subsequent word.0117

For our second word, we also have it capitalized, but we also have it lowercased, and you can also go wild and put the whole thing as uppercase.0131

Now, the convention is to, every time you have a new word, capitalize that letter, so it uses that to separate everything.0142

That is why you see, for our HelloWorld here, we have the W uppercased, as well.0153

Let's look at instantiating our first class.0161

Here I have HelloWorld.new, and it's creating its new object, and it's going to create that reference into this variable, just called hw.0167

That new will create a new instance of our class.0178

Of course, it's always like this: n-e-w, all lowercase.0183

That will actually create our class into memory.0190

After this, you can do a couple of things: I can call the class method, and it will tell me the class it's part of.0196

If I take my variable hw, and I call a .class on it, it will return to me, "This is part of the HelloWorld class."0204

I can also ask it, "Is it...?" one of these classes; so, using the the is_a? method, I can say, "Is it a class called HelloWorld?"0214

If I do that here, it's going to return true, since that is what we instantiated it as.0226

Next, let's look at initializing values.0237

To do this...instead of using function, which you often see in other languages, they often use what is called define.0242

They use def--all just in lowercase, d-e-f, and this defines an instance method for the class.0252

Like I said, in other languages you might see this as similar to the function, and then you would have your name of it here.0263

The initialize method is invoked automatically when new is called.0275

So, as you saw in that previous class, we called new to it, but you don't actually define any method in your class called new; you actually define it as initialize.0280

Speaking of that in your class, when you are creating it, it would just be def, and then initialize.0293

That creates this method that creates your object.0307

This is an instance method; it is a method that is invoked on an instance of the class.0314

So, whatever subsequent methods you make from this, they will be instance methods that are part of this class.0322

Now, this Initialize class is kind of interesting, because the new is actually a class method, but initialize is an instance method in this case, because you are defining it in this structure.0328

Let's look at an example here.0341

I have this class called HelloWorld; in it, we have our def, which says, "This defines an instance method of the class," so we have our instance method initialize.0344

It takes one parameter; it's taking a name.0359

What you see here is, it says we have this new variable that says @name, which is the instance variable, and this is getting the same value as that parameter.0363

So, as I do here, I created my class here; I instantiated the class.0378

We also initialized the name value.0393

And see here, I have HelloWorld.new, and I pass in the parameter Justin, which is the name value.0409

As I said before, we are initializing these values; what is happening here--first, we have the initialize method; it is automatically made private; if I were to do HelloWorld.new, and then I call the .initialize method, it wouldn't allow me to access it, because it's private.0423

The initialize method is accessible inside the scope of class, and you can call it as initialize, but otherwise--if I call it outside that scope--it's not going to allow me to do it.0445

It's automatically made private; the parameter is stored in local variable name.0456

Here we have this parameter name; that is the local variable.0463

Then, the name is assigned to instance variable @name.0468

We have our instance variable here...this is our instance variable, and this is our local variable.0472

If you look at it, you can tell--why is it an instance variable?--It has this @ sign here.0490

Next, let's look at defining the to_s method.0503

This allows us to have a string representation of that class.0509

I'm going to continue working on that hello_world method.0514

Here, we are going to...we have defined and initialized name, and we're defaulting it, now, to an empty string.0521

It says that, if I don't enter any value, it's going to enter an empty string of "", and it's going to allow me to continue that class, anyway.0536

So, I can call it without passing a parameter in.0551

Then, it goes ahead and declares and instantiates that instance variable from there.0554

Then, I'm going to declare that to_s method for that string representation.0560

It's going to create a new local variable called name inside this scope in that function.0565

That local variable only works in that method.0571

This says, "OK, I'm using this ternary operator, so I'm saying if this instance variable, name, if that is empty, I want you to pass world in there."0575

"But, if it isn't empty, go ahead and just pass name."0588

Here it says "Hello", and it interpolates the value name, so we have two values: this could be either "Hello world!", or it's going to say, "Hello", and then it's going to have whatever this @name value is.0592

This one is if it's empty, and this one is if it's not empty.0618

For example, here I just call HelloWorld.new; since it is empty, it's going to just print out "Hello world!"0633

Let's go ahead and set up some code for this, so you can see it in action.0643

I'm running my Ruby Interpreter.0652

I'm just going to create that class, so I have my HelloWorld; I'm going to initialize it with that default parameter, so it's going to do that empty string; @name=Name...0654

Then, I'm going to declare my to_s method here, so we have if @name is empty...if it is empty, it's going to pass world; else, it's going to pass name.0671

Then, this is what I'm returning; I'm returning that local variable name.0684

There you go: I have our class initialized.0693

Now, if I do HelloWorld.new, you will notice that it prints out "Hello world!"0695

But watch this: if I do HelloWorld.new, and I pass Justin in, you will notice it changes that value to "Hello Justin!"0703

I can pass that to a value, too, and it will still create that new object, hw, but for the to_s, you will notice it still prints out "Hello [world/Justin]!"0711

Plus, I can call hw.to_s, and it will print out that string again.0724

Next, we're going to look at this value called self that is used in the class.0737

Self just means that instance, that object of that class--the instance of the class.0744

When we do this, @name belongs to the object that self refers to, so I can do self.name.0751

For example, in this value, we actually updated this class now; it's going to initialize to a first name and last name.0760

I'll show you how this is done: first, let's just go over the code very quickly.0771

Now, we have first_name and last_name; they both have default values, so if I don't pass it in, it's going to create an empty string for that.0778

It has two instance variables now: @first_name and @last_name.0786

We have a new method called name; when I call this method, name, it's going to give me the full name--first name and last name--very simple.0794

Our to_s method has been updated; notice, now I call self.name.0801

What this does is--notice, when I call self.name, it's just going to call this method up here.0809

It's going to say "Hello" with the first name and last name with it.0817

Let's go ahead and show an example of how this works.0825

Let's just declare that method again.0832

This time, we updated it, though.0836

The first_name, the last_name...and I'm declaring the instance variables now.0843

Here is where I'm going to use a little interpolation; I'm going to say, "Interpolate the first value and then interpolate the last_name value."0859

Then, the last one is--I want to do that to_s method, but this time, I'm using that self keyword.0877

Here you see, that self.name is being used.0887

Let me show you it in action: so I do HelloWorld.new of John Doe; when I press Return, I have the new with the first_name, last_name, and it actually calls that to_s method by default when I declare it.0893

It says, "Hello John Doe!"; I can also do hw.name, and it's going to do the same; it's going to print out the name; and I can also just do hw.to_s, which will call name also, but uses that self method that we created, which is an instance of that object.0913

It gives you all of these properties that you can use while inside that class object, for your own functionality.0932

Next, let's look at accessor methods.0948

The accessor method returns the value of that variable.0953

For example, we're looking, first, at this HelloWorld; we have two accessor methods here.0960

All they do is to tell you what is that value in that instance variable.0974

These are also known as getter methods.0981

The first one will get me that @first_name value; the next one will get me that @last_name value.0995

I can actually show you how these accessor methods work; I have some code I want to show you here.1009

Here we have that same code piece again, but you notice I have two accessor methods here.1020

I'm going to declare that HelloWorld object, HelloWorld.new, and I'm going to call these two methods, hw.first_name, hw.last_name, and then we can see these methods in action--see how they return that value.1032

I'm going to execute that code I just showed you, that HelloWorld; the first one--John is the first name, Doe is the last name, and it's using these accessor methods to get the value back.1055

The next thing we want to look at is setter methods; setter methods, not getter--we just went over getter methods.1077

These are also known as mutator methods.1082

They set the field to a value specified by the argument, so whatever parameter you pass in that method, that is going to be the one that it's setting, and it's going to set it to the instance variable.1088

I updated that HelloWorld class again; so this time in that HelloWorld class, here are my accessor methods, or getter methods.1102

Notice that I have made them one-liners now; I'm using semicolons to shorten it from three lines to one.1116

The reason I did that was just to show you different flexibility you can do with the Ruby code here.1124

Here, we have our setter methods.1132

Here, we have our first_name, our parameter we pass in; that parameter itself is the one that we are setting that value to.1145

Setting that value, it will go to that first_name, that instance variable.1153

Then, we have this method called last_name; it also takes one parameter, and that value will be passed into the instance variable last_name.1158

These are the setter methods; they're very simple, but they do have a convention, and this is the basic convention they follow to set that value.1166

I do have this code in action so you can see it here.1179

In this code, I have my getter methods--my accessors--and then here are the setter methods, just like we said.1185

I want to show you this code in action, so we have the whole class set up here, and I'm going to initialize the HelloWorld object here.1197

First, I'm going to call those getter methods.1208

Then, I'm going to use the setter methods to change the first_name and last_name.1211

Here it says "Jane Smith," and then, when I do puts hw, it's going to call the to_s method, and the first_name and last_name should be updated--let's see what happens.1216

We already called HelloWorld 1, and I'm going to call HelloWorld 2.1235

There you go: so, we have our two methods here; it gets the first values we set with the object; then we updated those values using setters, and it says, "Hello Jane Smith!" now.1241

Now, Ruby allows us to automate the getter and setter methods; we just hand-defined them; we got the basic convention down--what these methods are and what they do--but Ruby has these built in because they're so commonly used.1265

They provide these methods to automate them.1280

These getter and setter methods are actually defined in the Module class.1285

You can invoke these in any class definition, and they are already part of these classes, too, so you already have this library already built in the class.1293

Let's look at a few of these methods.1304

The first one is attribute reader: it's attr_reader.1306

This creates a getter method for the instance variable with the same name.1313

In our previous slide, we had first_name--the method is called first_name--it's going to do the exact same thing here.1320

It's going to have that instance variable with that same name.1329

They make it really, really easy to create these methods: you just pass in the symbol, whatever name you want to use.1334

So here, I have my class HelloWorld; I just call attr_reader, and I use this symbol for first_name and the symbol for last_name, and that is it.1340

It saves my time; it doesn't mess up my code with all these getter and setter methods; it just makes it very simple and easy to put that in your class and define it.1351

Now we have our getter method; do they have a common setter method? Yes, they do!1366

We have this attribute writer here; the attribute writer creates a setter method for the instance variable with the same name.1372

Again, all I do is do the same thing; I can just use attr_writer, and then I just put first_name, and then last_name, and that is it--the same way as the reader method.1381

But, the attribute writer isn't commonly used, because usually, you want to be able to access it and read it out if you're editing it.1416

The more commonly used one is just attribute--attr.1425

This is the real one you want to get used to; it creates both the getter and the setter methods.1429

In Ruby 1.8, you would use attribute accessor, but in 1.9, you don't have to call attribute accessor: they shrunk it down, and you just need to call attr.1439

This works with 1.9, and it's a common way to do it, and it's the popular way; I would recommend you use it.1450

So, how do you automate this getter and setter method? 1465

For our example, with our HelloWorld, all I would do is...I want both those methods, so I'm going to call attr and pass my first_name and my last_name.1469

Those are going to get me that symbol value, and it's going to define those two methods for each one of them.1480

What does it give me? Since we didn't make this code, it's kind of magical, because it's using that Module class...it's going to create me a getter method called first_name and last_name.1489

Think of it--if you had just created it yourself, you would have that defined here.1501

Then, the setter method is going to be first_name with that equals sign, so you can think of it like this, too...1506

Now, we have four methods that are magically in this class.1515

That attr already handled all of the magic for us; it already created it and defined it for us.1523

There are some things you need to know about using Ruby's accessor methods.1533

First, one good thing is that they run as fast as if you had hand-coded them yourself, so there is no time penalty for that.1541

The important thing is that these do map the same instance variable name--whatever symbol you give it, it's going to be the same name for that method and that variable.1555

So, if you needed something more complex, like a more complex accessor with a differently-named variable, you would need to define them yourself.1564

If I call method first_name, but I want to tell it to go to a different instance variable, then this wouldn't work in that case, because it has to all be preserved in that same name.1577

I would definitely say to check out Ruby's accessor methods and play around with the class.1593

Other than that, that is the end of the first part of the classes.1600

See you next time at Educator.com, and we will go on to the next class lesson after this.1606

Welcome back to educator.com. Today's lesson is on setting up your environment.0000

The goal of this lesson is to get your environment set up so you are using the same Ruby version as us, and you have everything in sync, so you have the same tools I'm using.0006

The first thing we are going to look at is ruby-lang.org.0025

This has the core Ruby code--source code--and we are going to be using 1.9.3 for all of our lessons.0032

If you are on a Mac, you might see 1.8.7 set up by default, but you should go through the steps in this lesson to get you set up with 1.9.3 so you don't see any issues as you are developing.0049

First, let's go to that website, ruby-lang.org, and check it out.0066

If you go here, at ruby-lang.org, you will notice that it has a lot of documentation information, about it.0071

It will give you a little gist of what Ruby is, and it will show you some code for a class.0085

What they do here, is they give you a lot of versions... from latest to oldest. 0096

So you will see that it says "1.9.3-p27 is released."0101

That's the latest one. And you can go here and see the change log, and see what bugs have been fixed. 0107

Go ahead and click the change log; you will see that they give you a whole list of things that have been fixed for this version.0119

You can go ahead and check it out if you want, but I think mostly you just want to get the most stable one, so I'll show you how to do that.0126

The main thing here...you see all this news...the main thing here is this "Download Ruby" button.0134

So, you click "Download Ruby," and it will tell you that the stable version is 1.9.3, and it will tell you three ways of installing Ruby.0144

Going through these ways: one is compiling it from source, another is using a third-party tool to install Ruby--and we are going to go through each of these-- and using a package management system.0156

One third-party uses RVM.0174

We're going to go over that. So, first, compiling Ruby by source:0179

You can just download this link here; that will give you the latest one.0187

It's tar and gzip. So, you know how to install source...0191

Go ahead and download and compile it.0195

But we are going to go through setting up RVM, so you might want to take a look at that before you just build from source. 0201

That is the next one: they say here, "Use a third-party tool."0208

This RVM is very popular. It's called Ruby Version Manager, and it makes it so installing Ruby is easy.0211

Plus, developing it is easy, because I can use multiple versions of Ruby as I'm developing.0222

It is available for Mac OS, Linux, or any UNIX operating system.0226

Windows does have a version, pik, that does a similar thing.0232

It's very easy to install: use this curl command...0237

And then there is RubyInstaller: that is for Windows, to install the Ruby version on Windows.0244

And then they have packages: Linux--you can use "apt-get" to install it, and also there is a package called pacman for Arch Linux system;0253

Mac OS X--you can do "brew install ruby"--0269

Brew is also another package--0274

Then if you are looking at Solaris or OpenIndiana, you can use this "pkg install runtime/ruby-18".0278

There are also other implementations of Ruby.0288

We are not going to get into those, but it's good to know that you have them there.0291

You have JRuby for a Java Virtual Machine, MacRuby to run Mac OS code, IronRuby for .NET.0298

So, that is ruby-lang.org for you.0310

Mostly, you build it from source; that's what you use for it; but we are going to go a different route.0314

Let's go ahead and click RubyInstaller, for you Windows folks; they make it really easy for you. 0321

I'm at rubyinstaller.org, and they have a 1.8.7 version and a 1.9.3. You are going to get the 1.9.3 version.0333

Click on the "Download" button--just choose this top one--0338

It's executable. You download it; you get an executor; you double-click; you install it.0343

It will get you all set up with Ruby. 0348

You also have a lot of information here about documentation for it.0353

You can go to this "help" button, and it has a whole wiki of information to help you set it up, as well, if you have issues.0359

Double-click, you're set up, ready to go.0368

And Mac OS X and Linux installation--we already saw it--0373

You go to ruby-lang; you can install it through source, install it through the package management system.0379

But we are going to do it differently. We are going to install it using a combination of Brew and RVM.0385

So, this is my recommended route; we are going to use Brew and RVM to do that.0392

I'm setting it up with a Debian Linux.0402

You can just use "sudo apt-get install ruby 1.9.3", and it will get your version set up for that.0408

And so, we are going to deep-dive right into doing it with Homebrew.0413

Now we have it for a Debian; we have it set up for Windows.0423

Now we are going to structure it for the Mac OS X. A lot of my lessons will be in the Mac OS platform,0428

So let's see if we can get you to mimic the same behavior on your setup right now.0436

The first thing that we want to do is install Homebrew on your Mac machine.0441

Just go to this link over here, and what we are first going to do is install Brew--0447

It's a one-line command, and then we want to run Brew Update, and then you want to install a library. 0456

Type "brew install libksba"0465

Actually, let me remind you that this library here--0468

You probably want to take notes on this, too, but--0474

This one is required because you are installing Ruby

This is required...for Ruby 1.9.30483

What Brew Update is going to do is update Brew and make sure it's on the latest system file.0495

So, you want to run that next, after you install Brew.0502

Let's go ahead and look at the browser to see that.0507

So I'm going to go up here, and I'm just going...let me zoom it in a little bit deeper...0513

But Homebrew is a package management system on OS X.0519

They make it really easy to install things.0524

If I want to install wget, I just use Brew install wget.0529

I don't have to worry about getting it from the source--it's going to handle that for me.0532

It installs these packages in their own directories, links them up for me, does a lot of the rough work.0535

You will notice there is a lot of information documentation to help you get along, but what I want you to do is just go way to the bottom of this page.0548

And look at the main "Install Ruby"; that is the main one we're talking about doing.0558

So, you have this command here...0563

Run that, and you will get Homebrew set up on your machine.0568

Because it does say Ruby on it, there are further instructions if you don't have Ruby already set up to do it.0574

Go to Homebrew wiki, and it will show you some other methods to install it.0584

Because, most likely, you are already on 1.8.7, but we're upgrading to

So what you will do is...because, when I run ruby -e, you will see...I'm already on

But what I did is first, some hints for just installing Brew, if I just run Brew, you can already see your setup. 0615

You have some examples of usage like this, when you run the brew command.0627

I'm not sure if there is a version. Let me run --brew...there is a version. So I'm running

But, when I install it, I copied that command from that page, I paste it here, and I just press Return. 0635

And it's going to fetch this Homebrew and it's going to execute it. And that is going to install it for you.0642

But since it's already set up, I'm not going to do that, but I'm just commenting about that.0651

That's it. Just run that command, press Return, and it will install it for you.0656

If I even ran it now, it's going to start doing some things, downloading it, but I'm going to cancel that, but that's it.0661

You will notice you have your "Install Homebrew": you are going to click here; it gives you some information; install it...0672

But really, that one-liner is all you need.0686

And remember, after you get that Homebrew installed, run Brew Update and Brew Install...this libksba library.0690

That is important so we can move you up to

OK, so we talked about Homebrew.0707

Next on our list, we need to install RVM.0712

And then we are going to look at OS X GCC installer,0717

and then installing Ruby

There is quite a little bit of things we have to do to get you up to that version.0726

Ruby Version Manager--that is what RVM is. That is the acronym.0732

This is a command-line tool that allows you to install, manage, and work with multiple Ruby environments.0738

Ruby environments/Ruby versions. It's a very nice tool.0746

It allows you to...let you be able to run Ruby 1.8 and Ruby 1.9 concurrently.0750

So, let's go ahead and look at the page. We're going to go to rvm.io.0755

I already have the browser set up. On the page, they have this...there you go. I lost my browser for a moment.0767

This is Ruby Version Manager. You get a nice splash page.0780

As you go down, you will notice "visit the installation documentation."0784

They have screencasts, so you can look at screencasts and see videos.0790

And, they have some things about Rails, too.0794

First, I want you to just look at the introduction.0799

They will give you a little spiel about what it does, and then what we want to do is just install it. That's the main thing.0804

They have a lot of great documentation here, but let's just get right into installing this.0812

Let's click...when you click the installation document, you will see "Installing RVM,"0818

and they give you a quickguide. Again, it's just like Brew; it's just a one-liner.0828

You have a curl command; you can just copy that, paste that in your terminal, and get you set up, on your way0832

Again, here is that command, right there. If you just run that in your terminal, press Return, let it install...it will take a little bit of time...0864

After that is set up, you have to update your .bash profile.0870

So in your relative directory--.bash profile-- you need to include this line--it's all one line.0875

And what this line will do is make sure that RVM is loaded when you open your terminal.0883

It loads RVM, which is very important for us.0896

If...I have my terminal here, all I have to do is run this command, press Return--it's going to install RVM.0903

Again, it's already installed on my machine, but if I did RVM --version,0912

You will notice it's RVM

So after you do that, let's go to my .bash profile.0928

I've already added that line, so if I go to relative path, and just do run-profile, and I press Return--this is where you would paste that command from that slide-- and all this does it to get RVM set up on your machine.0934

It runs source, so make sure it's loaded up on that terminal.0955

Then, you can go ahead and run RVM. You will see a lot of usage information there, and a lot of different things you can do with RVM here.0960

Now that we have RVM set up, we have Brew installed, and now we need to set up this OS X GCC installer.0980

This, again, is something we need for Ruby

So you need to go to this link, and you just download the file there. Double-click; install it to your Mac OS machine.0994

And it's going to be set up, installing.1004

I go to the link here...you want to choose the latest one...10.7-v2. 1009

But you can also see, that's for OS X versions 10.7 and up1015

If you are going to download to OS X 10.6, then choose the one right below that.1023

Download it to your machine, double-click, and it will install it from there.1030

That will install the OS X GCC installer.1036

So you are almost complete--we almost have it set up. We just need to get 193 going now.1043

And now it's very easy. All you have to do is run this Ruby...install...

And that should get it going--very easy. In your terminal, RVM install 1.9.3,1057

Return, let it load up. If you are on an older Mac OS version--if you are on Snow Leopard--1063

You might have to, if you have any issues, do RVM reinstall 1.9.3, with GCC Clang.1068

That should resolve any issues; there are some common bugs with Snow Leopard, so just run that and try it out. After that, just run ruby-re;1080

If you see that Ruby 1.9.3, you're set.1091

You can also run RVM-help; they have a lot of usage information to see commands you are going to do with RVM, because what they do in RVM is you run RVM, and then you can run options.1096

In these options, you can run different things.1112

The next thing we're going to do after this is create a Gemset.1118

That is one of the options there.1122

You can actually change the Ruby version, too.1127

OK. Now we are set up. We have RVM going.1131

The next thing I want to do is set up this Gemset.1136

What this does is to encapsulate all the different Gems I have-- all my different plugins-into one area.1140

This area--we're going to call it Educator. Let me show you that, too, now.1150

RVM, Gemset, Create, Educator.1155

You see, when I press Return, it says "Educator Gemset Created." 1161

And later on, I can just use "RVM, Use Gemset, Educator," and it will go back to it.1167

Wait...it looks like something...I think I have to use "RVM, Gemset, Educator,"1174

Yes, "RVM, Gemset, Use Educator."1190

But what this does is, if I do Gem List, 1195

There is a list of Gems here, and when I create a new Gemset, it will have a different list.1200

So, let's do "RVM, Gemset, Create, Educator 2," and I'll do "RVM, Gemset, Use Educator 2,"1207

You will notice that if I do Gem List, I will have a different set from if I do "RVM, Gemset, Use Educator 1."1218

They are both different containers with their own gems in them.1226

OK, run that, test it out--run RVM, the Ruby version, and you should have your Ruby set up, your RVM set up; you have your Ruby 1.9.3;1235

You have a Gemset created, as well; and all you do is set that as default.1245

When RVM...Use 1.9.3 at Educator,1252

So, what this does is it sets up your Gemset...1257

It sets up Ruby 1.9.3, and then it defaults it--makes this your default environment whenever you are developing.1268

The reason those are...it's --flak.1290

And there you go! You have RVM set up, now you have your environment set up, and now we can get to some cool things in Ruby--1294

Start doing some development...we have the latest Ruby version going...1300

You're getting happy, learning about Ruby, and you are getting really ingrained into it, so it's nice.1306

That is the end of this lesson, and I hope you enjoyed it and will go on with me into the next lesson.1314

There we will go on to deeper functionality in Ruby and start getting closer to doing some code.1323

Welcome back to Educator.com.0000

Today's lesson is on classes; this is the second part.0002

For this lesson, the first thing we want to go over is defining operators; we've done it with methods; it's good to see it in action with classes, since this is where you would most likely be using the operators.0008

You can define all of the exact same operators.0027

Arithmetic operators: addition, subtraction, multiplication, division, modulus/remainder; unary operators--you can define the plus operator, the minus operator, and the increment and the decrement operator...0034

When I'm defining these operators...let's go over how to do it.0056

I have this class called Basket; what it does is: first, takes out initialize--you can initialize it with an Array of items; it holds a basket of items.0065

I have this method called initialize; the first parameter is just items, equal, and it's an empty Array by default; if you pass things in it, then it will instantiate this variable @items with whatever you give it.0078

Then, I have these new operators; we have an addition operator and a subtraction one.0096

It takes another object--I'm saying it's going to take this other object, this other "Basket," and it's going to take the items in this existing Basket and the ones from this new Basket, and it's going to add them together.0107

So you see, it creates this Basket.new, and you have your @items here, and it adds those with your other Basket.@items, too.0125

This is the same with the subtraction: instead, it's going to look at the items in this Basket and remove any items it sees in the Basket B.0137

To do that, we call @items, and then it says, from that parameter, to subtract any items that exist in that other Basket.0150

Let's go ahead and go through an example with this.0162

I'm going to call my terminal, my IRB, and just create the class first; I'm calling this class Basket.0166

I'm using the attribute reader, the getter method we saw in the first lesson.0181

Then, I'm going to initialize the items class, and then the next one is, we're going to look at that addition operator.0190

Here, I do my @items; that is the items that are in my current class; and then, I'm going to take that other class and add those items in, too.0216

Then, the same thing with the subtraction: I'm going to do the exact same thing, but instead, it's going to remove the items from this class.0227

There you go; I've created my class.0240

Let's first just do a basic instantiation; do a Basket.new; you will notice that it defaults to an empty Array of items.0243

That is no good for us; we actually want to set some values.0253

So, I'm going to create my Array; I'm going to put some grapes, some bananas, and some apples.0257

OK, so we've created our Basket; we have three items in here; I'm going to create another Basket, and I'm just going to point that to C.0267

For this one, I'm going to put some grapes, some apples...let's put some pineapple in there--how about that?0277

We have two Baskets now; we have three items in each one.0284

Let's go ahead and use our operator methods we just created.0290

I'm just going to call B+C; I have [grapes, bananas, apples]; I have [grapes, apples, bananas]; and we're going to put them all together in one Basket. 0294

I call that, and now I have this new Basket that has six items in there.0305

Basically, using the code that we just created, we defined it, and you notice how easy it is: we just call B+C, and we've just created that method.0315

We can also use spaces; it works fine, too; it uses it normally, just like Ruby does for any of their Arrays...and String classes use that concatenate...you're doing the exact same thing now--you're defining that operator.0326

Same thing: I do B-C; now, I only get [bananas]; why is that?0340

For my first object, I have [grapes, bananas, apples]; in my second one, I also have grapes and apples in there, but I do have pineapple, but it doesn't need to worry about that.0347

But since grapes and apples are in that first Basket, it's going to remove them completely, so the only thing left in that Basket would be bananas.0359

That is how these operators work; you can define unary operators the exact same way.0366

Next, we want to look at class methods.0380

A class method is invoked by the class itself.0384

You don't have to actually instantiate any class; you don't have to call any constructor class method; you just call the class name, dot, whatever the method is, and that will create it.0392

So, this is invoked by prefixing the name of the method with the name of the class.0403

Here are some examples.0409

For the Math class, we have--we have many class methods for this--one is just Math.cos; that calls the cosine method.0412

Math.exp calls the exponent method; and then, we also have Array.new that calls the initialize value, which is an instance method from the Array.0423

But this will create the new class; so just Array.new will do that.0436

What happens now is...what I want to do is show you this class method in action.0441

Let's use a real-world example so you can see how it's defined in your class, too.0451

I have this class, Basket; first, remember: when I create this method, it's invoked by prefixing the name of the method with the name of the class.0455

This would be...you would have your class here, and then your method; this is how it's structured.0469

We're going to use that to create our method here.0480

Our method is going to add up all of the items together; it's going to create them all in a created new Basket, just like we do with the additional operator.0483

This is going to allow multiple Baskets to go in there.0492

Here is my class method: I just call define(Basket).add, and the parameter it's going to take is this asterisk sign of *Baskets.0496

It's going to take multiple Baskets, and what it's going to do is, it's going to create this new Basket.0505

In my code, I'm going to say, "OK, I'm going to create this new Array of items."0511

It's going to go through each of the Baskets, one by one; it's going to get each Basket and store all of the items in my local variable right here.0517

After it does that with all of the Baskets, we have all of these items in this local Array; then, it's going to say, "OK, I want you to flatten them; make them one Array; no multidimensional Arrays; and create a new Basket with that."0529

That is our class method at work.0547

Notice, here we are using this unique variable parameter that we discussed in our methods lesson0552

This is a variable argument parameter.0564

Otherwise, now we have our class method.0573

There is another way you could do it: if I have it already defined, I can actually add in the class method later.0578

That's what I'm going to show you right now: I call this opening up the class; this is another way to define class methods, and it adds an additional method to the existing code.0588

All you do is to call that class and just pass in that class name; so I'm going to pass in Basket.0602

This is going to do the exact same functionality as the other code; all this code is the same.0614

But notice, I didn't call Basket.at here; it just says define(add), takes a parameter called Baskets, and takes that Array of items; goes and gets all the items; and creates this new Basket.0622

This might be useful if I have a lot of these class methods; it saves you a little code--I don't have to add the class name in front of add.0638

It's good if you have existing code, and you just want it to find your class methods in it later.0649

It's probably good if I show you an example of this now.0654

Let's get the terminal here: OK, so from this example, we have our IRB terminal set up; we actually defined that basket method.0660

Instead of creating that whole thing from scratch, we can just add on to this basket method now, and create that class method.0670

All I do is just to--first clear it; it's still in memory, so--I'm using that < < that's going to allow me to put in that new class method.0679

Then, I'm going to pass Basket in here; then, I'm just going to do define_add_Baskets.0691

This is our class method; I'm going to declare the items in there, and then I'm going to call Baskets.each.0704

Then, I'm going to create my new Basket from all those items.0722

There you go: I had my existing Basket and added this new class method.0729

Now, let's see it in action; we're going to just get a couple of different Baskets.0735

Let's call one of them Fruits; I'm going to make this Basket.new; it's going to have grapes, strawberries, and almonds.0740

That is my Fruit Basket.0754

I'm going to make one of nuts: let's say I have peanuts, almonds...oh, I called it net instead of new.0756

Now, we have a Basket of that; now we have those two.0772

Now, I can just call my new class method, Basket.add, and I'm going to pass in those two Baskets.0778

We have something going on here...let's see, if I do Basket.add...undefined local variable...OK; I spelled it wrong here; I forgot that s.0803

Let me just declare that again; I'll show you where I made that mistake: here, I put Basket, but it should actually be Baskets.each.0819

This will fix that issue, and then we can continue on our way.0837

That is OK; so, we updated our class method; we still have our Fruits and our Nuts; I can just do Basket.add, Fruits, Nuts...0846

There you go; when I use my add class method, it creates this new method, and it adds them together.0859

I can also add multiple Baskets; so, let's do one--Meats--how about that?0869

I do Basket.new; let's say we have some chicken, some steak; so I have my Basket full of raw meat--it's not cooked yet.0879

Then, I can do Basket.add, put that in, and it's going to create a bigger Basket with everything in it.0894

So, we have grapes, strawberries from Fruits; almonds, peanuts from Nuts; and chicken and steak.0900

There you have it; we used this class method; we are able to use that with our other methods now, and it is still in the same scope for that class object, so we still get that benefit of keeping it simple--keeping it in the scope.0908

It's not part of any instance method, but we can still use it to simplify our code, so it's nice.0924

Next, let's look at the Array and Hash access method; this is very useful--you're not actually in an Array, but you can still use it and define it; Ruby gives you that flexibility.0941

To use it, use these square brackets; this allows you to define your own Hash access method in your class.0959

I have some code here; I just call it Triplex; it takes three values from the beginning, and then you can put that value out and manipulate it to do different things.0974

Let's go over this class; first, we have the attribute reader, so we can read the first, second, and third value from the method.0993

Then, I have this initialize method: it takes three values and creates those instance variables from them.1004

Then, here is where it gets interesting: we have that Hash access method here.1012

Here we have our Hash access method.1019

Notice, here we have the square brackets; it takes a parameter of index; so this is actually like manipulating an Array.1029

I can call the index values from it, and that is what it does; so, when I pass 0, it's going to get me that at first instance variable; 1, the at second; 2, the at third.1041

Let's see if we can take this code and show it to you.1055

Very essentially, it's going to call this triplex method, and it's going to call 0, 1, and 2, and show those values for it.1067

Let's go to our class, so you can see it.1083

Here you see the case index, (1,0), called first, (1,1) called second, (1,2) called at third.1096

Let me go over and show you the other part of that code that you just saw.1109

After this, we want to look at enumerating the values.1116

We are defining the each iterator here.1121

Since we don't have an object to loop, we can just use the yield in our method.1127

What we're going to is define that each iterator; I just called yield; it's going to get that first value, the @second, the @third.1134

Very quickly, you can see the code here: I called triplex.new(1,2,3); it calls each, and it's going to just print out those values: 1, 2, and 3.1153

The next thing we're going to look at is defining, testing for equality; so we're just defining this ==method operator here.1173

I put ... here, just for all the extra code; this is being defined below that. 1185

Here I have this ==; it takes a parameter of other, which is another triplex object.1194

Here, I'm going to test for equality between these objects; if the three values are the same and they're both a Triplex object, it's going to say this is equal.1201

You can define them your own way in your own code, depending on what you're developing, but for this case, I'm going to say they have to be the same class type--that is why I put this method here.1210

This is a Triplex; I want to make sure; if it isn't, it's just going to return false--it's not equal.1224

Here we have our @first; it's going to look at these two values and make sure they're the same.1233

Make sure the second value is the same, and make sure the third value is the same.1240

Here, to test for equality, all you do is...you have your first object here--I want to show you this Triplex object--and my second one (the same value is in there); I'm going to choose this == value and test for operators; it says, "Hey, these are the same; this is true."1252

But notice, if I take this and just pass an Array in, it's going to be false, because this is an Array object, and it doesn't allow that through.1274

Plus, if I create a new one, and I put [2,1,3], that would be false; but also, if I were to just do == Triplex, and just pass [1,2,3] with the new, this would return true.1283

Let's see this code in action; let's go back to that code file we saw earlier.1305

First, we have the Hash method; we already talked about the each iterator; and then, we defined that == to test for equality.1313

The first thing I'm going to do is to run this whole class; it's going to instantiate a new object called Triplex; the first one is going to define three values, 1, 2, and 3.1334

Then, it's going to just access them, just like a Hash.1350

This first one is going to just print out 1; t1 will print out the second value, which is 2; t2 will print out the third value, which is 3.1356

Next, we're going to look at enumerating the values.1371

This is the one with the yield we're using up here.1376

That is going to print each number subsequently on one line, so it's going to be one line, 1; a new line, 2; a new line, 3.1382

Then, we're going to look at testing equality--just that code we just went through with the slides.1398

We're going to have our Triplex new value, [1,2,3]; we're going to create a new object with the exact same values.1405

It's going to--if it's ==, it's going to return true; we're going to check with different types--it should be false.1413

Then, passing it with the Triplex.new[2,1,3]--that would be false, also.1422

But, as I just showed you, we can validate that if we call Triplex.new and just do [1,2,3], and check if that returns true as we assume.1428

I'm just going to call my Ruby Triplex here, and notice we get our Hash access value [1,2,3].1445

We already defined it; now, we have our enumerating value, where we use that each method; we call that yield, [1,2,3]; and then, we're testing it for the equality, so first, we're instantiating those objects, then we test if they're equal.1456

It's going to be true; then we test that with an Array; we're going to get false; we test that with a new object, shifting the second and first value around--it should be false.1473

The last one is where we actually create a new object, and it says true there.1484

That is that; here is our new method here.1492

The last thing we want to look at is just constants.1500

We've already talked about constants in previous lessons, but you can also define them in your class, and it's very useful, because some constants are made just exclusively for that class itself--you want to define it outside in a global scope, then.1506

You want to keep things really organized, so you can create a constant in a class.1522

It's usually defined at the top of the class; that's just convention; but you can do it whatever way you want--whatever is most functional.1528

Outside the class, they must be prefixed by the name of the class; and that is very important.1535

Inside this class, if I call, I can just call 0 here, and that will get me my constant.1542

But, outside the class, I have to call this Triplex::0.1551

This will get me that constant value there.1560

For this constant, what it's actually doing is just to say that if I call Triplex::0, it's going to create for me a new object with none of the values initialized to anything other than 0--so first is going to be 0; second is going to be 0; and third is going to be 0; first, second, and third.1566

Otherwise, that is the end of this lesson on classes.1594

Hope to see you next time here at Educator.com!1599

Welcome back to Educator.com.0000

Today's lesson is on classes; this is Part 3.0002

The first thing we're going to go over is class variables.0010

This is pretty important; you will see many variations of variables in this lesson, and some of them are for instances of classes, and some are just classes.0014

This is class variables; these are the visibility to them; we really want to know what can see it and what can't.0028

It's visible and it's shared by class definitions, class methods, and instance methods of that class.0037

That is very interesting; you can see it in instance methods.0045

By any other language, you would say the same--I can see it in class definitions; I can see it in class methods; but note that it's not visible to instances of that class.0051

If you're using it in an instance of the class, in that one, you're not going to see visibility.0066

How to implement this: the names must begin with this @@; this will create your class variable.0073

I have a little example here: I created this class called GlassJar.0087

Here you see that there are actually four class variables here, so here is an example of those class variables.0096

I have this class variable coins, gummyworms, marbles, rubberbands; and I initialize them all to 0.0105

Let's go to a deeper example, because I have other code that I want to put here, and I have it in another example here.0117

Let's look at my file here: it's called the GlassJar.0132

You will notice that, at the top, I have those class variables: coins, gummyworms, marbles, rubberbands--all set to 0.0135

Then you can see that we have the initialize method to help us create our new instance.0149

It takes one parameter; it takes a Hash; by default, if you don't put anything in there, it's going to make a default empty Hash, items=empty Hash.0160

We're going to initialize first our own local variables, and then we're going to store that into our class variable.0174

These class variables are very interesting, because it is shared amongst the classes.0183

You will see these values you set, and you will see it continue to hold its value as you use it more and more.0189

Let me show you an example.0200

First, for this example, we're going to initialize all of these local variables.0201

Then, I want to say, "OK, in this coins variable, I'm using this ternary operator," and it says, "If coins is nil--if I don't pass in coins in this has--default coins to 0; else, I want you to give me the length of those coins.0208

In this initialize, I'm asking--for these coins, the gummyworms, the marbles, the rubberbands--I'm asking the number of those that are in this jar.0227

For this example, you will notice that there is also a += ; I'm going to continue incrementing that, so if someone creates a new GlassJar that has five coins, and then I create another GlassJar that has ten coins, this class variable is going to say 15, and we will validate if that is true.0239

Exactly the same with the gummyworms, marbles, and rubberbands; I use this ternary operator, and I'm taking this Hash with the symbol; again, if it's nil, it's just going to do 0--it's not going to add anything to it.0262

If you pass in a number greater than 0, we're going to add that in.0279

There I have my initialized method, and I have my class variables being set and added to, whenever this new instance is created.0283

Then, I have this other method; this is a class method, so we have our class method.0299

It's called totals, and all it does is just to report the total number of items.0308

It's going to look at all of these class variables and say, "Hey, here's the total number of coins you have in all your jars; how many gummyworms--the total amount in the jars? Total amount of marbles and rubberbands?"0314

I do some interpolation here; I have the class variable set inside for each of them, and it's going to output this out.0326

After we have this class done, we want to set some of these objects up and report the total.0338

For the first one, I have this Hash; we have coins, and I pass an Array in, so it's going to take this Array, and it's going to change that to a length so it can aggregate this data.0349

I don't care about what kind coin it is for the class variable--only for that instance object.0366

For this example, you will notice it says coins; it sets a quarter in there, and then we have another element in that Hash called marbles, and it puts an Array of two marbles, blue and green.0375

When I call this GlassJar.totals, I should be able to see: coins should have a 1, and marbles should be 2.0394

Here, I set that whole object into a local variable called G, so I just put the output puts G, so we can see the output when you run this code.0410

For the next one, I have another variable called H; what I do here is...I'm actually creating another GlassJar object; in this version, I have two coins; I have a dime and a nickel, so that's two coins; and we have one rubberband--it's an orange rubberband.0420

When I call this coins total, this GlassJar total, you see the coins--it's pertaining to aggregate, so we have two coins here, and in the past one we had one coin; so it's going to be a total of three coins.0441

Then, we don't have any rubberbands here--well, we have one rubberband, but we don't have any marbles here.0461

But, we did in the past one, so it's going to continue to aggregate it, so we're going to have marbles: 2.0470

And we have that one rubberband, so that has a length of 1.0475

So, we have one coin and two marbles; on the second run, we added two coins and one rubberband; so, we have three coins, two marbles, and one rubberband.0481

For my last object, I set it to value I, and I added a gummyworm into it, so it's going to have the exact same totals, but a gummyworm, too.0492

At the end result, we should have 3 coins, 2 marbles, 1 rubberband, and 1 gummyworm.0502

Let's go ahead and run this and see what output we get from it.0518

What I do is run Ruby, GlassJar, 1--that's the example we're going to look at--and you will notice, what I did is, I organized it by the value for each of the objects.0529

For the first one, we have that local variable G with the first value, so we get 1 coin, 2 marbles--we didn't say any gummyworms or rubberbands.0542

Then, for the second object, we take the aggregate of these two new objects; so, the coins total to 3; the marbles total to 2; and we have that new rubberband that we put in, so we have 1 rubberband, that orange one.0553

Then, our third object down--we just added that gummyworm, so we have 3 coins, 1 gummyworm, 2 marbles, 1 rubberband.0567

This is pretty powerful; we have different instances, but we have these class variables that are shared between the instances, and we can access them.0575

But notice that this isn't being called an instance method; it doesn't have access to that; it only has access to these if you use a class method, so you have to use a class method to get that visibility to use these class variables.0589

The next thing we're going to look at is class instance variables.0615

This might get pretty confusing: it's not an instance variable; it's not a class variable; it's called a class instance variable--leaning more toward the class side, though.0619

When I say that--it's more similar to a class variable than to an instance variable.0630

It's associated with the class, rather than the instance of the class.0638

The best way I like to put it is, with the number 3 here, it's instance variables of class objects.0644

You have your class objects--this is your instance variable.0652

It also may get confused with instance variables; the reason is that the syntax is the same.0658

So, what you would see in an instance variable looks just like a class instance variable.0669

What is the reason we use these class instance variables? You can subclass an existing class.0677

Let's go ahead and look a little deeper into how this is used.0683

Here I have class instance variables, and we're just going to use the exact same example we did in our first one, but in this case, we're going to do class instance variables in its place.0688

Notice, we're just using an @ sign; there are no two @'s.0699

This is that class instance variable.0705

We're just setting it to zero like we did before.0712

Let's go ahead and look at an example, so you can see some more of that code that goes right below here.0715

I have an example; it's just called GlassJar2; it does the exact same thing as the first code example we were looking at.0727

You have your class instance variables; they're all set to zero; let's look at our initialize method.0737

This constructor is interesting, because we're defining the initialize, but, since we can't use that class instance variable here, we have to actually set it up in self.new, which is a class method, so it allows us to do that.0743

First, we look at initialize; it's very simple.0763

You take this Hash of items, and we're just storing it to local variables: coins=items(coins), gummyworms=items(gummyworms), marbles, rubberbands...0767

That is not really relevant, so we should probably actually set these to instance variables, just so they could be used now; because, if I set them as local, it's not going to be used anywhere; but for our case and purpose, we're not worrying about that, because we'll implement that instance method later for whatever purpose we need to.0779

Getting back to the example: we have these class instance variables: what is important is this self.new method here.0800

We have a class method that allows us to initialize the class instance variable.0810

Here, we have @coins, and we're just doing the exact same thing we did in the other example; it's going to take that Array and check the link.0817

If it's nil, it's going to be zero; if there is an Array of coins in there, it will get the link, and it will store that in here.0829

It also has a += ; so it's keeping this total account, so it can continually add it in and utilize it as an aggregate as all of these GlassJars--and we do that with marbles, gummyworms, and rubberbands.0837

Again, we still have that same class method here with self.totals; it takes the total number of coins; the only difference is--look here--we're using instance variables in here, but remember, this is getting that class instance variable.0855

It's kind of confusing, but just think of it as, if I'm using this class method, it's going to grab that class instance variable.0872

It doesn't have access to those normal instance variables.0879

Gummyworms, marbles, rubberbands...it's just going to output the same thing.0883

We'll see that again.0887

We have our example here; again, let's go ahead and add them up: we have coins, which is going to be one coin; two marbles; and for our H object, we have two coins, so that's going to make it coins-3; we still have 2 marbles; and one rubberband.0891

Then, for our last one, we just add that gummyworm in.0913

That is the only new thing that's in here; so let's go ahead and take a look at that.0918

Let's have this run and see if it has a similar output; we have 1,2,3,2,1,3,1,2,1; I'm going to clear it and just do number 2, which is the code we just saw.0932

They do look exactly the same; so, we've used class variables and class instance variables, and we were able to mimic the exact same logic we've been doing here.0946

That is class instance variables; let's get away a little bit from variables and look a little bit more at method visibility.0970

You have three types of method visibility for instance methods; we're looking at instance method visibility here.0980

They have three types: they have public, private, and protected.0993

Depending on what visibility, you can access it from different places; you have different scopes.1001

How does it call this public/private/protected? These are instances that belong as part of the module class.1009

From all the different code you've already seen in the past, we haven't called protect and we haven't called private, so by default, they go to public--so automatically, they default to public.1020

There is one exception--that initialize method; that one is always private.1034

That is why you have to call that new method; you can't call initialize.1046

Let's look at those again; number 1, we have that public method that can be invoked from anywhere; number 2, for private methods--private methods can only be invoked by other methods of that class.1053

That's interesting; I can't just go and make this new instance variable and just call it from its reference; only methods inside that class have access to these private methods.1070

Number 3, protected methods, can be invoked from within its class or its subclasses; so protected method gives it a bit more flexibility; not only can my own methods call it, but subclasses of it can call it, too.1086

By the amount of visibility--let's say here we have greatest visibility vs. least visibility--you would see that public has the greatest visibility, and then protected, and then private.1106

In this trend, least visibility to greatest visibility...1155

Let's look at how I use these methods; I just stated what they do; let's see it in some real code.1164

If public, private, or protected is called without arguments, all subsequent methods below it will have the specified visibility.1174

This is very simple; if I just call this method, what it means is, I call it, and anything below that is going to be that visibility.1185

First, let's look at this example here with this GlassJar.1196

If I don't call any of these methods, it's going to default to public visibility, so all this part is public.1201

The next thing, I call this method called protected; again, I call it without any arguments, and any code below this is going to have protected visibility to it.1214

The last one, just like I said before--I call private; anything below that will have private visibility.1240

It overrides; as long as I put a new method there, it's going to override and take precedence to whatever was in the past.1248

So, all of this is going to have private visibility.1257

Now, I didn't actually include any code here, but all this stuff is where your code would be, and all of it will have that scope; so, if it's private, all of these methods will be only accessible by other methods that are inside this whole class.1265

Public and protected, as well--of course, they have access to these classes.1295

Now, let's look at invoking method visibility, but this time, what if we include arguments--how do you do it with arguments?1300

First, with arguments, they alter the visibility of the method.1308

The argument being passed is the name of the method in the form of a symbol or string.1315

What argument am I passing? I'm passing the argument as the name of the method; that's the key part here--that name of the method.1323

Let's look at an example; we have this GlassJar; we have three methods: one processes marbles, one processes rubberbands, and one erases all values from that jar.1334

By default, these are all public; but notice, right down here, we actually have these methods, and we have that argument; so we have protected, and then we call process_marbles; so this is our first argument; and then we have another one that says process_rubberbands, and this is our second argument.1347

It's under protected, so both of these are protected methods.1379

Then, this last one, we called private--so only methods in this class have access to this, to erase all items.1391

This one is private.1401

We looked at method visibility with those instance methods; let's look at class visibility.1412

We're going to look at variables, constants, and methods; let's look at the whole range of them.1424

First, instance variables and class variables are essentially private; they can only be called within that class itself.1432

Constants are public; if you wanted to access a public--we've seen in the past--it's very easy: you call the object with a ::, and then you have pi--this is public; it's very simple to call; it's open outside the scope of that class.1442

Now, let's look at changing the visibility of the class methods.1469

For the first one, you can change the class method to be private; so you just call private_class_method; it takes one argument--that's the method name.1475

This makes that existing class method private; and the argument, remember, is passed as a symbol--that is important.1500

The next one--let's say you have a private class method; you can turn it into a public class method.1509

You probably won't use this too often, but we should talk about it, still.1513

We have this public_class_method; it takes, again, that one argument--that method name; it makes existing class methods public, and the argument is passed as a symbol1518

By default, class methods are public; so you would have to make it private before public, and then call this again, so you wouldn't use it too often.1532

Let's look at an example here.1545

Again, let's continue with the GlassJar; you understand what we're doing here.1548

For the first one here, we have a class method, and it says to collect all items: it's going to grab all items--you give it a bunch of jars, and it's going to collect all the items and total everything up.1552

Then, what we do is--this is our class method, but we say, "Hey, we don't want access; we don't want people to have access to this; they're looking at our object here; I don't even want access in that class."1577

So, I can call private_class_method; stick in collect_all_items, which is that class method here; pass it in; and now, you've privatized that method--it's private now.1586

Normally, we could call that--so I call GlassJar.collect_all_items--now it will say NoMethodError; this is a private method; I'm not going to allow you to have accessibility to it.1608

Let's next look at subclassing; we're going to talk about that and then talk about inheritance; in Ruby, you will see that they are very similar--they're different terminology, but we're going to go over both of them here.1629

First, subclassing allows the creation of a new class based on, but also modified from, the behavior of an existing class.1647

This allows me to extend the functionality and modify reused code, but I don't have to repeat myself, which is nice.1662

This class can extend another class, where the child is called a subclass and the parent is called the superclass.1672

This is important, because we are going to use this terminology coming up.1681

I have class A, which is a parent, and class B, which is the child; the parent here is called the superclass, and the subclass is class B; the child is called the subclass.1684

Remember, this child extends this behavior of the parent to it.1718

A class can have a number of subclasses; in this case, child has a subclass from the parent.1729

Every class has a single superclass except BasicObject; BasicObject, if I asked it, "What is your superclass?", would say nil.1740

Remember, with this superclass, I can call it on any object; I can call Math and say, "Give me your superclass," and it will tell that to me.1749

Then, I can call the next object from that and call its superclass; eventually, it's going to hit BasicObject, and when you call BasicObject.superclass, you're going to get nil, because it doesn't have a parent class to that--that is the last one.1760

Again, here we have an example with the GlassJar.1784

We might call extends--GlassJar extends class Jar--so GlassJar is the subclass; Jar is the superclass.1789

Let's look at inheritance now that we've talked about subclass/superclass, child, which extends parent...1807

Inheritance allows a class to have the same features of another class above it; that class inherits those features.1820

Now, we can say that, in this case, child inherits parent.1827

The parent methods get inherited by the child.1842

We said they extend in the subclass; a class also--here we go--may extend or inherit from another class known as the superclass.1847

So, class may extend--we said that about subclassing--now you can use that inheritance, or inherit from the class known as the superclass.1858

Every class inherits directly or indirectly from the Object class.1868

Now, this Object class--if I call its superclass, that will be that BasicObject.1874

Object inherits from BasicObject class, just as I said.1881

From the previous example, class GlassJar inherits from class Jar.1886

So, let's bring it all together; you have subclassing and inheritance.1896

Descendants are the subclasses of a class, so that's descendants--and they are subclasses of the subclasses, so all these are called descendants.1902

Now, if I want to look at ancestors, ancestors of a class are the superclass, plus the superclass of the superclass, up to the Object.1916

Again, let's say, looking at Math, I look at one of its superclasses--Object.1928

Then I look at another superclass; I look at BasicObject.1938

What this says is that, for this object, anything above it is called the ancestor; so if we're looking at Object here--here is our scope; we're looking at Object; anything above is an ancestor; any things below it are called descendants.1946

What other descendants do we have that are objects? 1969

We have Array; we have String; we have Math; we have Hash; the reason is that all of these objects have a superclass of Object.1973

So, these are all now descendants of the Object, as well.1989

But, changing your scope--as if I were looking at Math, Array, String, or Hash, and I want to know what its ancestor is--BasicObject or Object; both are ancestors of it.1994

Now that we have that terminology down, let's look at how I extend a class.2008

It's fairly simple; to extend a class, all you have to do is add this < sign, and how you do that is you append that to the class name.2016

You append this and the superclass to the class statement; here is how you do it, right here.2030

So, what we are saying is that GlassJar extends Jar.2039

Also, GlassJar is a subclass of Jar.2054

The best way to see it is code that is in action, so let's go ahead and throw up our IRB...our Ruby terminal.2064

Let's go ahead and create some code; let's show an example of how this inheritance is working.2075

First, I'm going to define this Jar; I'm just going to say, "Hey, tell me what class I am--when I call this to_s object, I just want you to tell me what type of Jar I am."2080

The reason is...self.class will print you Jar; but when I call this GlassJar, I'm extending Jar, but I'm not actually defining that to_s method.2097

Let's see what it outputs; so first, I'm going to just initialize that Jar object, and I just call the class...2112

OK, that makes sense; we have a class that's Jar here; but now, let's see if I did GlassJar.2120

I call class on that; notice, when I call new, it tells me the Jar, and the type is GlassJar; I can also just call the to_s method right away, so you can compare.2140

We see inheritance working here; we extended the Jar object and actually used that method from the parent, and extended it so GlassJar has this to_s also.2155

OK, it inherited from that object; but let's see more of this inheritance.2170

First, we call to_s; we have that GlassJar; to_s; we have Jar; and let's look at it at a different angle.2183

Let's see if we defined it further--so let's look at some other examples to show this.2193

We talked about extending a class, and next I want to show you an example of code where we override this method.2200

To do this, we're going to look at a method called type.2208

Here, we're going to override a method; this subclassing that we just showed you allows you to modify an existing method, too.2211

We have this Jar; we're going to have this new method called type.2222

If I just call type and Jar, I want it to return generic; this is a generic jar; I don't know what type--what material--it is.2228

But with this GlassJar, when I call type, I want it to return glass.2234

Let's see it...I'm going to exit and run it again to clear the logic in there.2241

First, I'm going to just call GlassJar; I'm going to do type; notice, I'm doing everything in that one-liner syntax--it shortens it up; I don't have to put in three lines.2249

Then, I call GlassJar extends Jar; now, if I go to Jar.new and I call the type, it gets me generic.2262

If I call GlassJar.new and call type, notice it also calls generic; OK, it inherited, but I want to override that method, so let's show you an example of override now.2273

Now, I'm going to call GlassJar, redefining the class object.2287

I'm going to put type here, and I'm going to pass glass in there; so now, when I call GlassJar.new and do type, notice it says the type is glass.2292

Now, we have just overridden the pass method in our subclass.2309

So, we have that power to do that.2317

Let's move on from here; let's look at chaining; that is the next thing we want to look at.2330

What chaining allows you to do is, if I'm looking at a method, I can actually add on to that method--keep the existing one, but add on.2340

Instead of overriding the method, chaining allows you to augment the behavior of an existing method by adding code.2351

The keyword super allows the chaining process to occur.2360

What super is is this method that invokes a method with the same name in the superclass of the current class or an ancestor.2366

This super method--sometimes you will see it with arguments, sometimes not; it depends what is being initialized and what the superclass requires.2376

Arguments can be passed to it, if necessary, by the superclass.2388

Let's look at an example for that.2393

I'm going to start my IRB again; so, for this one, I'm going to create a Jar; I'm going to put some attributes here this time--so I'm going to have some marbles and rubberbands.2402

I'm going to have my initialize method now; it's going to require marbles and rubberbands; and we're going to set some instance variables here.2416

We have our Jar object; now we want to do our GlassJar; this one is going to say, "OK, you have marbles and rubberbands, but I'm actually going to initialize mine, so this can take pens, too."2433

Hold on; I made a little mistake--I'm supposed to actually have my subclass to Jar; then I'm going to call pens; because then, it knows, when I call super, who is its parent; the parent is the Jar.2454

I do attribute pens, I call my initialize, and notice that my initialize--I'm actually using three arguments; I have marbles, rubberbands, and pens.2467

All I'm going to do is to call super; so, for marbles and rubberbands, I already had passed functionality--my parent already knows how to set these values for me concerning my object.2480

So, I'm just going to call super and let it handle it; it already knows how.2491

Since it doesn't know how to handle pens, though, I'll set that myself.2495

It says there that we have superclass mismatch for GlassJar; let's see how we can fix that; for the first one, we may have redefined it; when we extend it to Jar, it says superclass mismatch.2503

Let's set that up again, so we start on a clean slate; I think what happened was that I set that initially without it, and it influenced that class.2546

I'm doing the attribute marbles and rubberbands...I'm going to do initialize marbles and rubberbands...marbles=marbles, rubberbands=rubberbands...OK.2559

Now, I'm going to call GlassJar and extend Jar; and here we have our pens; call our initialize, marbles, rubberbands...and we have pens now...2578

I'm just going to call super; it knows how to handle these--I don't need to worry about it...but you know what? I'm going to set those pens and...2592

OK, so we're good now; we solved that issue.2603

Now, first, I'm going to initialize that Jar object; I'm going to put a number that tells me how many marbles and rubberbands; I'm going to do 5 and 4.2608

Here we have our Jar object: 5 marbles, 4 rubberbands.2618

Now, let's look at GlassJar; I'm going to call GlassJar.new, and I'm going to call 1,2,3; that is three arguments, so that says I have 1 marble, 2 rubberbands, and 3 pens, and it sets those here.2629

Also notice, it did that super method; it got it from the Jar--we don't have to handle it there.2645

If I call marbles, it has 1; rubberbands has 2; I'm going to call pens, and it has 3.2652

What if I do pens in Jar?--remember, Jar doesn't know how to handle pens; this Jar doesn't have the capability; only that GlassJar does--so pens doesn't work.2664

Rubberbands works fine, and marbles work fine.2677

That is our chaining.2690

The last thing we want to look at is the singleton pattern.2694

The singleton pattern is a class that can only have a single instance.2702

It's interesting: we have this class, and it's only going to have one single instance.2714

If I create any others, it won't allow me to; I can only have one instance.2719

There is a special way to implement it: Ruby has a singleton method that implements it.2722

So, you have to make sure the module is loaded; to set it up, you have to run require 'singleton'; then you have to include singleton in whatever class you're doing--so that's include Singleton; and then you need to define and initialize method for the single instance of that class.2728

Then, all you would do is, after it is all completed and set up, you would set up 1,2,3; whenever you access this method, you have to use instance; you have to call instance provided by the module to access the class.2752

Let's say I have my object; I would have to call instance and then whatever method I have after that.2766

Let's go ahead and look at the RDoc for this.2784

Here, look at the RubyDoc; here it says that "the Singleton module implements the Singleton pattern"; to use it...let's look at an example.2789

I have this class; first I have to include that 'singleton'; then, when I call it to get the instance, all I do is to run Class.instance, and it's going to get me that class.2803

Look, they actually did == , and it says it's true.2818

Notice, if you do Class.new, it's going to give us this NoMethodError.2822

This new is private, so it's not going to allow it, because it's using the singleton pattern.2827

"The instance is created at upon the first call of Klass.instance."2836

Once you call that, then it's going to create it; notice, you call the instance, and then the object space has an increase of 1 of value to it.2842

The actual code from this RubyDoc is pretty slim, but it gives you a lot of knowledge of how the Ruby module has set up this singleton pattern.2855

How is this achieved? If you were to do this yourself, without doing include Singleton, you would have to make that Class.new and Class.locate private; you would have to override Class.inherit, Class.clone...2867

So, to insure that these singleton values are kept when inherited, and when they're cloned it would be the same instance, you would have to provide that class instance method that returns the same object each time it is called.2881

You would have to override the load to call Class.instance, and you would have to override that clone and dup, too--you don't want them creating new objects of this, so "raise TypeErrors to prevent cloning or duping."2893

By default, "#_dump...returns the empty string"; "marshalling, by default, will strip state information"; "classes using Singleton can provide custom _load...and _dump...methods to retain some of the previous state of the instance."2909

So, you see, here is the example; you have to run that require at the top; you have to include Singleton; and then, you notice, they call the _dump and _load here.2926

Then, you can call instance, and then you can run some methods.2938

Notice, they have some accessors--keep, strip--they can run those here, set those values...2944

They use Marshal.load, so you can dump a state into a local variable and load that back in.2950

You can also look at the class methods to see how it does that.2961

Now, let's look at our own example using the Jars.2979

Again, first, let's look at implementing this; so first, we have step 1, to make sure you require the 'singleton'.2986

If you're using a framework, you might have to initialize it beforehand; whatever is best.2996

This code will require that right here--I'm running IRB--to include that Singleton.3001

3: You set that initialize method up.3011

So, we have 1, 2, 3 steps here; we have the Singleton module loaded; we can include that in here; so now, we've included the module; we loaded the module; and third, we set up the initialize method for this instance.3015

When they call this instance one--when I call instance--it's going to set this up, and it's going to set marbles, rubberbands, and pens to 0--all of them to 0.3048

I do use these commas to keep it on one line.3061

Then, what we're going to do for this object...we're going to have this add method, and it's going to allow us to continually add as we get more marbles, rubberbands, pens; add it into our instance object here.3065

It takes one argument; marbles, rubberbands, pens default to 0.3088

If we do place a number in there, even with 0, it will add them into the totals.3093

Just like our previous slides, we're going to have something that does the total items.3103

Here, we call marbles, and it's going to show me how many marbles I have; rubberbands--it's going to show me how many rubberbands I have; and pens--it will show me how many pens I have in there.3110

Let's go ahead and run some code that has this.3123

I think I have it here in Jar.totals...3133

OK, here is our code; we load our Singleton, include it, have our initialize; we have add--we talked about total items...3137

I'm going to run this file; it's going to run some things: first, it's going to call this Jars.instance, total items. 3152

First run--it's going to create this object...set it to 0; it's going to return it to total items.3161

We haven't set anything in here yet, so this is just going to get 0 for all of them.3168

Then, I call this add; it's going to add a marble, a rubberband, and a pen; when I call this totals, I should get marbles: 1, rubberbands: 1, pens: 1.3173

Then, when I add 2, 3, 4, this is going to aggregate them together, so we have 3 marbles, 4 rubberband, and 5 pens.3187

I just commented on the side, so it's not going to actually affect the code.3204

I call Jar.totals, and you notice here that it does do it: we all start with 0; then 1,1,1; then 3,4,5.3209

That is the singleton pattern; that is the last thing I wanted to go over in this lesson on classes.3222

I hope you enjoyed it, and see you next time at Educator.com!3231

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

Welcome back to educator.com. Today's lesson is on using basic tools for using Ruby.0000

The goal is to look at a lot of common tools that I will be using, and you will be using, when you develop in Ruby. 0007

The first one, that is most important, is Interactive Ruby (IRB).0016

It's a command you can use in the terminal to run Ruby commands.0023

Without even building any code, you can just debug and test things with it.0028

So, that is IRB. It's called the Interactive Command-line Environment.0033

It's good, because I can see results or errors after I enter each statement.0040

So, some examples: first, I can run "irb-v" and it will tell me what version I'm running. So, it says I'm running

Afterwards--if I execute "irb"--no arguments-- it's going to execute the IRB terminal, and you will notice it's executing, because you will see the Ruby version and the patch number to it.0060

Then, it has this "greater than" sign that tells you, "You can enter a Ruby command here."0078

So this command...I put "hi"...it spits back a string of "hi."0085

I can do some math commands in there. Let's go ahead and see it live.0090

Like I said, you can do irb-v...actually, now I'm on

Put in "irb" and press return...I can try some different things.0101

I can put in strings--"test"--it returns "test" back-- like a reason; it says, "OK, I'm going to return that string back to you";0106

I can do "15+5"; it says "20."0114

I can do some standard output commands. I can do puts "hello world," and it looks like it's working fine.0119

When I want to quit it, all you do is just put exit, and it returns out.0126

It's a good way to test my Ruby code as I'm developing, to check and see if certain things are working right, if I can load specific modules.0130

And libraries, and gems. 0137

So, why use Interactive Ruby?0143

Like I said, use it for debugging. 0147

When you are creating your code, you are going to have complicated classes, modules, objects...use it for debugging, so you are not having to go through all these steps--you can just look at that one piece and fix it.0151

We're playing around with Ruby, just getting more familiar with the system, getting more familiar with libraries, modules, and Gemsets...you want to learn how it works.0165

Getting more familiar is always good. Using this Interactive Ruby, we can do this.0177

The next one we want to look at is this tool called RDoc.0182

It's the documentation tool system in Ruby.0191

You might use JavaDoc for Java; Ruby uses RDoc.0197

And what this will do is it will generate your documentation from the Ruby source code.0201

But they do have a syntax, and they do have a layer--a structure of how you want to do it.0206

So, to let you see it, we are actually going to go and look at the Ruby core documentation, and take a look at how to build it first.0213

We're looking at the Ruby Documentation System.0220

Do you notice, at the top, they have the structure--"Methods," "Classes," "Files"--0230

This is actually their own "RDoc" of the RDoc app itself!0234

At this RDoc library, they actually tell you the libraries they use for their methods.0241

We actually don't want to look at the underlying things, though; we just want to understand it.0246

First, they give you a nice intro: it tells you, "This package uses RDoc; it uses simple markup. It's an application that produces documentation for one or more Ruby source files."0251

When it generates this documentation, it's going to generate it using HTML.0264

So the output is going to be HTML as it generates it. It tells you how you install it; we already set it up with our "setting-up-the-environment."0271

But, for those that didn't use RVM, it would tell you here how to do it, as well.0280

But we want to move on to the summary here.0287

Just see its usage: it's a command-line; you just run rdoc; it takes flags, options, and names.0292

This is most important: a typical usage, just to generate the documentation, all you do is run rdoc and return;0299

But to generate documentation for your source code, "This command generates documentation for all the Ruby and C source files in and below the directory."0306

So, now that we know how to do it, we know how to install this RDoc.0317

But how do I know the way to structure it in my code?0325

Let's go ahead and do that now. First, you can see the usage: here are all the flags you can put in it...0329

For our purposes, we just need to run RDoc.0336

But first, let's look at an example of how they do it. You will notice they use a lot of these hashtags.0341

These hashtags are actually comments in Ruby.0347

You see that they have author, copyright, and license.0352

RDoc would generate that out, and it will make it look very neat, to show these different properties.0356

Here they have an example of a class, some methods, and all of this will be documented in the RDoc code.0363

What's important to us is that markup code. This is how I standardize it,0372

and organize this code in the RDoc. 0378

First, it says, "comment blocks can be written fairly naturally."0382

And, it tells you I can make paragraphs. "Paragraphs are lines that share the left margin.0386

Texts indented past this margin are formatted verbatim."0390

If I want to make lists in RDoc, it says I can use stars or dashes for bullets in my bullet lists.0394

Put one each time, and it will generate that into a bullet list.0400

"For example, the list that produced the above paragraph looked like..."0405

it has the description, then it has a star, then it has bullet points.0411

Let's look at the next one, number 2--Labeled lists--"Labeled lists...are typed using square brackets for the label.0419

So, you just do the square brackets, put the label in, and then you will notice they have plus signs as a command to copy the standard input.0426

Also, they have a labeled list that may be produced by using "a double colon after the label," which we saw in their example code. "This sets the result in tabular form, so the descriptions all line up."0434

"This was used to create the 'author' block at the bottom of this description."0448

Author, label...you can put the title...0452

And that's that.0457

Let's move on to the headings.0464

On the headings, notice that a Level 1 heading has one equals sign.0470

A Level 2 heading has two equals signs.0478

What happens...a Level 1 heading is like an <H1> tag--it's a very big heading.0483

2 would be a subheading to that; a Level 3 is a sub-subheading.0489

So, just like <H1>, <H2>, <H3>...it's going to get smaller and smaller.0495

You can do your subtopics. You can also do italic, bold, and typewriter font-- you just add this italic, bold, typewriter tag. I'll show you how to do that.0500

Names of classes, source files, and methods, "containing an underscore or preceded by a hash[tag], are automatically hyperlinked from comment text to their description."0513

It will also tell you how "method parameter lists are extracted and displayed with the method description."0526

"If a method calls yield, then the parameters passed to yield will also be displayed."0535

So, if it runs yield like this statement, which we will get to later, then it will tell you what method it calls it, and also what parameters will be displayed to it.0540

And then there are other directions with the comments, but you can look at that, and just get into it, to see it in action.0560

What I'm going to do now is run the terminal.0571

I actually have two code files ready.0575

I have a book rb and a hello world.0581

What we are first going to do is, let's go ahead and look at the code, before we actually generate the RDoc.0586

First, here is our book example.0597

It's a class...attribute assessors...0602

It has a class variable,0607

some for instance variables, and some methods.0611

I didn't actually put any R documentation in here. 0615

So let's look at...an even better example, but we can still see this in action after we do RDoc.0618

It still gives good information, even though we haven't documented it.0623

Here we have a hello world, and this...I do have a class itself...I'll show you puts "hello world"0627

And you will notice that I do have headings here. I used that equals sign.0634

And I used equal equal for a subheading, and we have a sub-subheading over here.0638

And every heading, afterwards I put some verbage...so let's see how that looks in RDoc.0643

We have our headings here, we did a little markup, and let's go ahead and generate that out.0651

Back in my terminal, I just run RDoc.0656

Very simple command...you will notice it says three fouls.0660

It says how many classes, attributes, methods, and it says, "OK, ten percent is documented now."0665

Now, when I run List again, you will notice that there is a directory here that says "Doc."0670

If I go in that directory, you will notice that there are a bunch of files there now.0676

I'm going to open that index, that HTML.0681

And you see it says the RDoc documentation, it gives us two classes, and it gives us four methods. Let's go ahead and look at that book example.0686

We saw the code to it. Notice how it organized it. We had those four attributes: it says author, content, description, title.0697

And we had a public class method, and you see it there, and it tells you the arguments and the parameters for it.0706

Then, we have two instant methods: we have a summary and wordcount.0711

I can click on it, and it will expand it out and give me the sourcecode to that.0716

So it will give you the line numbers--it's nice--and that code, they parse it out, and they organize it here for us.0720

But a better example is--let's go to that other class, with the hello world--0728

It had a class code, myfirstclass. It's a very bare, simple one; it only had one method.0733

But notice my headings. Here is that one equals sign.0739

Then we have our subheading here, and our sub-subheading here, and they get smaller, and the text that I put below it--it has a text block there for that, as well.0743

And I can click on it, and it shows me the method.0756

I can click back to home, and it will go back to home of the RDoc.0760

And you see it has the classes and methods there. Again, RDoc does generate in HTML--you see some HTML here.0766

Let's close that back up...so that is RDoc for you.0775

So, why use RDoc?0781

One reason is to learn the core Ruby functions.0786

RDoc is key to understanding libraries. Sure, we took our existing code and generated our own RDoc.0793

But you also want it to learn about the Ruby code--the standard library in Ruby.0802

If I want to learn the Ruby core code base, this is very important.0809

What I can do now is...let's go...I can show you that.0815

So this is actually the RubyDoc for the Ruby core.0820

We are actually going to reference this quite often in a lot of these lessons.0826

They have one section for the methods and one for classes.0830

For example, let's say that I wanted to learn more about that Fixnum class that we talked about.0835

This one is methods, so it will tell me all about the methods, but what do I want to learn about a class?0843

I know exactly which one I want to look at, so I can go up here, and those are in alphabetical order.0853

Now that I have found Fixnum over here, I can click on that, and it will give me RDoc information about this Fixnum class.0858

It will have it here. It tells you about instance methods--it will tell you about class methods, too--it gives you text descriptions of what they do, and it gives you examples, as well.0869

So you see, even the core libraries use RubyDoc to document their own code, and now you can do as well in your own code.0882

And you will notice also, in this Ruby core, there is quite a bit of libraries and objects here.0895

For this course, we will go over quite a bit of these classes and method objects--many of the common ones.0904

There are a lot of them here, but we will go over many common ones, so it makes it a lot easier to get familiar and learn other ones, as well.0914

RDoc is key to understanding the libraries. Once you get going, it makes it a lot easier to develop, as well.0922

I just showed you generating the RDoc; you just run this doc command, and you saw that it generated into this Doc directory.0931

We saw the RDoc documentation, and you can run rdoc-help; it will give you the usage information,0943

and if you will need to add any special flags or commands to that, as well.0952

The next thing that we are going to look at is the Ruby Interpreter. You are going to use this quite a bit.0959

We have our code pieces...you want to run a script. You are going to use it as a script.0964

You can just run ruby--help to see if there are any particular things you want to run with that.0969

You can run that ruby--help, and you will see that there are different switches, to program, where you actually put in the filename and any arguments you want to put to that.0977

For example, I can just run Ruby, put the program here, press Return, and it will execute it.0988

For example, we were using the hello world; all you have to do is run ruby helloworld.rb, and it will execute that Ruby script. Let's go back to that code.0995

Ignore all that markup--all that description.1005

You know it's there. It says ruby helloworld. But we want to look at that code.1010

We have this class that has one method. It says "test."1015

Actually, even though it has this class method, it's not even using it. It's just puts "hello world".1021

Let's go ahead and use it. Let me just say...some code...first class=myfirstclass.new...1029

And I'll just output that.1036

For this method...so it should output "hello world" and "test."1040

We have that saved there; now let's go ahead and execute it.1048

I'm out of my RDoc directory; I'm back in the current relative directory of my code, which would be hello world; And notice how it outputs "hello world" and "test".1054

So, it did exactly what I wanted it to do. I ran that Ruby command...1071

Let's just run ruby-help and see what other usage things I can do with this command.1077

You will also see the switches, the program for all the arguments...1083

And there are a lot of different arguments, depending on what you're doing.1088

You will find them important for your task at hand.1091

Just for this simple strip, we're not going to use any of them.1095

You can check your version, too--just ruby--version--1103

And that is the Ruby Interpreter.1107

You will use that quite often.1111

The next one we're going to looke at is Rake.1117

This is a build tool that helps you build, compile, and process large files.1122

You can tell it to do complicated tasks, but make it one simple push-button.1129

You can say, "Rake, I want you to deploy all this to all these servers."1137

You can write all the different pieces to that, and next time around, it's already implemented; just run Rake, deploy to all servers, and it will push it all out there.1141

There are some similar tools like this in other languages.1151

In C, you have Make; in Java, you have Apache Ant.1155

If you ever run Ant, it's kind of like Rake in that you just run your Ant command, and then you run your file, or your task.1161

So, bringing that to Ruby, you would have Rake.1178

You would put your task here.1182

Let's go ahead and look at the Rake RDoc.1187

We'll get a better feel of how it is used and how we can build it ourselves, for our own code.1191

This is actually from our own RDocumentation, but here it is: rake.rubyforge.org--1199

There is some information here: it's called Rake; it's called Ruby Make;1209

"This package contains Rake, a simple ruby build program with capabilities similar to make."1215

It "has the following features: Rakefiles...are completely defined in standard Ruby syntax."1220

Is that interesting? The tool is all built in Ruby, as well.1227

No XML files, no Makefile syntax.1232

It supports patterns for tasks, Flexible FileLists, and it has "a library of prepackaged tasks to make building Rakefiles easier."1237

..."tasks for building tarbells and publishing to FTP or SSH sites."1245

To install Rake, all you have to do is run gem install rdoc, push Return, and it will install Rake.1249

If you want to get deep into the code, all you have to do is add these things called tasks to it.1260

So, following this example, "You must write a 'Rakefile' file which contains the build rules." And here is the example:1273

If I just want...in Rake, you can tell it to default a task.1281

So when I want to run Rake, I say, "I want you to default to run a task called 'test.'"1285

When I do that--I press rake test or rake return--1290

it's going to run Ruby Interpreter and the file.1297

As you see here, it says it has two tasks: a task named rake, which, upon implication, will run a unittest in Ruby--and you see, it says unittest.rb, and a task named default, which does nothing, but has one dependency, namely that test task.1302

So, that default says, "I want to run this test task," but it's also an array, so I could add multiple tasks into this array.1320

You just give a comma and add more tasks to do sequentially, one by one.1329

You see how they run it here; they run Rake; it's going to automatically run that Ruby command.1334

No more do I have to go through ten lines every time I run a command; I can just use Rake and give it all in that one task.1342

"Handle it for me. Organize it."1348

Then you are going to go more in here--they give you a lot of documentation about reports, about Rake...1353

there is a GetHub where you can make requests and comment on things...1362

We can see the codes, too, for Rake--there are source codes out, open source, at GetHub.1368

Then they have reference materials here--presentations and articles about it and more references we can look at about Rake, and just get a really good feel of it.1375

I think what we should do here is actually show examples of it.1390

I actually have one. We're using that hello world.1398

Using that example, we just use that task: default,1402

And when I tell Rake I want to run that hello world, 1406

that thing you saw, the hello world test, is going to do the same thing when I run this Rake command.1411

To know I have it installed, I can just do rake-whatever version.1416

OK, it's there...rake-help, and you notice how it can output that1424

If I run Rake, it executes that task.1434

This will be hello world--it's hello world test.1439

And also, I can just run the task itself, specifically--so I can just run ruby hello world,1442

And...oops, I meant rake hello world...1447

And then it will run that task.1452

And that runs ruby hello world. "hello world" "test".1457

We have that code...and that is your Rake.1464

So, why was it created?1470

It removes all of that weird Make syntax that we were seeing in the past.1478

You don't have to worry about XML scripts, like in Ant.1483

It's platform-independent build scripts. So no more weird syntax; you can use Ruby, which is great.1490

No more XML--platform-independent. And you can run Ruby anywhere now where it exists.1500

No more need to have Make installed; that is a great reason, as well.1506

So, in summary, what have we learned today?1512

We learned the IRB, the Interactive Command-line Interpreter-- the Ruby Interpreter-- to run our executables, RDoc to do our documentation, and then we learned about Rake, which handles our complex tasks and makes them simpler.1520

Those are the few tools that you will be using quite often1556

That is why we brought it into the lesson.1569

Now that you know these tools, we can use them, going forward, with the next lessons, as well.1574

Before that, I also want to show you the ID editor I will be using1582

When I'm editing and showing code, as you saw,1593

I'm using a program called Mac Vim, so if I go ahead and Google Mac Vim,1597

It's the top one, Vim for Mac.1604

They also have Vim for Windows.1607

Just search for Windows Vim and you can get it, too.1612

But it's a text editor--Vim for Mac OS X.1616

You can just click this link and download it, install it, and then you can use the same editor I've been using.1620

You can open .rb files and edit them.1626

Open source is free. It's nice. It does the job.1632

So, also, we want to add here to check out that Mac Vim.1637

Or, if you are in Windows, the Windows Vim. Otherwise, that is the end of today's lesson.1647

We have you set up with your tools and your environment, and now we will be digging further into the code. 1653

See you next time here at educator.com.1661

Welcome to educator.com. 0000

This site is on Ruby specifics.0001

For this course, we want to go over a lot of the common things you will see in Ruby--a lot of things people see as questions, and a lot of specifics that are just on Ruby itself.0006

First, we are going to go over the Ruby syntax.0022

There are a lot of things about Ruby that are different versus other languages, that you should know.0026

Hopefully, we will cover them here; and the second thing we are going to go over is the common code conventions.0032

The reason is that there are a lot of conventions in Ruby that you wouldn't see in other languages.0038

They are a lot more different, and we will go over it now. 0045

The first thing we want to go over is comments.0051

They are created using the hashtag.0055

If you use other languages, you might see a forward slash--that's in PHP.0058

It is similar in Javascript; but in Ruby, they are hashtags.0067

What happens is, wherever the comment starts, it is going to continue to the end of that line.0073

For example, here on this slide, you have x=100.0082

You have this hashtag here, and then you have the actual comment you leave here.0087

You will notice that anything afterwards, it will pick it up, all the way to the new line over there, and the same over here.0093

The actual Ruby code looks at x=100 and y=200, and then it will use that for its Ruby compilation and whatever process they're parsing.0104

And here, on this site, we are showing you a multiple-line comment. This is one of the most common ways you will be commenting when you are doing multiple lines.0117

I would recommend you use this one here.0131

You will notice, on this line, there are four hashtags.0137

This one is a two-liner, and you will notice that there is a space here, and a space here.0141

That is very common. If you leave a comment, you always need some kind of spacing here.0149

If you have more to this comment, you could just add another hashtag.0155

In this case, if it is a paragraph, I leave one hashtag, and another hashtag here.0160

And then, I put whatever code or code description I have.0167

Here is another multi-line comment.0183

This one is not as popular; you will see it more often in other languages.0189

It has docstring type comment.0194

It starts with this begin tag; that is when it tells Ruby, "OK, if you see this =begin, don't actually do anything with it--don't interpret it; instead, take this as a comment."0197

And you will notice that everything all the way from begin until it says end--that is all a comment.0210

So, the main thing here is =begin and =end.0226

That will create this type of comment; I wouldn't recommend it--it is good to know, though, if you are transitioning from one language to another.0231

For this example, I am going to show you an RDoc comment.0240

The reason we are going over this one is to show you, when you generate your RDoc, you can have headers that are larger and smaller, depending on the topic.0246

If it's a main heading versus a subheading, you will notice here that you have this one equals sign, which is the main heading.0258

For a subheading, you will notice that there are two equals signs.0268

And, you could continue that down--so it becomes a sub-subheading with equal equal.0272

You will notice that I actually have some RDoc comments here.0286

First, you will see, with "Overview," there is =Overview.0290

That is a heading, and then you will see =Usage--there is another heading.0296

Here is ==Options--there is a subheading.0302

And For Future Use--that is another subheading.0307

Let's go ahead and generate this out and see what it looks like.0310

Again, we are in the current directory.0321

There is the hello-world.rb--I'm just going to run rdoc there.0324

It is going to generate it into this Doc directory.0329

I'm going to open the index file.0334

And the one we are looking at is this myfirstclass.0342

So, you can see from this browser, Overview is part of the RDoc--you know that it is a very huge text piece.0346

Usage is our heading.0355

Options is our subheading, and For Future Use is our sub-subheading.0358

You will notice the size difference.0361

Let's minimize that...0367

The next one I want to go over, the second part, is common code conventions.0387

What we want to do here is to go over some of the most popular conventions that you need to know for Ruby, and a lot of things you will notice that are very questionable, that we hope to answer here.0392

Here is a very basic code piece.0406

The class is Book; we have an attribute assessor for title, author, description, content.0411

This is our constructor; initialize...it takes four arguments: title, author, description, content.0418

We create class lists and variables here.0425

We have a method called Summary; it creates a description, and that becomes a summary, because we get the first 150 characters.0428

And then, we have another method called Wordcount, and that counts the words from the content.0438

If you will get it all in...we will go into it in deeper parts in other lessons, too.0444

The first code convention is that you will use two spaces per indent.0451

And you will never use tabs.0458

That is very common. You will notice here that this is actually two spaces.0463

If you did have extra code, where it was a method and then you had an if statement; there would be another two spaces.0475

For every comment you have, you want to use two spaces indentation.0484

And also, you will notice at this class itself, the class is actually CamelCase.0496

But it is only for the class itself.0508

So we have the book--it's Capital--but then you will notice all the other methods here.0512

They are all snake_case, so they are all in this lowercase space format.0518

For an example of CamelCase, we could say, maybe, our next method could be BookTitle.0525

So, this would be an example of CamelCase.0539

But if I am making a method--let's say I make a method for BookTitle--you will never see capital letters; it will all be lowercase.0547

This is snake_case...let's call it that...0555

Let's call it get_title.0567

The next one we want to go over is identifiers.0582

You will use this all the time in coding.0585

They are very basic: you have your normal variables--your variable x, your variable temp--0588

You will also see constants.0596

Ruby convention for constants is that it always begins with a capital letter.0599

The recommended way is to keep it all caps, all the way.0604

So we have PI, all caps, MAX_WITH, all caps--between spaces, you use underscore.0610

And, in that same case, if you have different identifiers that are for different scopes...0624

Global has a dollar sign.0629

Instance variables have the "@" sign.0632

And then, for class variables, you have your "@@".0636

I want to show you an example of this using our Book code.0642

Here is our Book class example.0648

First, notice, we have our two spaces.0655

And then, for our method, we also have two spaces here.0658

You will notice our instance variables and our class...they have the "at" sign.0662

But let's say I wanted to add a class instance variable.0670

We can call it publisher.0679

Let's just call it Educator, LLC.0682

Let's say that I want to add that to my summary, so people will know that this book it published by Educator, LLC.0687

I can add this to my summary.0695

What that would display is...when you call up this summary method, it will show Educator, LLC and your description here.0705

Now, you have different reserved keywords in Ruby.0738

You want to make sure you are not using them in your code.0742

There is alias, and, begin, breakcase, class...def is a method, break is if you are in a loop, begin...and I'm sure you have used those--they are big caps, but more commonly you might use any of these words for a name.0744

Those are reserved, so if you can't find--your code is making a hiccup, or you have some kind of issue, it could be because it's one of these words in your code.0766

The next thing is: parentheses are optional.0778

They are sometimes optional, depending on how you look at it.0785

You do not need parentheses in your methods.0788

Functions do not require parentheses.0792

So, here for our method, search for page--there are no parentheses here.0799

But you will notice, here, we do use parentheses; they are actually the exact same method, but they are optional.0806

Here we have a search for page.0815

This is us calling the method; again, no parentheses.0819

My recommendation is, use parentheses if you have questions about it; once you get more flexible and know what you are doing, then you can remove them.0824

Most people coming from other languages actually enjoy using parentheses, and they are known to keep it that way.0834

So, I would suggest just to go with that tactic at the beginning.0840

And here is another example without parentheses: this is a very popular one--we are going to use puts a lot, and notice we don't need the parentheses here--it makes it easier.0845

For this library, to go over the new lines, these are actually statement terminators.0860

We don't have semicolons in the actual code itself.0870

Usually, in other pieces of code, you have a semicolon here that ends the statement.0874

But, in Ruby, it's common to just have the codeline.0880

The new line itself will actually end it.0885

For example, here you have total=x+y.0891

And you will notice that the x doesn't have any semicolon.0895

So x+y is not going to work at all.0903

Let's do an example with the IRB.0911

So, I do total=x+y, which is what we are going to do.0914

Let's do x=100, y=250.0923

That didn't seem to work. Let's try it again.0929


x=100, y=250, total=x+y.0945

So we get our 350; but you notice if I do total=x, it's already returned 100, and if I put +y, then I get 250, and if I put total itself, it's still going to declare just for the x-value.0955

The next thing is, you can continue code pieces with the period.0980

You would think--we just said--new lines are terminators: they end the statement; but in this case they're not.0985

This period tells it, "Hey, continue looking at the next line."0991

We are calling this array new, but it's also pushing introduction to this array.0998

And then we are calling this period...this period says, "OK, we're not ending it; we're going to continue to the next line."1004

Push your body, push down the array, push summary to array.1010

We have this array here that has introduction, we have our body, and we have summary.1016

And you will notice it's very clean--it never ended--it was actually one stem in itself, and it did the job.1047

For this sample--you can still use semicolons.1058

If you like semicolons, declare them, but get used to not using semicolons.1061

We can use multiple statements on a single line with it.1069

For example--this one is broken up into two lines, but if they were a single line, I would be doing two processes on one line.1075

I recommend you don't use this, though.1082

But there are cases where it is useful.1087

I'll show it to you on this next slide.1092

For example, if we have this code block, notice this is all one line.1100

That might not be exactly the same one I want to show you, though.1109

Let's go back to our Ruby Interpreter.1114

Let's say I'm doing a process, declaring a value to x, a value to y, and then I want to do total=x+y.1117

Notice, I want to show you that even when I enter the semicolon here, it does continue to the next line down.1135

So it hasn't ended the actual statement yet.1144

I can actually do puts total.1147

Notice that was all one line.1151

It outputs that for me.1155

Again, if I were to just do x=100, it's still going, so I can output it here.1158

The next part is code blocks.1173

I recommend you only use the one-liner for this one in cases where you have a single statement.1176

You will notice, for this one, I have a bracket, and a bracket here.1184

But the most recommended way to do it is with this "do" and the "end."1188

The reason is it's just much cleaner; notice, when I use these brackets, I can put it all in one line, which is nice.1196

But, if you have multiple statements, you want to use the do x, your put xx, your other statements, and your end.1205

This one here, I would say, is recommended for multiple lines.1215

And with that, we have gone through our Ruby specifics course.1235

Thank you for being with me here, and I will see you in the next one.1239

Welcome back to educator.com.0000

Today's course is Ruby datatypes.0001

This is the Part 1 lesson.0004

We are going to go over three datatypes today.0006

The first one is numbers, and then strings, and then symbols.0012

First, we are going to look at number.0027

There are a lot of children that inherit from the numeric class.0029

Numbers have a lot of objects that deal with them.0036

First, we have our Float class.0042

It's a child of Number.0046

And then we have this Complex object, BigDecimal, RationalObject, Integer.0049

So we have five objects: Float, Complex, BigDecimal, Rational, and Integer.0062

Integer has two children that inherit from it.0069

We have Fixnum, and then we also have Bignum.0075

All of these number objects are actually instances of Numeric.0081

The first one is Float.0093

One thing that you will notice about Float is that it uses native floating-point representation of whatever platform it's on.0099

The other big on is Integer; that is going to be the most popular one to use.0110

These are just regular digits.0117

Fixnum stores a smaller amount--31 bits.0121

If it's ever an integer that is greater than 31 bits, it is going to be upgraded to a Bignum object.0133

We are going to look at the Integer class now--at Integer literals.0146

This is part of the Integer class, and they represent whole numbers.0149

Here we have a zero--that's an integer--1, 2, 3, and this huge number here--that's all part of the integer class.0160

There are also different types of integers based on the base type.0175

So, we look at binary base-2--you can actually do that with this.0179

Our base-2 object--you want to look at this 0b.0188

When you call that 0b, that knows it's a base value, and it is going to render 375.0194

Now, we look at the Octal class.0205

That's base-8.0209

For example, you call up 0, 5, 6, and 7--it's going to be 375.0215

And then we look at our hexadecimal--our base-16.0220

We have our 0x177--it's going to be 375, too.0229

Let's do an example of that.0236

If I do...0, 3, 175...you have 253...you notice this is part of the Octal.0243

I could also do Binary, so it would be 0000...10--it's two.0259

I could also do hexadecimal--0xa--you would get a ten.0269

So, those are the integer literals.0279

The next thing we want to look at is the floating-point literals.0285

This is part of the Float class.0290

Examples of a Float: this is 0.0--it's a floating-point value--3.14159--notice that both of these have a period there--that's how it knows to make it a Float object.0293

Then we have this E value, which makes it ten cubed.0314

Now, the exponent value can be the letter e or capital E.0320

There is a max constraint to floating-point.0335

I go here, and I put in floatmax--it's actually a pretty hefty number.0341

It might say 1.79, but you have to look at this e--that's e to the power of 300a.0352

So this floating-point value can go quite high.0357

It should handle plenty of your cases.0361

The next thing we are going to look at is strings.0372

Strings are mutable objects: you can change them.0375

They have a lot of operators and methods.0381

There are definitely a lot of things you can do with strings.0384

Inserting and deleting text...you can do searching in there...replacing text...and there are a lot more options.0387

Check out the RDoc.0397

As you see here, it says, "A String object holds and manipulates an arbitrary sequence of bytes--typically representing characters."0406

"String objects may be created using String::new or as literals."0416

"Because of aliasing issues, users of strings should be aware of the methods that modify the contents of a String object."0425

Typically, methods ending in this exclamation mark--or bang value--that means modify their receiver, "while those without the '!' return a new String."0431

"However, there are exceptions, such as String # [] =."0442

Now this "!"--what it will do is modify the existing object, so when you have that "!" value, it is not going to create a new object.0450

Some interesting things here: new--this is our constructor to make a String object; try_convert--this, I believe, is only 1.9.3, but what this does is, if I put in a string in this value, if it allows it to convert that to String, it will be String, but if it doesn't allow it...like this regular expression object...it will just say, "This is nil. I'm not going to allow that in."0459

Let's look at some of the string literals.0499

You have your single-quoted strings--you will notice that it has single quotes in our value.0502

Notice, with this one, we have to put a slash in front, because it's another single quote in a single quote.0517

The most popular string literal you will be using is the double-quoted one.0530

There is a lot more flexibility--you don't have to worry about the slash here--you have your double quotes.0533

This should all be on one single line.0541

If you want to do a new line with that, it will not work0546

You can try it out; we can test it.0552

But remember, the new line is going to terminate this.0554

So, let's open a new one.0562

First, I want to look at the single quotes.0565

You can actually span these lines to multiple lines.0569

I have an example: 'This is my sentence.'0572

Let's say I want to make a paragraph with this.0577

I can use this forward slash here and have that spanned even further.0581

'This is my second sentence.'0588

I can keep going on to more sentences.0600

What I want to show you is that, when I press Return here, this is when it's going to actually end it.0606

Notice here--this is a Return--this forward slash is allowing me to expand this all into one line.0611

There is no end break here.0619

Also, for double-quoted values, let's try the example we just tried.0623

It might cause an issue...but if I do "This is my sentence"...have a double quote...press Return...interesting! Look, it actually puts a quote here!0630

This says, "I know you haven't ended your string yet; continue until you do it."0640

"This is my second sentence."0646

I put my double quote; press Return; and look there! It actually recognized it and put a new line.0649

So it does allow it...that's interesting.0656

Let's look at some of the string literals; we just saw one, actually.0668

For the escape sequences, we just saw the new line.0671

You could also do the tab.0677

It's going to add another space--a tab--in your actual quote.0681

We have our double quote: you notice that all it is, is we are putting this slash in front to make it a double quote, and we also have our backslash.0686

The reason is, if we want to put double quotes in a double-quoted string literal, you have to have that slash...you have to add a backslash to display that.0697

One very fancy thing with Ruby is that they support interpolation in the strings.0711

We're going to go over that now.0721

For example, we have this total=100+150.0722

There is our code--but I want to get this value and print it out.0725

So, look at my next one: I put the total in this hashtag and this curly brace--this is going to allow me to put Ruby code in here.0733

I can put Ruby into these codes.0749

It's smart enough to say, "Hey, this total value is already being stored in this identifier, total, but it has a value of 350"--it's going to know that it is an integer value and make that a string so it can read it.0756

So, the end output will say the total is 350.0773

Again, you could use this with anything.0778

The scope--for example, it worked for globals, instance, class variables...depending on the scope you're on, you can use a dollar sign total...this instance, you can even put..."at" total, or class variable, 0785

I want to do that here, too...total.0813

One very interesting thing about Ruby is that it does allow sprintf.0826

Printf/sprintf are very popular methods that are used in many other languages.0834

It is not something you want to use in Ruby, but it is there; it gives you the flexibility to use it if you want to.0841

It does help to make the transition easier to Ruby.0848

So, we have our sprintf--our string value--here, and we have our percentage, which says this is a Float.0852

And we can just pass in math, the two dots, two dots, and pi, and that will render this value into the Float.0861

Strings also support Unicode escaping.0873

You just have to add this slash, and the u, plus the four-digit hexadecimal digit.0881

In this case, we have 0041; it says "A".0890

But interestingly enough, with the string interpolation, we can actually remove the leading zeros.0896

Here, we didn't even put 0041; it's just forward slash, u, we have our hash, curly brace, and we just put 41.0916

It's smart enough to know that you are using leading zeros there--"I can interpolate that in and render out the same tag--I can render out the A value."0925

Let's go ahead and try it out in our terminal here.0940

For example, we can just make it ourselves, and just use 0024--notice this dollar sign--but I can also use the interpolation, and it will also show a 24 value.0949

Also, you don't need to use quotes; you can also show it using just delimiters.0978

It's very flexible: you can use parentheses, square brackets, curlies, greater than/less than, dash-dash, pipes...0986

Anything...particularly special characters, where you have another one at the end--it will support it.0998

For example, here you have the single-quoted string rule.1003

We also have a double-quoted string rule.1010

Notice, the single quote is in lowercase; the capital Q means it's double-quoted.1014

And another one that follows the double-quoted string rule--that makes it very simple--is this percentage value.1020

Before we do that, I do want to show you some...1036

See if it can go very quickly on that...so, if I say this is a string...you will notice that this is following the single-quoted values.1041

But let's say this is value, and I want to put my token in here, notice it takes it literally?1055

Token is just taken as part of that text.1068

But I can say token=abc, and when I take that exact same one and make it part of the double-quoted rules, it will interpolate it: so it wil say "value is abc."1075

Next, we want to go over the string literals.1091

We are going to look at heredoc.1096

This is used for multiple-line documents.1102

It makes it very easy to show all your text, because you can preserve all the spaces and things that you put between it.1108

To start it off, notice, I use this "less than", "less than"1121

This token tells Ruby, "Hey, this is a heredoc; this is a comment here; I'm not going to worry about how I'm going to parse this out," because once I put this "less than", "less than" here, that is saying this is a string.1132

"So this string--I'm going to store it as a value until I see this here token again."1154

That is where we see here also.1161

So, it says, "Any value that is between these two pieces--I'm going to take that as a string value, and I'm going to store it into this lorem ipsum text."1164

I wouldn't recommend using heredoc; it's very popular in other languages, but it's not a Ruby convention that is really followed.1181

But there are reasons to use it, especially if you have a big text piece--so use it at your own flexibility.1187

Next, we are going to look at public string operators.1195

This one is just concatenation.1201

I have our string: hello, space, hello world.1204

When Ruby sees this, it's going to create a new object that has our hello world here.1210

Now, this object that it creates--it isn't going to append it to hello, to the quote, or world; it's going to create a brand-new object and return that.1219

Now, if you use appending, and use these less than-less than, it will actually store that at the end of the value.1236

This x value appends this quote, and it appends world; you will get the exact same value, hello world, but this is actually part of the x object.1244

Here is another operator string quality: you have your equal equal, and your not equal.1262

Equal equal just checks if two values are the same.1271

Not equal says if it isn't or it is.1274

If I have x=abc, y=abc, x=y, you will notice that it returns "true."1282

Let's say it's not equal: it returns "false."1295

Very popular; very simple to use, too.1299

Also notice, with the x=abc, y=abc, it does return the final object, which is something in Ruby.1302

Next, we want to look at the substrings.1319

We have our value: a=educator.com.1321

If you look at this second one here...we are going to get a substring of it.1327

So we use these square brackets, and we say 0..7; this is a range object, and it's inclusive.1334

What this says is, "Starting from the first value (which is zero), get me the whole value all the way until I get to the seventh index."1357

We have our value 0 here...let me put that here...that is zero, and here is our value 7.1367

It gets from 0 to 7, and it will return "educator."1380

There is another way to do it, and this is with square brackets.1385

This one, with the square brackets, says, "Start at this index."1389

It says, "I'm going to start at index 0."1400

Again, 0 index is the e, and it says, "Give me the eight characters starting from that index."1403

We are getting our eight characters--1, 2, 3, 4, 5, 6, 7, 8--so we have "educator."1426

The last one I want to go over, a substring, is--we can actually go back to the last text to the front.1434

Here we have our a-1.1441

This will actually look at the last value of that string.1446

It starts from the last indexed value.1451

If I were to do a-2, that actually returned the o.1473

Next, we want to talk about string encoding.1489

First, with Ruby 1.8, strings are a sequence of bytes.1491

This makes it tougher to do encoding--it makes it part of a byte schema, and it follows an ASCII character set.1502

Now, in 1.9, strings are actually a true sequence of characters.1525

They also have an encoding method as part of it.1530

I can specify if I want it ISO, UTF-8, or some other encoding.1539

It's not confined to the ASCII character set.1546

That is one reason we are using Ruby 1.9.1556

The last thing we want to go over is symbols.1559

Symbols represent names and strings in the Ruby Interpreter.1564

They are stored in a table that stores names for the classes, methods, and variables.1570

This allows the Interpreter to void most string comparisons.1578

It also makes it very easy for it to display these values onto the screen, and to do a lookup without having to worry about where these objects come from.1584

For example, I have this "symbols"; a symbol is basically...this is the basic one.1600

Another way to declare it is with two dots and a double quote; two dots and a single quote; or the percentage, s, and the parentheses.1611

The basic way is the first one--this is how we would mainly use that.1626

You want to use symbols in cases where the object doesn't require to be mutable.1639

For example, if you have a string versus a symbol, you always want to go with symbols first, because it's more efficient.1648

Another way with symbols and strings--you can actually convert them back and forth.1660

So, I have this symbol here; I can call this method to_s, and that will convert it to a string.1665

Again, with a string, I can use to_sym, and it will convert that to a symbol.1675

Let's look at the RDoc for this Symbol class.1690

What we are looking at here, notice, has some description about Symbol.1701

It says it is "generated using the :name and :"string"," and you have the to_sym method.1712

Now, what is very interesting is, looking at this example here, we have our module One, class Fred.1719

And you will notice that you have this class, a symbol, :Fred, and then you have another global object, f2, that has Fred.1731

And, at the bottom, all of them actually have the same object ID.1745

Think of this versus if I were to create a string called Fred, declare that in f1, then declare a string in f2--Fred--then a string in f3.1750

They would have different object IDs, whereas the syms, they all use the same...it's very much more efficient, and a better use of the way it's stored in Ruby.1766

Welcome back to educator.com.0000

Today's lesson will be on Ruby Gems.0002

What are Ruby Gems? It's a package utility for Ruby--think of it as--you have different packages, like tar, zip, compressed files--well, the package utility for Ruby...that is what Ruby Gems does.0005

It has the exact same feature for that.0024

Let's go ahead and look at RubyGems.org, so we can see more of what they are, and get a good feel of how it is being used.0026

This is the RubyGems.org site.0039

There are tons of downloads here--865 million.0044

There are a lot of different Gems.0050

You can use these Gems in your projects; you can put them into your project and use them for your own codes, so you're not reinventing the wheel.0051

You're using this code for your own benefit, and you can concentrate on the stuff that matters.0063

There are some links here: there's one that says "Learn"; it will teach you about Ruby Gems by clicking that.0069

There is a share button, and there is "Install RubyGems"0078

You can browse the guides--it's explanations, tutorials, references...and Gem specifications.0083

We are going to go over all this stuff; we want to make sure you get used to these RubyGems.0090

This is a very popular thing in Ruby.0095

Here is...to update your Gems...gem update --system...you can build your Gem using gemspec...and you can push your Gem to this website.0098

It does require you signing up for an account, but then you can create your own Gem, and you push it, and it will be out there for everyone to use it.0109

Notice, they also give you a list of what the most-downloaded Gems today were, what was just updated, and some of the newer Gems out there.0116

Before we go any further, make sure, at the top right, you do sign up.0131

Make sure you sign up for the site; then you can keep track of the Gems you've downloaded and the ones you are pushing--uploading.0135

Notice, here are all the "Learn" buttons...we will go through them later, but let's go back to our slide now.0144

Ruby Gems has their own packages for different languages.0161

I put here Java JAR--that is Java's version of Ruby Gems--the JAR function.0168

And UNIX and Linux have the tar utility.0178

These are just a couple of them; the other languages do have their own, also.0182

These are more of the common ones I've seen.0191

So, what is a Gem? Like I said, it's a package--we've already gone over that--and it contains all the necessary files and information to run on this system.0195

In these Gems themselves, they will have version information.0206

It will also have the date it was created.0219

Author is quite common; the description of what the Gem is; and there are more pieces of information, too.0227

What is the use of these Gems?0255

Extend or modify functionality within your Ruby application.0258

Avoid duplication: you don't want to be creating code that someone else has already created; why don't you go with a Ruby Gem...find that piece of code, see if someone has already developed it and just pull that in.0267

You can make their Gem better--be a contributor and help out.0280

Again, stop reinventing the wheel.0284

Fourth, follow Ruby's open source foundation--strong open source foundation--help contribute.0288

Installation: we are going to go over how to actually install Ruby Gems.0303

If you have already looked at our installation lesson, and you have RVM set up, it's already set up; so you can actually skip this and go on to the next lesson.0310

But this is still good information, so if you want, you can look at this, too.0322

Otherwise, you go back to that site that we showed you, download the source--it's going to come as tar/gzip file--uncompress that, go into the directory, and just do ruby setup.rb.0325

That will get you set up with RubyGems.0344

Now, how do you know if you have it installed?0348

First, to update to the latest RubyGems, you just run gem update --system.0352

This gem command will actually be existing on your system after it's installed.0361

So, you could do gem --version, and it will show you the version of it.0366

Here we go: we can run through some of the commands now, too.0371

So, if I do gem -v, it gives me the version, and when I do gem --version, it also comes up; when I do gem in this, I get a lot of usage information about it, too.0395

And this is the RubyGem sophisticated package manager.0410

I see this with gem--and gem help will actually show you the exact same information.0423

And gem --version is just to verify that you installed their correct one.0437

Now, we can have an example of setting up a Gem.0443

One that you should already have it, if you don't already, is the Rake Gem.0448

That's gem install rake.0452

We can go to the site and take a look at that.0457

Right here, in this search box, I'm going to put "rake."0466

It gives me a lot of Gems that have used Rake for their dependencies, and they add features to it, but we just want the main one.0473

I'm going to get the Rake one--Rake

It has the download link, documentations and stats...you can subscribe to follow the Gem and track what progress is being made...0491

It says that the total number of downloads was quite a lot, in the number for this version.0501

You can actually see the Gem following the version.0508

It doesn't really show you anything about how to install it, but it's so easy--it's just gem install rake; it's right there.0515

It's already installed, but we can actually go ahead and try it now; let's see what information it gives us.0526

It should say it has already been set up, but...0532

It's a very simple command--gem install rake--and it will show some information there.0535

While that's running, let's keep going, though.0549

So, verifying the Gems--you can see what Gems are installed with Gem List.0564

We should actually do that; let's take a look at that and make sure it's set up.0572

For this task, look for the Rake and the version number--there's a rake command where we can verify that.0577

Then, just run rake --version.0587

I'm going to quit out of this; it's going kind of slow, but let's check.0595

And there you go--it has already been installed: Rake

Rake --version...there it is...it's already set up; I'll just do rake and see if it gives me some usage information.0607

There you go--if I have my rakefile on here, I can go ahead and run it.0615

That's good...let's see if there's a rake help here...there is a rake help file, too, to show you how it's used.0620

rake...and included the specified, explicit filename with -f.0635

The options are below.0640

The next part is: we want to go over the structure to create your Ruby Gem.0650

It's a very common structure that is used, and it's very standard, so please follow it.0658

If you do add extra common things to it, those will just be additions; so this structure should be followed.0667

First, you also have...whatever Gem you have--the directory that holds everything.0674

It doesn't matter what name you give it.0684

What is really important is this gem.gemspec file, because that is going to be the specifications that everything is set in.0687

First, what happens is all your code is going to go to this Lib directory, which is a subdirectory to Gem.0696

You don't have to specify your file as gem.code; it could be whatever name you give it.0705

As long as your specifications say that file is included, it will include that file.0713

And we are going to go over the specs, so you can take a look at them.0720

Test is for unit testing; it's recommended to use it as you're doing your first one.0724

You're probably still learning it, so...0732

For our uses, we're not going to do any unit tests right now.0739

Readme--very self-explanatory: if I get your Gem, I look at the sourcecode, I want to look at your readme to see how I set it up, a description, installation...any tips or common things I should know.0747

Your rakefile--if there are any tasks that are common that you created to run your Gem--anything I need to load--you can specify in your rakefile.0764

This is also optional.0776

So, this test and this rakefile, and even this readme, these things are all optional, but recommended.0778

And the last one is the gem.gemspec file.0796

Depending on the name you give your Gem, this will change, but whatever name you give it, that should be the name that is placed here and even in your code.0799

OK, to the specifications: What is in the Gem?0819

You can include the name of it, a description of what it is, the version, who made it--include your author information...the developer...0824

And, as you continue to build the specification, you want to continue to update it and update the version for it.0839

Let's go ahead and do that now--let's create our first Gem!--how about that?0848

Let's start with the specifications.0853

I actually want to...get another code base...OK, so we have our structure in a lib directory.0862

The first thing we want to do is create the spec file.0873

gemspec...so the first thing is you want to create your Gem specifications.0878

The first thing I'm going to put is the actual name of this Gem.0897

I'm just going to call it Educator.0900

And then, I'm going to specify the version; for this version, I'm going to start very, very low.0905

Here is the date we are using.0919

Let's just make this like a hello world app--how about that.0937

Authors--for authors you can specify multiple--I'm just going to specify myself, but you can continue to add more to the list.0943

Now, for s.files, this is very important that you include all of your files to this directory.0964

So this one--we're only going to use one file in this.0974

Too big...and the homepage...they will actually give you a place to load your Gems; in this case, it's called Educator.0978

I'm going to show you the path it is being uploaded to, so you can see--you could even download it yourself.0989

OK--we have our Gem specification done.0997

We have more than enough information for it.1000

Really, you need to require the version, author, and the name, because those are needed for the Gem to be generated out.1007

OK, we've saved that; if you look in there, we have our educator.gemspec.1021

Let's go back to the slide now.1029

We have our specification done; next, let's look at the guide to creating our first Gem.1034


OK, I have it here.1050

Well, we already know what a Gem is; we want to make our own now.1054

Introduction...our first gem...required files...executables...tests...documenting your code...1063

Even here, you will notice they are fairly simple.1074

There is structure; they have a .gemspec file; the code is in the lib directory.1079

It even says, reading through this file, "Package is placed within the lib directory"; once your Gem is loaded, you just do require, and it will run it.1087

They have their specifications here.1100

When you have created a gemspec, you can build your Gem and install it.1104

Let's go ahead and try it out.1110

I want to actually go in the directory to create it.1121

Let's go back to the slide first...so we know all the tasks for it--that is our next thing; we need to build it.1130

After we implement all the code--this includes developing the code for it--then we can install it, run it, and publish it.1142

Our main thing will be this build step, where we have to create the code.1159

After that, all these are things that are more simple to do.1167

Let's go to work on our first step to build this Gem.1174

Since we are calling it Educator, it's going to actually be called educator.gemspec.1179

Let's get some code for that now.1196

Make sure we're in the right directory...now let's touch educator.rb, class Educator, and we'll have just a very simple hello world.1200

OK, excellent: so we have our code file; we have our .gemspec.1232

Now, let's try to build it.1238

It says no description was specified; but it still built the Gem.1245

Warning...and we didn't include a description, so let's do that.1253

Let's try it again now...excellent: we don't get the warning.1271

Now, we go to the file itself: Educator-0.0.1.gem.1275

The next thing is, we want to actually install this gem now that we've done the build step.1286

So, what this command does: gem install educator-0.0.1; it's going to install this Gem into my RubyGems.1295

I can actually use this Gem in my code.1305

There you go: it's installed--I have my Gem installed, I have some documentation--I didn't actually set up any, but we've gone through the RDoc lesson, so I'll let you guys do that.1310

So, now I can just go to require 'educator'--it says "true"--then I specify the actual code, and there you go: it prints "hello world."1322

We've built it, installed it, and now we can publish it to the world if we wanted to.1332

We went through this step; gem install rubygem-0.0.1--this version will change, depending on what you put in the .gemspec, and this will also change.1352

Again, to run it, since it's already in your Gem directory, you can require it--as simple as that: just putting the name of it in.1371

The next step would be, "Hey, I have this great code piece; I want to publish it to the world! I'm going to help other people use it; I already developed it--why don't I help others and maybe, if I have some feature I would like to have with it, but I don't have time, maybe someone else could work on it, too!"1385

This step is to publish it.1403

To do this, you would sign up on RubyGems.org, and then they have a command to help you publish it.1407

It's back on their site...it's exactly like the slide--you run this curl command, it goes to this URL--you will specify a username when you sign up for your account, and now it's going to ask you for that password that you created with the user.1417

Once you do that, you just run gem push, and it will actually push it to the website, and your Gem will be up.1448

Notice here, you are doing this curl command; change this to your name; and then this URL will stay the same, and you just put in this and the curl with the Gem; you put in your credentials, and you put your password here.1456

Then, all you have to do is just run this gem push, and it will push the Gem to RubyGems.org, and you will see it by the message: it will say, "Successfully registered"--your Ruby Gem is online now.1481

Other than that, I would say just start looking at those Gems and downloading some.1505

We've looked at Rake; another popular one is the Rails Gem; FasterCSV is a Gem you can use to parse CSV files, and it's quite popular, and it does the job really well.1512

Then this one called Koala will allow you to interact with the Facebook API and start using Open Graph.1531

So, that is the lesson today at Educator.com.1542

You just learned RubyGems.1545

Join us again for the next lesson!1548

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

Welcome back to Educator.com.0000

Today's lesson is on objects.0002

Objects are a pure object-oriented language.0011

Everything in Ruby is an object, as we've gone through in other lessons.0018

All values are objects, and there is no distinction between primitive types and object types.0029

All objects inherit from an object.0041

The reason on this third part--we say that there is no distinction between primitive types and object types--if you come from other languages, and you are looking at int values or even Booleans, usually they have a primitive type for that.0047

But, in Ruby, you know that you already have your TrueClass; you have your FalseClass; you have your Integer class, your Boolean class...so everything is still just an object.0061

For this slide, we are going to talk about object references.0096

Whenever you are coding in Ruby, you are not actually making code for an object.0101

You are actually making and developing the code for the object reference.0107

Every time you are programming, you are actually using a reference of the code; you are instancing the code, creating a constructor, and you are working with the reference of it.0112

Especially if we are working with the already-built-in Ruby core...0125

That is why, here, I say this is what we work with.0132

When we work with the Ruby core--any of the Ruby core libraries--we are working with references; we're not creating the actual classes for this; it has already been created.0137

We are just using those classes to do our development in our code.0149

So, we just do the updates, the modifications, and the creations for those references.0155

We have an example here.0164

What we are going to do is: First, we are going to create array.new.0166

It is going to be stored as an Array object entitled Fruits.0176

Here, what we are doing is creating another value, called Foods, that equals Fruits.0182

This is going to copy the reference to Foods.0189

Here, on the next line, we do a foods.push(apple), so it stores 'apple' into the Foods Array.0196

Then, you do a .push(cherries), which also pushes it into that Foods Array.0205

Take a look, now: we have our Array with 'apple' and 'cherries'.0209

Now, if you print that Array out, the Food and Fruits will have the same Array.0228

But, if you create a new Array with Foods, you will notice, if you print Foods and Fruits, that they are actually different references now.0238

One is pointing to an extension .new Array object, and the other one still has this 'apple' and 'cherries'.0248

Let's see it in action so you have a better idea.0257

What I am going to do here is just to launch the IRB.0266

Let's go ahead and use that first example.0270

We have our empty Array of Fruits; now, I'm going to point Foods to Fruits, so we have a reference--again, an empty Array; now let's go ahead and push those elements there.0275

Notice, we are chaining the elements together, so we're actually doing two statements at once.0292

Now, you see, if we do Foods, there are two elements in the Array.0302

We can compare--Foods, Fruits--and they both have the same elements.0309

But if, on the Foods, I create a new Array, now it has a different reference.0315

So, when I do the print, right there, you will notice that the Foods is empty, and the Fruits still has the two elements.0322

This one here--the array.new--this is just the empty Array; that is what it points to now.0345

Now, as you just saw, when we create a new object, we use the constructor.0360

That is what we are going to talk about next: this .new method.0366

We can create new objects, and we are working with a reference, because we are not actually building the object; we are working with references every time we're programming.0370

Here, we are going to allocate memory for the new object.0380

Then, we invoke the initialize method.0386

When you call that new, there is no new method in the class: so it is actually using a class called Initialize.0390

So, for this myfirstclass.new, this one is going to look at your method initialize.0399

Let's see a live example of that in action.0422

This goes into the myfirstclass, and I get it substantiated with new; and notice that there are zero arguments here.0426

I'm going to exit here, and I'm going to go to an .rb file called myfirstclass.0438

We are going to create that class and show you a live example of the initialize statement.0451

I'm CamelCaseing it here...here is my Initialize...and we'll just call it Value...and let's do something to print out what that value is.0460

We have our class, Myfirstclass--when you call that new method, it is going to pass the value into Initialize.0483

Notice, we are requiring a parameter here now.0496

I can just do IRB...I'm going to require that class.0504

Let's go run the example.0513

Now, I'm going to show you something: notice, it says argument...wrong number of arguments is 0 for 1.0518

The reason for that is, in my constructor, I actually specify "I need to put one argument in there."0527

Let's do that again.0534

I'm just going to put in a value--I'll put FooBar in.0540

Next, I can call my other method that returns it--and it prints out FooBar.0547

That is an example of the .new method in action.0559

Notice, for this example, we had a parameter val.0568

Over here, we passed in that FooBar method.0578

Next thing--we will just go really quickly about this--there is garbage collection in Ruby.0592

When an object is no longer needed, it will automatically be destroyed.0598

"No longer needed" equals it's no longer reachable.0607

If there is no way to access that object reference anymore, it's going to go away--it's going to be garbage-collected--if there is no way to access it.0611

That is why--so you have to be careful with this: global values will always be reachable if the Ruby Interpreter is running.0621

If it's a global value, it's always reachable.0629

You would have to release it--release that value, the reference it's pointing to, to get rid of it.0632

Otherwise, it is always going to stay there.0639

So, with global values, I would recommend you be careful.0641

You don't want to end up having a bunch of different values that are running globally and using up all your memory, so definitely be careful with global values.0652

The next thing we are going to talk about is object identity.0666

Every object has an object identifier; what we use for that is this object_id method.0670

This keeps every single object unique for the lifetime of the program.0680

Usually, when you call this array.new, this object_id, you will get some huge number afterwards that is the object ID; so, if you call array.new, object_id, you will get a big number for it.0691

Let's show you an example here.0705

Let's just try a bunch of different things; I'll do string.new, array.new, and let's do even another string.new.0713

We have all of these...three objects here; now we just call a.object_id; notice, we get some ID value.0727

As I get the object ID--same for the Array; notice the value is different; and, for the C, the string is actually the same--it's an empty string--but you will notice that it also has a unique object identifier.0738

As you see, every object ID here is constant and unique for the lifetime of the program.0765

The next thing we want to talk about is object class.0779

The class method will determine the class of the object.0785

By taking any object, you can specify this class method, and it will tell you what object this belongs to.0791

You can also use the superclass method, and it will determine the parent class.0801

Here is the example we have: we have this string; we are going to allocate a new string object with the value Newstring.0810

After we allocate it, we call this object method class, and it will get us this value String.0821

Also, notice that we can call superclass even to Object.0832

Object has its own parent class, too.0836

Object' superclass, its parent, is BasicObject.0843

Now, if you call this BasicObject's superclass, there is no object above it.0850

If I were to take basicobject.superclass, it would just return nil.0856

Next, we want to go on through object testing.0884

The first thing we will use in object testing is the instance_of method.0890

The instance_of method checks the class of the object.0897

You will notice it here: we use our same string, Newstring, and I can say, "Is this string an instance of String?"0903

In this case, it is, so it will say true.0915

Again, you could also have used parentheses here--I didn't use them.0919

There is another method we could do to test what the object is.0923

This is the is_a method.0929

The is_a method tests the class and its superclasses.0931

Remember, string is part of the String class, but the String class also belongs to the Object class.0938

If I call string.is_a?(object), because that is one of the parent classes to it, it returns true.0953

Also, notice this === method also is true.0962

If I do string.is_a?(string), it will return true; if I do string.is_a?(basicobject), it will also return true.0970

The next method we are going to look at is the respond_to method.0985

This method allows you to test if you can invoke a method.0992

This might be useful if I'm coding something and I'm agnostic to what the object is.0997

Some of them might have this method; some might not.1007

By using this respond_to, I can check, "Hey, does it have this method?" before I start processing it and calling this method.1010

For example, let's say I have an Array, and I want to clear that Array: so I want to say, "Hey, does this respond to the clear method?", and this one says it's true, so it does.1017

There are also other methods that have it; you will see it in the Hashes, too--Hashes have a clear method.1030

You can do the exact same thing: if I have a Hash, let's say I do hash.new(h), I can just do h.respond_to?, and I can pass in the clear, and see if it does have it or not.1043

The next object testing we are going to look at is the equal tilde.1074

The equal tilde, by default, returns false.1083

But you will see that there are some Ruby class objects that do use it.1087

The string and the regular expression use it for pattern matching.1091

So, if we have this value, abc123, I'm saying, "Hey, I want to do a regular expression, and tell me, in the string itself, does it contain a digit?"1099

I'm trying to match a digit in this one.1113

You will notice that, to match a digit, I'm using this whole expression here.1124

That is going to return me 4.1134

4 is saying where in this string index it found the first digit, and that is what it returns me.1137

If you start at 4, a is 0,1,2,3,4...so this value, 1, is in the fourth index place.1145

This is actually the index value.1157

If you are creating your own classes, you can actually define this equal tilde to what is needed for your class.1166

Again, you should use it for some type of similarity matching.1180

If, for regular expressions, you want to look for some kind of object, and match something, kind of like a needle in a haystack, this is what this equal tilde would be useful for.1185

The next one we are looking at is this less than, equal, greater than value.1202

What this value does is, it is used for comparing two object instances of the same class.1209

That means, if you have two instances, you compare equal, less than the other, or greater than the other.1218

If it's equal, it's going to just give you a 0.1229

If it's less than the other value--let's say, for example, I have a value on the left side that is less than the value on the right side--so let's say, for example, a is less than b; if a is less than b, it is going to turn back a 1.1237

If a is greater than b, it will return a -1.1265

So, if it's a negative value, the negative value says the left value is bigger; if it's a positive number, it's saying the right value is bigger.1282

A negative value...negative equals left value is greater.1296

And a positive value...again, this is -1, and this is a 1...this means the right value is greater.1313

And if they are equal, it will just be zero.1334

Let's go through a few examples of this.1338

Here, we have the value 1, and we are comparing it with another object, the value 2.1343

This is going to return back 1--why does it return back 1?--because this is a bigger value than the one on the left, so the right one wins in this case.1351

This value...we have a value 5 at the beginning, and we're comparing it to value 1; notice, it is -1: the one on the left wins.1365

Here, we have a value where we are comparing 5 with 5; it's the same, so it's 0; so it's equal.1376

It is equal in this case.1384

We can go through a live example of this, too.1391

Let me show you, so we can get a better feel for it.1401

We can use the IRB again.1407

Let's go ahead and just use our string.new; what I am going to show you is an example with the string object.1411

Because it uses alphabetical ordering to define which is greater...1425

First, let me show you some of the examples that we went through for these slides.1432

First, if I do instance_of, pass in string, you will see it returns true.1438

Then, let's do the respond_to method, and it responds true too, so string also has a clear method.1446

Now, let's go ahead and show you the string: so, we have our mynewstring, and I'm going to use that comparison value with the letter a.1457

Notice that it returns the value 1 for this case.1467

Now, let's do that again, but this time with a z.1474

This time, it returns a -1; so, in the first case, the left side is mynewstring, and in the next case, we used the z, and it returns -1.1479

Let's do something else: what if we just pass a with z...let's see what we get.1501

For this example, the first one is winning with the letter a, which is interesting, because you would think that the later letter would win.1509

If I pass another one, if I pass more letters to that, it still says the first one is winning, just because it only looks at the first letter.1522

That's that comparison.1541

Now let's look at object equality.1546

There are many ways to compare objects.1550

This way...we are going to look at the equal method.1555

What this does is, it checks if they refer to the same object.1561

You might get confused...it uses equal for other reasons...but just use it for checking the object; don't use it to check the values in these objects, if those are identical...this just checks if object a has the same object ID as object b.1567

Look at this example: we have a=100; then we have b=100; these both have the same value, but they are different objects.1583

Notice, when we call this a.equal? b, it returns false.1615

But look, if I make a new value called c=b, it is looking at the same object reference; so c.equal? b will return true.1621

The next object equality method we are going to look at is ==.1636

We are also going to be looking at this not equal, which is the opposite of it.1643

Again, let's use our example: a=100, b=100; a==b does equal true in this case; it's looking at the strings ==method, and it says, "Hey, this value inside--that is the one I want you to check for ==," so it returns true.1648

And not equal is just the exact opposite, so it's not true--it will be false in this case.1674

a!=b will return true, though, if I took a value like...100 is not equal to 101.1680

I do 100!=101; this would return true.1694

Let's look at object equality for Arrays.1710

We are going to look at an example where they have the same number of elements and the elements are equal.1715

Both of them have 3 elements in A and B, and they have the exact same values that match up.1725

For this case, notice, when we call a==b, it returns true because it matches both these cases here.1736

We have three of them on both, and these three values are the same.1745

That is how the Array uses the == method.1758

But if you call this object_id, not equal to object_id, this will also return true, since they are different objects.1762

The next object equality method we are going to look at is the Hash.1780

The Hash--what it does for equality is, it checks if they have the same number of key-value pairs, and the second thing it looks for is that the keys and values are all equal.1785

Notice that this value is the same--1,1,2,2,3,3, for the key, and the value is 2,3 here.1804

So, a.equals? b is true.1816

But let's say I were to make a change: instead of 3 here, I change this to 4--I update it like that.1819

Now, if I call a.equals? b, it would return false.1832

The next equality method we are looking at is ===.1847

If this hasn't been defined, it's going to default to just ==.1856

If it hasn't been defined, you should define it on your own, but if it isn't, it is going to just say, "Hey, you have an == method; that is the one I am going to use."1867

How do objects utilize it? The class tests if an object is an instance of that class.1875

So, for that example here, look at this string--we have this string class, and it's going to say === abc.1883

Now, abc is an instance of that class, so it will return true for that case.1895

If we look at this example, the Range tests if it belongs in that Range.1904

Again, this is an ===.1910

Since, out of this range from 1 to 100, does that 50 belong in that Range of 1 to 100? It does, so this will also return true.1918

Let's check out how symbols utilize it.1941

The symbol tests if that symbol is the same--and if that is, it's true--or if it's a string with the same text.1944

We already know that if I do Educator === to the same symbol, Educator, it's going to be equal.1955

If I even took a symbol a===a, that would be true.1965

But, in this case, I actually tell it, "Hey, if it has a string with the same text as a symbol, that also is going to return true."1976

This also returns true in that case.1986

For this next part, we are going to look at object conversion.1991

Ruby allows objects to be converted to a different class.1999

It has two ways of doing this.2005

One way is with this explicit conversion, and another way is through implicit conversion.2008

Explicit conversion is something you kind of force it into, whereas implicit is kind of more implied.2016

Let's see it in action so it will make more sense.2025

First, we are going to look at this explicit conversion.2029

Like I said, it is used to force a conversion to another representation.2033

Some methods that do this...you have this to_s that converts it to a string, to_i converts it to an integer, to_f to a float...2039

Let's say I take a value, 5--I can do to_f, and that will convert it to 5.00.2051

to_a will convert to an Array, to_c converts it to a complex value, and to_r converts it to a rational value.2065

You can try this with many of the popular ones--Arrays, Hashes, Strings, Integer values--and you will notice that some have these conversion methods already in them that allow you to do it; others don't.2076

Here is the other way: you can use implicit conversion.2096

Implicit conversion is used more for comparisons and conversions.2100

These are other methods that have to be defined to do implicit conversion.2107

You have your to_array, your to_str, your to_int, and your to_hash.2116

Let's look at an example: what we're going to look at is this class called FooBar.2130

We have both examples: we have our explicit conversion method, and then we have our implicit conversion method.2138

Let's open our terminal so we can take a look at this.2163

Let's see if I have the object here...I do not, so let's go back and take a look here...2178

We're going to call FooBar...one is lowercase; the other is uppercase.2196

I'm actually going to create this class in the IRB.2207

I'm going to make the class foobar...my explicit one will be lowercase...and I'm going to make my to_str in uppercase.2210

OK, so I have my foobar object here.2224

First, let's substantiate it with f=foobar.new.2229

Let's show an example of these two cases.2237

For the example one, I'm just going to put value f, and it returns the explicit version.2241

What happens here...it's doing interpolation on the f, so it calls the to_str, this value foobar.2256

But another way is--watch--if I do value, and I do +f; it's all caps this time.2264

This is using the implicit conversion.2275

So, depending on which way you code, you will notice that it's not using the same method.2278

When you are creating your objects, you have to think about this and develop for these reasons.2286

That is conversion.2298

Now, let's look at the Kernel module.2303

The Kernel module allows you to do object conversion, also.2307

There are some methods that allow it.2312

First, you have your Array, and this allows you to do conversions to_ary and to_a.2318

For this, it will convert to_ary or it will convert to_a, depending on the circumstances.2332

If it's to_ary, but let's say it's not defined or it returns nil, then it will use to_a.2344

This will be used if it's not defined or it returns nil.2355

Now, for the Float, this converts the argument to Fixnum or Bignum.2365

Number types are converted directly.2374

You will notice that these are all actually functions that are built in the Kernel module.2378

Then you have this Integer, which will convert arguments to Fixnum or Bignum, and number types are converted directly.2389

We also have a String function that will convert the argument to a string by calling the to_s method.2401

Notice how these functions will do conversions, but they use different methods to do them.2410

The Array one uses to_ary and to_a, but the String uses just to_s to do that; versus the Integer, that will take your value and convert it to Fixnum or Bignum, depending on what is passed in.2417

Let's see it in action.2430

First, you can use these in any order; these are global conversion methods.2434

First, let's look at Array: if I pass Array, the value 5, it creates an Array just with that value; so if you pass this Array function, a value, it's just going to creat a new Array with that value in it.2440

Let's try Float...if I pass 5 into Float, though, notice, it takes this value that is an Integer, but it actually converts it to a Float using the to_f function...so if I do 5.to_f, it does the same thing.2456

If I pass Integer, the value 5, it's just going to call it by itself; it's already an Integer.2478

And if I just do String, it's just going to return the string value for that, also.2490

Again, this is just using the to_s, so it's just creating a new object of itself.2498

The next thing we are going to look at is object conversion using coerce.2518

This is used for mixed-type numeric operations.2524

It is intended to find a common type between two operants.2531

You will see here, what we have is 1.2--we have a Float value here.2540

And 3--we have our Integer.2551

Notice, what it does is, when I pass this Float, and I say, "Hey, I'm going to call this method coerce, and I'm going to pass you this argument 3," I want this value 3 to have a compatible type with the Float object.2559

It's going to say, "Hey, with this Float object with the coerce, make this 3 so it's friendly, so I can work with it."2577

What it returns is the Float value of 3, which is 3.0.2584

Here is our original value, 1.2, that called the coerce object.2592

Now, this is also a Float here.2604

Let's go through an example so you can get a better idea...2610

Let's see...I thought I created some code here, but let's go ahead and create a new piece called coerce.rb.2621

Actually, no, I do have it here.2635

I have a file called point.rb, so what we do here is, I have this object called Point, notice the constructor: I have this Initialize takesone value; it initializes that to a point in this class.2639

There is another method called coerce; other is the value that gets passed in, so I'm saying, with this method I'm creating, it can take one value--which is the way you should be defining coerce.2660

Then, I have this case statement; it says, "OK, with this value other, when it's a numeric value, I want to convert that to a Float."2681

What it will do is, it's going to return an Array, with a Float and the initial value, which is that point.2696

This will return a converted numeric value, which will become a Float, and you get your initial value, which is that point.2708

So, this will look, actually, like this; else, it's going to say, "Hey, I'm going to throw an error, because I can't handle this type."2727

My coerce method only allows me to work with this Numeric type, so I'm going to just say, "Else, erase the type error; tell it 'This can't be coerced into this other that class.'" This other that class is whatever class is in there.2741

In the future, I could use this point value and say, if it's a string, maybe I want to go ahead and try to convert that...to coerce it into my point value.2760

But I'm not there yet, so I'm only handling the numeric case.2772

Let's go ahead and try it out.2777

I'm going to require my Point class, so I have my Point object now, and I'm going to just work with 2, and I'm going to say...I need to substantiate point.new...I'm going to pass 5 in here, and then I'm going to do 2+p.2780

Notice how, even though I have this Integer value, 5, I do 2+p, and it converts it to 7.0.2810

It's using that coerce method that I defined to convert that to a Float, so what it's doing is, that p is becoming 2+5.0.2822

Since that is a Float, this is another step that Ruby is doing; it's changing that 2 into a Float, too.2835

It's saying 2.0 and 5.0.2843

So, that's coerce; you can see a little different trick of how it is being used there.2855

The next object conversion we are going to look at is Boolean.2860

We know True and False are distinct objects.2865

Both do not have a conversion method, though.2872

Object conversion...but both of them don't have a conversion method...what am I talking about? I'll get to that.2878

However, every object does have a Boolean value.2883

If I have this object that is not false, nor nil, it is treated as true.2888

You could think, whatever object I'm using, if it's not false or nil, it's going to be true.2896

If I use this method here, if I am not nil--which is, if I even pass in the value 0, it's going to print my value; if I pass you a string with a, 0, 1, these all will print this value.2906

Let me show you a better example, though.2928

Let's go back to our IRB.2930

What I'm going to do here is, I'm going to create a method called whatbooleanvalueami.2935

I'm going to check both--if the object is equal to nil, or if object is equal to false, I want it to return the False string.2945

Otherwise, I want it to return the True string.2958

Let's try some different values for this, now.2966

Whatbooleanvalueami...so I pass a 0...oh, I forgot to define the argument, so let's start again.2969

I'm going to put this object...it's pulling that up...0...the number 0 returns true; 1 also returns true; strings return true; but if I return nil, it's false, and false also returns false.2982

I can try...math, pi here; that also returns true; so if I take any of these objects--let's say I'm making my if statement--and I'm going to say 'hello world', and if I run if0, it will print 'hello world' with that.3015

The next thing we're going to go over is object copying.3047

To do this, we have two methods.3051

You have this dup method, which copies tainted objects--only tainted objects.3054

And then we have this clone method, which is able to copy both frozen and tainted objects.3061

It copies singleton methods, and it is meant to be overridden to do deep copies.3069

Both the dupe and the clone create shallow copies by default.3076

The reason I say, in the clone, that it is meant to be overridden to do deep copies--dup usually does a level 1 copy, but clone--you can get even deeper by how you define it.3081

Let's go back to our example.3098

Let's see...let me show you some in action.3104

Let's say I have my Array, two elements, and I create a new one using the dup method.3110

Now, when you print x and y, notice they both are the same; they both have the same values.3124

Let's do this now: let's put in a value, 3, into x.3131

Now let's check out our y value.3137

Notice the y value still doesn't have the value 3 in it; it's doing a level 1 array copy with this dup, and it's a shallow copy, so 3 is not in there--but if you look at the x value, 3 is in there.3141

Let's do an example of how we can define this with a clone, because by default, clone is also going to do a shallow copy, but let's instead define the object to do that, and then we can make a deep copy of that.3162

To do that, I have an example that we can use: this is basket.rb example.3180

This class has a constructor with fruits and vegetables.3188

It uses the attributes accessor to define @fruits and @vegetables.3198

But what's interesting is, I have this clone method here.3207

It will tell basket.new that I am going to do a deep clone copy with self.fruits.clone and self.vegetables.clone.3210

Before we do that, I am going to put here 'deep clone copy', but let's not even define that at the beginning; let's just try it without it.3226

I also have this method called show_all; it will print all the different fruits and vegetables that are in this basket.3238

First, I am not going to do the clone method; let's see how that works.3249

If I do this basket.rb...and I create my new basket.new...I'm going to define two Arrays, fruits and vegetables, so I have my fruits, and I'm going to do my vegetables...3255

And then I'm going to create a new one using this clone method, so I have a and b.3279

Now, I'm going to call this show_all--we have our fruits and vegetables--and then I'm going to do the same with our new value--with a.3285

They are both the same.3294

Plus, if I do object_id, they are also unique; they are both different objects.3296

What is interesting, though, is: if you do this vegetables.object_id, and then the b one, notice they have the same value to it.3310

So, if I pass into the value b...I'm going to add another vegetable into here...I'm going to add some broccoli...so now, if I do show_all, notice a actually has the vegetable 'broccoli', too.3324

And, if I do b, it also has the same value.3344

It's because they are using the same object, because it's a shallow copy.3348

Let's do it again, but this time, we're going to define the value.3351

What I'm going to do here is...here I've added it back in, and we are going to do the exact same thing here.3359

Apples...carrots...and let me define that as b again...oops, I forgot to require my class...now, I'm going to do our deep clone copy.3375

And again, I'm going to do my vegetables--I'm going to put some broccoli in there.3399

If I do b.show_all--and let's also check out the object_id--and now I'm going to do a.show_all...notice, the 'vegetables' in a doesn't contain broccoli.3406

But also notice that the object ID has also changed.3421

Here is the example with deep clone that we have done.3430

Deep clone method...so you see, by default, both of them are doing the shallow copies, but clone--we have overridden it to do deep copies.3434

The next one we will look at is object freezing.3453

Frozen objects are immutable; you can't change them.3457

The internal states cannot be changed, and any mutator methods you use will fail.3463

If you try to change it, it will not work.3474

Here is an example: we have our fruits example: we have array[apple, banana, grapes].3475

I say to the Array that I want to call my freeze method; next, I ask it, using this method called frozen?, so it says, "Hey, is it frozen?"--in this case, it says true, because I called freeze beforehand.3483

Now, I'm going to try to use a mutator method; I'm going to add pear into it.3500

But my result will say, "This is a runtime error; you can't modify a frozen Array."3506

The next one we are going to look at is object marshalling.3518

There are two different states when you do marshalling.3523

There is a save state and a load state.3526

With the save state, you call marshal.dump and your object.3531

That will serialize the object and all descendant objects, and it is going to save that state--serialized.3538

Then, you can take that returned object and you can call on it marshal.load, and that will return that serialized data back into your Ruby object.3550

Let's see an example of that in action.3565

Again, I am going to run my IRB.3570

I have a function I want to show you that I have been using--I mean a class: this class called MyWord: it has one constructor called Initialize; it takes one value, word, and the word variable gets passed.3578

I have another method called shout; it's going to take that word and shout it with an exclamation point.3600

I'm going to require that in my method, and I'm going to substantiate that with hello, so I have my MyWord object.3608

And now, I'm going to do a dump; so it's going to serialize this.3627

I'm going to put data=marshal.dump; it's going to get the whole object and serialize it; notice, it knows we have that here.3632

I'm going to take that data and load it.3646

I'm going to do obj=marshal.load(data), and there we go: we have our object again.3650

Let's check if they are the same ID, though--so we'll have this o.object_id...856510...and then our new serialized one...they are actually different object IDs.3660

They aren't the same; that is interesting.3675

Let's go ahead and try to shout it; it says 'hello!', and then I use the o, and it also has the same value.3678

So, it's not the same object, but the element, the value, is the same inside it.3689

That is object marshalling.3703

Next, we are going to look at tainted objects.3707

This uses the method taint; it marks the object as tainted, and it's used for untrusted user input or an unreliable source.3711

What is an unreliable source? Maybe it came from a form-fill off the Internet.3725

This is some value that wasn't in our system before; it came from an external source that...we don't know where it came from; it could have been anyone.3736

We could say this is a tainted value.3749

For example, let's say we have something that looks like user input...so I want to pass in this value: lorem ipsum 123.3752

Now, I don't know who that source is, so I'm going to mark it as 'taint'.3761

That is going to say that the object is tainted.3767

Later in my code, let's say that I have this process that says, "OK, is this object tainted?"--it says it's true: yes, it is.3770

When I know it's true, then I can run other validations.3779

I can make another...I can put my code here and run validations if it's tainted.3782

What you would want to do, usually, is if it's tainted, you can run some type of validations--else, if it isn't, you can do something else.3789

I run my validations; let's say they all return true--everything is valid, there is nothing bad about it--then I can call this userinput.untaint, which will mark it as untainted again.3813

Then, let's say--it's not tainted anymore--I want to do another check; I can check, "Is it tainted?", and if it isn't, I can continue to the next process.3829

The last one we are going to look at is untrusted objects.3844

This is new in Ruby 1.9.3849

First, you have this trust method; this marks the object as trusted because it came from a trusted or untrusted source.3852

Then, you have another method called untrust.3866

If it's trusted, you can clear it up with untrust; and then you have this untrusted, which is more like a reader attribute for that.3874

Now, all these...this whole trust, untrust, trusted...these are all part of the security model for Ruby.3884

It's still fairly now, and you can use this to...how necessary, but if you mark it as trusted or untrusted, Ruby might have some library set say, "Hey, if this is an untrusted source, maybe I don't want to run the code this way."3896

I recommend you look more into the Ruby security model to see how it will fit in your code, if you...I recommend to use this.3915

Otherwise...we have gone through a lot of different methods in Object; we have gone through equality, conversion, coercion...3927

That is the end of this course.3939

I hope to see you next time at Educator.com.3943

Welcome back to Educator.com.0000

Today's lesson is on Ruby loops.0001

Ruby has quite an extensive collection of loops.0009

When you are working with Ruby, there are many different ways you can do your coding--it's very flexible--and in the same sense, there are many different types of loops you can be using.0016

That's why I say, in this next one: You have the flexibility to choose the one that is best for you.0032

We are going to go over the four different types.0039

The first two are real loops: the while and the until loop.0046

Next, we will look at the for...in loop, and then we will look at more loops that you will see in objects.0053

We are going to do this through iterators and enumerables and objects.0062

The first one we are looking at is the while loop.0071

The while loop has code that executes while a certain condition is true.0075

This while loop will keep going if this condition holds true.0086

For this example, we have the items=0, and we have a loop that says, "While the items is less than 10, do the things in this code block."0090

Here is the code block.0107

It says, "I want you to print out the number of items that it puts"--so that is a new line--and then it says, "Increment the number of items by 1."0114

When this first gets in this loop, it is going to print the value 0, it's going to increment items, and then it's going to go back to this check that says, "Items less than ten?"--and if it is, then it's going to say, "OK, we're going to go back in the loop."0125

So, it's going to say 0,1,2,3,4,5,6, all the way down; the question is, when it hits this mark here: at the 9 value, it's going to print that out.0144

But the last one is going to increment to 10, and that is going to hold false.0162

So that is where it is going to stop.0167

It's going to print 0 to 9.0169

The next one we will look at is the until loop.0176

The until loop says, "Until this condition becomes true..." so that means, while it's not true, it's going to continue doing it.0179

It's interesting, because most other languages, you won't see an until loop; so this is kind of a special one that is in Ruby.0193

Here we have items=0, and this loop says, "Until items is greater than or equal to ten, continue doing these things."0203

It says, in this case, that I want to print out the items and then increment the items by 1.0212

This is just like the while loop, but we are using the until loop for this.0220

Notice also, our check here has changed, so this is going to print out the value 0, print out the value 1, 2...and once this items equals 10, it is going to stop.0226

Again, it's going to go all the way down to value 9, and it's going to have the exact same code as the while loop, but we are using the until loop for this case.0243

The next thing we're going to look at is single expression loops.0258

This is a very compact form--usually you see single expression loops as a kind of one-liner loop.0262

It is expressed as a modifier.0275

When I say "modifier"--you have your while here--and for example, let's say x=0.0281

What I'm going to say here is, "I want you to print out x=x+1 while x is less than 5."0292

Notice, it's all in one line; so, it's going to continue doing this while x is less than 5, and then once it hits this condition, then that is going to end that line.0307

Notice, the end result is x=5.0322

Again, I'm going to do that with an until loop--I'm going to use y=10, and it says, "Until y=1, continue to decrement that y value 1 by 1."0326

Once it hits 0, it will hit this condition, and the until loop will say, "I'm going to stop now"--when this y is equal to 0.0338

The next one we're going to look at is the do while loop.0351

What this does is...first, the body will execute: the body executes before it checks the condition.0354

The second one...it is not followed as a Ruby convention...there really isn't a do while loop in Ruby, so we are kind of using that flexibility to create our own.0364

It's useful if you are adjusting to Ruby from another language, which...many people use do while loops in Java, C, C++...so it might be common to use it this way, too, because it's more familiar to you.0376

So, for example, we have this x=10; we have this begin loop that says, "Hey, I'm going to take this x value and decrement it by 1."0389

It's going to end, because it's also going to say, "While x > 10"...so you notice, for this one, the end result is 9.0399

What happens is, you get this value 10, and it gets decremented to 9, and then it goes in this while loop, and it says, "OK, 9 is not greater than 10, so I'm going to stop there."0413

Another interesting thing you can do is use parentheses.0426

With parentheses, the condition actually executes before the body.0431

Let's take that exact same example.0436

If I take x=10...I have my parentheses here; inside, there is a block that will decrement the value of x to 9.0439

It goes down by 1; but my condition is "while x > 10".0451

Now, since, with these parentheses, the condition executes before the body, the end result will still be x=10.0455

It never actually runs the block in there.0463

The next one we are going to look at is the do until loop.0470

This is very similar to the do while loop.0474

For this one, we are going to make a countdown; once it hits 0, it goes "Boom!"0477

So, I have my countdown=100; again, I'm using the begin/end; this is my block of code; and it's going to print 100, decrement it by 1, and check this condition.0483

This will have the value 100, all the way to 1, and then it's going to say, "Boom!"0505

Notice, here it says, "Once the countdown=0, don't run that code again," so it does not print out 00518

1 is the last one, and then it prints "Boom!" there.0525

Let's look at using the break inside the loops.0530

This keyword 'break' allows you to stop the execution of the loop.0535

I have this countdown, and it's equal to this huge number here.0543

While the countdown > 0...what we're going to do is say, "If this countdown class is equal to Bignum,"...it's going to say, "Oh, this number is too huge; it is going to take forever to count down to 0."0552

So, I'm going to say "Abort it, and break out"...it's going to break out of the while loop.0566

This break here allows you to stop the execution of the loop.0572

After that, if that case doesn't exist with this countdown, it's going to go ahead and decrement that countdown, one by one.0580

In this case, this is actually going to run.0591

Here is another example.0603

In this one, we are taking an Array, and we are going to break out that loop if it's not a fruit.0606

In this case, we are going to do a check, since we have this Array here: let's say a carrot came up--that's a vegetable, so we are going to abort out of this loop.0613

We are doing this until loop here, so first we check, "Hey, is the 'fruits' already empty?"--if it's empty, we're going to stop it.0622

First, we pop one fruit out.0636

If that fruit is equal to a carrot, then we are going to say, "Abort it: carrot is not a fruit."0640

What you will see with this is, it's going to first print out the apple...it's going to pop that out, I mean; so part of this 'fruit' is going to be popped out--so this is popped out.0648

Then, it's going to pop out the banana.0666

Then, it's going to get to this 'carrot', and it says, "Hey, wait: I can't work with this--this isn't a fruit."0674

It does this check, and it's going to abort and say it's not a valid fruit.0680

Now, because we actually had it pop before...it is still going to call this pop command, so it's going to be carrot here...and then it's going to say that it's aborting.0685

"Carrot is not a fruit."0703

That is how we use break for that.0709

Now, let's look at the for...in loop.0715

This is also known as the for loop.0722

Here is the structure of it: You have this for value; it has this var, and that is the value in the collection; and then you have your body of the code.0728

Now, what is var? Var is a variable or a comma-separated list of variables--we will go over that.0744

Collection is an object that contains an 'each' iteration method.0755

So, this object that is the collection itself has to have an 'each' method in there so it can do this for loop process.0763

Body is the code inside the for loop.0775

Here is an example we have: for the first one, we have a numbers array, 1 through 9.0785

Notice, what is our var? That is the i; our collection is this numbers, and this is our body--the puts i.0794

Notice what it is doing here for this loop; it's going to just print 1 to 9.0812

With this for...in, this i is this value 1, and then it will be this value 2, and what it will do is, it will go through each one of these, one by one, until it gets to the end; then it prints all the values out.0822

Again, you can do this with a range, too; range is an object that has an each method, so you can do the exact same thing.0846

It's going to print the exact same output with this.0852

For this third example here, I want to show you that you don't even need to use the do declaration.0857

Notice, here I just do for i in 1..9, and it's still going to output the same value.0865

Now, for this last example here, I have this numbers=1...this hash: numbers, 1, has a value of 1; this key 2 has a value of 2; key 3 has a value of 3.0874

What is interesting here is, notice, the var actually is a comma-separated list now.0895

The var here is actually k,v.0901

Our collection is this numbers hash.0910

It's using the hash each method, so it says, "Hey, for this each method, I'm allowed to put in two arguments," so it says, "I'm going to put k is v and a dot."0914

So, for this one, it's going to say, for the output...it will say 1 is 1, 2 is 2, 3 is 3.0926

An example here...I can show you a live example.0946

Let's do the numbers equals...1 is 1, 2 is 2, 3 equals 3.0958

We have our hash; now I'm going to create my loop.0969

For k in numbers plus k is v...end.0975

I have that same output here: 1 is 1, 2 is 2, 3 is 3.0987

Next, we are going to look at a nested loop.0997

With your loops, you can do nested loops: what is a nested loop? 1002

It's this example here; what it does first is, it looks at this value i.1005

The first one will be 1.1015

And, in this loop, then, it's going to go through this j loop.1018

It's going to go ten iterations in, and then it's going to go back to the i loop, and then it's going to work with 2,3,4,5,6,7,8,9,10.1021

This is best with an example, so let me show that to you.1033

Let's go ahead and clear this out.1038

I'm going to just put the exact same loop here.1041

We are using the range for i, and 1 to 10 for j, and 1 to 10...and I'm going to just do print i.2s--take that integer value and convert it to a string...1045

Notice that we get this huge table of values because we're running this nested loop.1079

This first value is the i value, and the second one is the j one, and the output goes all the way down.1091

That is our nested loop.1102

Next, we are going to look at numeric iterators.1112

The Integer has three commonly used iterators: upto, downto, and times.1116

There are other iterators, but we are just going to look at these popular ones.1126

The first one is this example: it's 1.upto(5), and it's really self-explanatory.1130

The upto will increment by one.1136

For this example, it's going to output 1,2,3,4, and the last one, 5.1146

Now, let's look at the downto; the downto one says, "This is 10, and I'm going down to 1."1156

This is going to decrement the value by one.1164

This is going to go 10,9,...all the way down to 1; that is where it's going to stop.1172

The last one is this times method.1179

It's interesting, because with this times, you can actually see the same thing you did with upto...so I call this 10.times with this block--it's similar to 0.upto(n-1).1185

This would be like 0.upto(9).1205

We can see that in action.1218

I do 10.times...notice that the output here is 0 to 9.1223

That is the output that is printed out.1251

The next thing we are going to look at is external iterators.1258

This uses the Enumerator class; let's take a look at that.1264

This is the Enumerable class; this is not the same one...here is the Enumerator class here; we can specify...the Enumerable class will get to that, but let's just change that for now.1274

The Enumerator class allows both internal and external iteration.1305

"An Enumerator can be created by the following methods"--there are Kernel methods here.1320

These methods "have two forms: a block form where the contents are evaluated for each item in the enumeration, and a non-block form which returns a new Enumerator wrapping the iteration."1329

The main ones we want to know are the each method...so you will notice there is an each method, and this allow external iteration, so I can put the e.next, e.next, e.next, and every time I do next, it outputs a value.1344

The next thing I want to show you is this peek_values and rewind.1368

As you create this iteration, you can see the values that are next.1376

First, you have this peek, so I can say, "Hey, I want you to tell me what will be next in this Enumerator value." 1385

I can use this peek, and under peek, see, it's going to say that it's 1.1395

Peek--that is 2, and then I can go to the next, and I can do a peek again to check.1400

You can also do peek_values just to see how far you are in the enumeration.1407

First, let's look at one external iterator.1414

We are using the downto, but this time, we're storing it in a value.1421

I call this countdown method, equals 10.downto(1)...so when I do the output, I do countdown, next, and the first one is 10; I call it again, countdown, next, the next one is 9; and again--it's 8; and I can go down all the way with that.1427

Here is another external iterator; this one is just using each.1451

I can just do e.next; it refers to this value here--the first next; the second one refers to this value, and the third one returns with that.1456

It returns the elements one by one through the iterator.1466

The next one we are going to go through is Enumerables.1473

What is an Enumerable? An Enumerable is a mixin that allows several traversal and search methods for classes.1475

It has built-in internal iterators, so you don't have to create your own.1486

Again, for this class, you have to define this each method; it's very important to using this mixin called Enumerable.1501

These mixins--you can just include them in the class you want them in, and then you have these cool traversal and search features as part of it.1509

Let's take a look at the RDoc.1518

Now we can look at our Enumerable class, here.1520

You can see it's a mixin, it provides classes for these traversal methods...I'll give you an example of what it does.1525

For example, here--look at this one--this [ant bear cat].1536

You can call this .all? method, and you can return a block.1545

And this block--you can say, "Hey, do all these words have a length greater than or equal to three?"1549

Using this, it will do the iteration for you, and it will say, "If that's true, I'll return true."1555

But, on this next example, it says, "Hey, are all of them the length 4?"1561

Since they are not all 4--only bear has 4 letters--then it's going to return false.1564

This Enumerable class allows you all these cool little methods that allow you to do these things, and they are all common methods you could be using, but you don't have to create your own; it's already built in.1571

Another one is this 'any'.1586

You can pass a block into it, and say, "Are there any words that have more than three letters?" or "...four letters?", and it returns true.1588

If you go through here, you will see that there are a lot of different examples.1599

We're going to look at more of this collect, and you will notice there are a lot of others...so we're going through some of the more common ones.1605

OK, so the common ones we are going to look at for the Enumerable objects are this Array, the Hash, and the Range.1622

First, we are going to go over Arrays.1634

First, we are going to look at the each method.1639

I have my Array: it has three elements: 1,2,3; I call the each method, and it is going to put them out, one by one.1643

That is from the Enumerable class; it allows us to do that; it's 1,2,3.1656

Next, we have our Hash; again, we have our each method, and again, it's very similar to what we were doing before with the for loop, but now we have a different way.1662

Again, they have the same output: 1 is 1, 2 is 2, 3 is 3.1680

The last one is interesting: we have this each_with_index.1687

Now, when I call this each_with_index, it will pass me this element, plus it will pass me the index value.1693

So, for this one, it will get me the 1 with the 1, 2 is 2, 3 is 3, and I can inspect it and take a look at that.1706

It gives me parts of the Hash.1721

The Range: again you can do it with Ranges, also.1725

Notice how the output is all the same, but we are doing it with different objects, and it is giving us the same one.1731

This is 1,2,3,...we're all incrementing by one.1735

Here is how the each_with_index works--it hopefully clarifies what you just saw on the other slide.1740

First, it does take arguments, and it takes object.i, index, and then you take your block of code.1746

It calls this blick with two arguments, the item and the index.1759

Let's go through an example, so it may clear it up some more.1765

We're just going to use that example we were talking about--that numbers 1,1--key is 1, our value is 1--same with 2 and 2, key is 2--3 is the key, and the value is 3.1770

OK, we have our Hash, and let's create the each_with_index now.1792

We have our block, item and index, and then we're going to output this.1804

First, I want to output the actual parts of this Hash that we're doing: so we have our item, and then we're going to show you the index value it gets.1812

Notice, the Hash elements--one by one, they are actually converted to Arrays.1832

This object is an Array of key and value; then, in that same loop, it says this is the value 0.1837

On the second one--we're going to our second part, which is 2, our value 2, that index value 1; and it goes to our third one, which is Array 3,3, and our index value is 2.1847

That is the Enumerables.1866

Now, let's look at another example of file processing.1871

With file processing, you have this each method; it's used for the lines of code in the file.1874

This example we have is example.txt.1885

This file, open...you can open the file, and then you can print out line-by-line, using this each method.1890

Let's see that in action.1900

I have an example here: I have some lorem ipsum text.1905

You will notice that it's a ton of text.1910

There are four paragraphs total.1914

What I want to do is output that.1918

I run my IRB, and then I'm going to just say the name of this file, and I'm going to do file.open, so it's going to open the file, and then it's going to create a block for that.1922

I'm going to use that each, so it's going to use that Enumerable, but notice, this file defines each its own special way, and it's going to handle them, so it's going to be line-by-line.1939

I create a block called line, and I can declare it there, and one by one, each line is outputted to the screen.1950

So it's just like we opened that text file; it has all the text here, too.1964

I know it's a lot of text--a little overwhelming--but you can see it all there.1969

So we have that; let's look at some common iterators and Enumerables.1983

Let's look at collect, select, reject, and inject.1990

First, we're going to look at the collect method.2000

It's also known as map; you can call collect or map; they do the same thing.2004

It executes its associated block with the each element of the Enumerable object; again, you can define that each; and it returns values of the block into an Array.2011

For this example, we have this 1 to 5; we're calling the collect method, and it's adding them together.2024

It's going to do 1+1, and you see 2; 2+2, you have 4...all the way up to 5+5; that is the collect.2034

The next one is the select; it's also known as the find_all method.2048

Again, you are invoking the block for each element in the object; over here, it is going to return an Array of elements for which the block returns a value other than false or nil.2054

For our example here, we have this block: it's i.mod(4) if it's equal to 0.2067

This is our block: if this returns true, it's going to store it in an Array.2076

There are only two elements in 1 through 10 that allow it--that's 4 and 8; both of them are going to return 0 for the mod, so it's going to return that Array with 4 and 8.2081

The next one we will look at is the reject method.2096

This is the opposite of select.2098

In this case, it returns an Array of elements for which the block returns false or nil.2103

Here, you have 1..10; you call the reject, and this is the exact opposite of select, so it has all the other elements in this Array.2109

The last one we're going to look at is this inject method; it's also known as reduce.2128

It combines all of the elements with a binary operation.2139

Now, this binary operation can be a block, a symbol that names the method, or an operator.2144

We'll run through some examples.2155

It can be passed an initial value, too--so if a block exists, and each element is passed an accumulator value and element...2157

Here is an example: we have a range, 5 to 10, and we call this inject method.2169

It says, "OK, I have this sum value that I'm going to continue to create and build on, and the end result...I'm going to output that to you."2175

It says, "I have some value"--in this case, it's going to be 0--it starts out as 0--but it's going to add 5 to that, and then it's going to add 6,7,8, all the way to 10.2186

Then, you get your value, 45.2208

Notice, we didn't actually pass any argument here, but if you do pass an argument, 0, notice it's still the same 45.2212

It defaults to 0 by itself.2221

You can also have a starting state, so if I put a starting state of 2, it ends up with 45 instead of 45 there.2225

This inject method is quite handy, depending on what block of process you are doing in your object.2237

If you have a number of elements, you can use this each method, and it will process it to do some type of total.2247

Here is an example: if inject is passed a symbol, each element will be passed to the main method; so we have this 5..10 range; we have a symbol here that says I'm going to do some multiplication.2256

It's getting bigger, so let's do that...and then it's going to just multiply all of those numbers together.2272

Also, remember, reduce is the same as inject--so that is why we're using reduce now.2281

And then I do 2; if I do 2, which is my starter state, it's going to do 2 times 5, all the way to 10--whereas the first one, it defaults to just one.2288

You have to notice how there is a difference there...so this is...times 2 will get you that value.2306

Other than that, that is the end of the Educator loops course.2320

Thanks for being here, and we will see you next time!2329