Baby steps it is.
Step 1
def titleize(x)
x.capitalize.split
end
You're not taking baby steps here. A baby step would be something like:
def do_something_to_word(word)
word.upcase!
end
and then:
def do_something_to_each_word_in_phrase(phrase)
words = phrase.split
words.each { |word| do_something_to_word(word) }
words
end
(there are shorter and more Ruby-idiomatic ways to do that sort of
construct, which make it more of a baby-step, but we'll save those for
later), and then:
def do_something_to_phrase(phrase)
do_something_to_each_word_in_phrase(phrase).join("-")
end
and finally you can put it all together if you like, as:
def do_something_to_phrase(phrase)
words =
phrase.split.each { |word| words << word.upcase }
words.join("-")
end
Idiomatic Ruby would reduce this to one line (inside the method), BTW;
look up "map" (also called "collect") if interested.
Also, you need to understand the difference between the methods that
end in a bang (!), versus those with the exact same name without the
bang. As luck would have it, I *just* wrote a blog post on that a few
days ago, including about *another* big bad gotcha lurking in them to
getcha:
blog.codosaur.us/2013/08/the-big-bad-bang-or-other-gotcha-with.html
You could use either bunch in this problem, but you have to know how
to handle them either way.
Questions
1. This splits and capitalizes the word right?
It capitalizes the string you pass in, and *then* splits it. The
order of typing is, in this case, also the order of execution. You
want the other way around.
2. Word encased in , how do we get rid of that?
Think a second about what the square brackets signify. Then think why
that would happen. (It's much more obvious if you pass it multiple
words.) If you're intending this to only act on single words at a
time, yes, that would be more of a baby step -- but then there's no
need for split.
3. What do you mean when you say but it back together?
Make the square brackets go away... even with multiple words.
Don't be ashamed to take baby steps, like it's not grown-up or
something. We do it all the time. It's the best way to keep clear in
your head what you're trying to do right now, and make sure you're
doing (or, when finished, have done) it right.
Also, don't think small steps mean slow progress; I can't think of a
*good* analogy, but think of it like the further you're going at one
shot, the more of a (mental) burden you have to bear. Like a rocket,
which needs to haul more fuel the further it's going, and more fuel to
haul that fuel, and so on, to the point where on trips far into space,
the fuel winds up being most of the total mass... while the proportion
of fuel in a little toy rocket is very small.
-Dave
···
On Tue, Aug 27, 2013 at 7:27 PM, trying 2 learn <lists@ruby-forum.com> wrote:
--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.