alexch / learn_ruby

a basic ruby class generated from test-first-teaching
http://testfirst.org
261 stars 2.84k forks source link

Returning value in "times" iterator #9

Open ianalexh opened 11 years ago

ianalexh commented 11 years ago

https://github.com/alexch/learn_ruby/tree/master/03_simon_says

This is a clear user error, but I can't find a different method or think of a different way.

In the "repeat" method, I'm supposed to be able to take any text and repeat it X number of times. The "times" method seemed the best way, but "times" returns the integer, not the printed text. I can't get past this exercise right now!

def repeat(input, x=2) x.times {print "#{input} "} end

imurchie commented 11 years ago

You can try using Enumerable#inject on a Range.

ianalexh commented 11 years ago

Thanks!

I will have to try that out. My solution in the meantime was to force a return, thus:

def repeat(input, x=2) print_array = [] x.times {print_array.push("#{input}")} return print_array.join(" ") end

...force it into an array, then kick it back out. It's what I call "Butt-ugly Ruby" (because I'm sure there's a better way).

In general, I'm still having trouble understanding returns. I'll have to keep working at that.

alexch commented 11 years ago

Hint: it's not supposed to print anything. Just return a string. Think about how to build that string.

On May 14, 2013, at 12:15 AM, ianalexh notifications@github.com wrote:

https://github.com/alexch/learn_ruby/tree/master/03_simon_says

This is a clear user error, but I can't find a different method or think of a different way.

In the "repeat" method, I'm supposed to be able to take any text and repeat it X number of times. The "times" method seemed the best way, but "times" returns the integer, not the printed text. I can't get past this exercise right now!

def repeat(input, x=2) x.times {print "#{input} "} end

— Reply to this email directly or view it on GitHub.

ianalexh commented 11 years ago

Hmmmmm.... thinking through..... in my second edition, rather than print, I force each word into an array. Then I spit the array back into a string (probably should not have used the name "print_array", as that is confusing).

That all seems convoluted, though... I keep trying to think of a better way, but I don't know yet if I have the tools / skills to build another way.

In my early tutorials (codecademy), the desired result is usually something that prints or puts to the screen. I haven't yet understood how to control returns.

It's similar in my pig-latin exercise. I've found a way to PRINT out the proper pig latin, but I can't get it to RETURN pig latin.

alexch commented 11 years ago

You have hit upon the primary reason I wrote these test-first exercises: to teach people how to write code that returns values, which is much more useful than code that merely prints.

Turning a string into an array and back again is a very popular Ruby trick. You are doing fine!

Look around my notes at http://codelikethis.com/lessons and you'll see several examples, e.g.

def titleize s s.split.map(&:capitalize).join(" ") end

(the "return" is implied for the last line in a method)

ianalexh commented 11 years ago

Very nice work. These exercises really are top-notch. I know that once I've worked through, my skill will have increased a hundred fold. Thanks again!