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"]]
How to Read Code | 49
How to Read Code
As implied by the quote introducing this chapter, the primary purpose of source
code should not be expressing implementation to a computer; it should be expressing
meaning to people. Programming languages are an incredibly expressive and
terse medium for the concepts programmers talk about. Proposals to make programming
languages more English-like inevitably fail not because of poor implementation
but because there is an inherent impedance mismatch between the domains of
English language and computer programming.
Thus, computer programming languages should be compared not by their levels of
raw power (any Turing-complete language trivially satisfies this requirement) or
speed of execution (for most applications, speed is not critical) but by their programmer
efficiency??”the speed at which a programmer can accurately translate his
thoughts into code.
Pages:
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85