4 Regex Enumerables
Upcoming SlideShare
Loading in...5
×
 

4 Regex Enumerables

on

  • 1,292 views

 

Statistics

Views

Total Views
1,292
Views on SlideShare
1,291
Embed Views
1

Actions

Likes
0
Downloads
8
Comments
0

1 Embed 1

https://www.mturk.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • s finds spaces, tabs and new lines
  • its fine to use is_a in irb..in code should use respond_to?

4 Regex Enumerables 4 Regex Enumerables Presentation Transcript

  • Class 4
    Regular Expressions & Enumerables
  • Regular Expressions
    /regex will find me/
  • What are Regular Expressions
    Regular expressions allow matching and manipulation of textual data.
    Abbreviated as regex or regexp, or alternatively, just patterns
  • What are Regular Expressions For?
    Scan a string for multiple occurrences of a pattern.
    Replace part of a string with another string.
    Split a string based on a matching separator.
  • Regular Expressions in Ruby
    • Regular expressions are put between two forward slashes (/match_me/)
    • They are escaped with a backward slash ().
  • Characters That Need to be Escaped
    . | ( ) [ ] { } + ^ $ * ?
  • Regex Basics
    [abc] A single character: a, b or c
    [^abc] Any single character but a, b, or c
    [a-z] Any single character in the range a-z
    [a-zA-Z] Any single character in the range a-z or A-Z
    ^ Start of line
    $ End of line
    A Start of string
    z End of string
  • Regex Basics cont...
    . Any single character
    s Any whitespace character
    S Any non-whitespace character
    d Any digit
    D Any non-digit
    w Any word character (letter, number, underscore)
    W Any non-word character
     Any word boundary character
  • Regex Basics cont...
    (...) Capture everything enclosed
    (a|b) a or b
    a? Zero or one of a
    a* Zero or more of a
    a+ One or more of a
    a{3} Exactly 3 of a
    a{3,} 3 or more of a
    a{3,6} Between 3 and 6 of a
  • Regex: .match
    >> category = "power tools"
    => "power tools"
    >> puts "on Sale" if category.match(/power tools/)
    on Sale
    >> puts "on Sale" if /power tools/.match(category)
    on Sale
  • Regex: =~
    >> category = "shoes"
    => "shoes"
    >> puts "15 % off" if category =~ /shoes/
    15 % off
    >> puts "15 % off" if /shoes/ =~ category
    15 % off
    >> /pants/ =~ category
    => nil
    >> /shoes/ =~ category
    => 0
    >> category = "women's shoes”
    >> /shoes/ =~ category
    => 8
    8th character
  • Scan
    >> numbers = "one two three"
    => "one two three"
    >> numbers.scan(/w+/)
    => ["one", "two", "three”]
  • Split with Regular Expressions
    >> "one two three".split(/s/)
    => ["one", ”two", "three"]
  • gsub
    ”fred,mary,john".gsub(/fred/, “XXX”)
    => “XXX,mary,john”
  • gsub with a block
    "one two three".gsub(/(w+)/) do |w| puts w
    end
    one
    two
    three
  • Title Case
    Capitalize All Words of a Sentence:
    >> full_name.gsub(/w/){|s| s.upcase}
    => "Yukihiro Matsumoto"
  • Live Coding Example
    Scraping Thentic.com
  • Enumerables
    Collection Behavior
  • Why Use Enumerables
    Ruby's Enumerable module has methods for all kinds of tasks.
    If you can imagine a use for the #each method other than simply iterating, there is a good chance a method exists to do what you have in mind.
  • What does Enumerable Mean?
    Collection objects (instances of Array, Hash, etc) typically “mixin” the Enumerable module
    The Enumerable module gives objects of collection classes additional collection-specific behaviors.
    The class requiring the Enumerable module must have an #each method because the additional collection-specific behaviors given by Enumerable are defined in terms of #each
  • Mixing in Enumerable
    class MyCollection
    include Enumerable
    #lots of code
    def each
    #more code
    end
    end
  • View all Classes Mixing in Enumerable
    ObjectSpace.each_object(Class) do |cl|
    puts cl if cl < Enumerable
    end
  • Enumerable::Enumerator
    Struct::Tms
    Dir
    File
    IO
    Range
    Struct
    Hash
    Array
    String
    Struct::Group
    Struct::Passwd
    MyCollection
    StringIO
    Gem::SourceIndex
    YAML::Set
    YAML::Pairs
    YAML::Omap
    YAML::SpecialHas
  • Test an Instance or Class
    >> a = [1,2,3]
    => [1, 2, 3]
    >> a.respond_to? :any?
    => true
    >> a.is_a? Enumerable
    => true
    >> Array < Enumerable
    => true
  • each
    Classes that include the Enumerable module must have an #each method.
    The #each method yields items to a supplied code block, one at a time
    Different Classes define #each differently
    Array: #each yields each element
    Hash: each yields #each key/value pair as a two-element array
    >> v_names = %w(car truck bike)
    => ["car", "truck", "bike"]
    >> v_names.each do |vehicle|
    ?> puts vehicle
    >> end
  • map
    The map method modifies each member according to instructions in a block and returns the modified collection of members.
    >> v_names.map { |v| v.upcase}
    => ["CAR", "TRUCK", "BIKE"]
  • grep
    The grep method 'searches' for members using a regular expression.
    >> v_names.grep /a/
    => ["car"]
    >> v_names.grep(/a/) { |v| v.upcase}
    => ["CAR"]
  • find
    >> v_names.find { |v| v.size > 3}
    => "truck"
    >> v_names.find { |v| v.size > 2}
    => "car"
    >> v_names.find do |v|
    v.size > 3 && v.size < 5
    end
    => "bike"
  • all?
    The all? method returns true if all of the members of a collection satisfy the evaluation of the block. Otherwise it returns false.
    >> v_names.all? { |v| v.length > 2}
    => true
    >> v_names.all? { |v| v.length > 10}
    => false
  • any?
    The any? method returns true if any of the members of a collection satisfy the evaluation of the block. Otherwise it returns false.
    >> v_names.any? { |v| v.length == 3}
    => true
    >> v_names.any? { |v| v = "car"}
    => true
  • Working with Complex Data
    irb
    >> load 'vehicles.rb'
    => true
  • inject
    >> $vehicles.inject(0) do |total_wheels, v|
    ?> total_wheels += v[:wheels]
    >> end
    => 10
    >> $vehicles.inject([]) do |classes, v|
    ?> classes += v[:classes]
    >> end.uniq
    => [:ground, :water, :air]
  • Complex Operations
    >> $vehicles.find do |v|
    ?> v[:name] =~ /Plane/
    >> end[:name]
    => "Plane"
    >> $vehicles.find_all do |v|
    ?> v[:name] =~ /Plane/
    >> end.collect { |v| v[:name] }
    => ["Plane", "Sea Plane"]
    >> $vehicles.find_all do |v|
    ?> v[:wheels] > 0
    >> end.collect { |v| v[:name] }
    => ["Car", "Truck", "Bike"]
  • Complex Operations Continued
    >> $vehicles.find_all do |v|
    ?> v[:classes].include? :ground
    >> end.collect { |v| v[:name] }
    => ["Car", "Truck", "Bike", "Sea Plane"]
    >> $vehicles.find_all do |v|
    ?> v[:classes].include? :air
    >> end.collect { |v| v[:name] }
    => ["Plane", "Helicopter", "Sea Plane"]
  • Homework
    Chapters:
    10.1 – 10.8
    11.1 -11.8