-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Returning value in "times" iterator #9
Comments
You can try using Enumerable#inject on a Range. |
Thanks! I will have to try that out. My solution in the meantime was to force a return, thus: def repeat(input, x=2) ...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. |
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:
|
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. |
You have hit upon the primary reason I wrote these test-first exercises: to Turning a string into an array and back again is a very popular Ruby trick. Look around my notes at http://codelikethis.com/lessons and you'll see def titleize s (the "return" is implied for the last line in a method) |
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! |
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
The text was updated successfully, but these errors were encountered: