Hi,
As the title implies, I'm pretty new to Ruby (and programming) and I've
got a question regarding regular expressions in the above Koans
exercises, specifically with the "sub" and "gsub" methods.
What I would like to know is: is my thinking correct here:
def test_sub_is_like_find_and_replace
assert_equal ["one t-three"], "one two-three".sub(/(t\w*)/) { $1[0,
1] }
end
So, as I see it, the "sub" method is looking for the first "t" in the
string. The hash says that it will replace [0, 1] elements of the string
following the "t" ("w" and "o" respectively) with "$1", which has not
been defined, meaning it will take these letters out of the string.
The gsub exercise is the same code, but with gsub instead of sub, so in
that instance it looks to find and replace the [0, 1] elements following
*every* "t" in the string.
I think I'm way off base here, but any advice you can lend me will be
greatly appreciated.
Cheers!
--
Posted via http://www.ruby-forum.com/.