Class: String
- Inherits:
-
Object
- Object
- String
- Includes:
- Enumerable
- Defined in:
- lib/String/each_char.rb,
lib/String/each.rb,
lib/String/grep.rb,
lib/String/each_line.rb
Overview
Description: Ruby 1.9 no longer mixes in Enumerable, so there’s no #each method. Here’s the equivalent 1.8 version of String’s missing #each method, but renamed to describe it’s functionality more correctly. See String#each, which calls this method as the default behaviour. Also see String#each_char.
Instance Method Summary collapse
- #each(unit = :line, &block) ⇒ Object
- #each_char ⇒ Object
- #each_line ⇒ Object
- #grep(pattern) ⇒ Object
Instance Method Details
#each(unit = :line, &block) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/String/each.rb', line 13 def each(unit = :line, &block) case unit when :line; each_line(&block) when :char; each_char(&block) end end |
#each_char ⇒ Object
10 11 12 |
# File 'lib/String/each_char.rb', line 10 def each_char (0..(self.size - 1)).each{|i| yield self[i, 1]} end |
#each_line ⇒ Object
10 11 12 13 |
# File 'lib/String/each_line.rb', line 10 def each_line lines = self.split("\n") 0.upto(lines.size - 1){|i| yield lines[i]} end |
#grep(pattern) ⇒ Object
22 23 24 |
# File 'lib/String/grep.rb', line 22 def grep(pattern) self.select{|line| line =~ pattern}.join("\n") end |