class String
History: When doing String#any_space? I needed a way to iterate through each character in a string for comparison but not using the ascii representation such as with each_byte.
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
.
Public Instance Methods
each(unit = :line, &block)
click to toggle source
# 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() { |self| ... }
click to toggle source
# File lib/String/each_char.rb, line 10 def each_char (0..(self.size - 1)).each{|i| yield self[i, 1]} end
each_line() { |lines| ... }
click to toggle source
# 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)
click to toggle source
# File lib/String/grep.rb, line 22 def grep(pattern) self.select{|line| line =~ pattern}.join("\n") end