Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

4 Regex Enumerables


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

4 Regex Enumerables

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