Skip to content
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

Open
ianalexh opened this issue May 14, 2013 · 6 comments
Open

Returning value in "times" iterator #9

ianalexh opened this issue May 14, 2013 · 6 comments

Comments

@ianalexh
Copy link

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
Copy link

You can try using Enumerable#inject on a Range.

@ianalexh
Copy link
Author

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
Copy link
Owner

alexch commented May 15, 2013

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
Copy link
Author

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
Copy link
Owner

alexch commented May 15, 2013

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants