Ruby we might Miss

Posted on August 9, 2011


This is the continuation part of the series I started.
The topic for this post :


String#%(args) interpolates the arguments into itself in the manner of sprintf. To provide
more than one value for interpolation, you must supply an array.
“%.5f” % Math::PI # => “3.14159”
“%.5f, %.5f” % [Math::PI, Math::E] # => “3.14159, 2.71828”

• String#[](regex) returns the portion of the string that matches the given regular
expression. If there is no matching portion, nil is returned.
“asdf”[/sd/] # => “sd”
“asdf”[/^sd/] # => nil
“asdf”[/d(.)/,1] # => “f”

• String#scan(regex) collects all of the regular expression’s matches against the string
into an array. If the pattern has captures, each element of the array is itself an array of
captured text.
“asdf”.scan(/[a-e]/) # => [“a”, “d”]
“hello ruby; hello regex”.scan(/hello (\w+)/) # => [[“ruby”], [“regex”]]

Posted in: Ruby