# What's wrong with this method

**URL:** <https://www.ruby-forum.com/t/whats-wrong-with-this-method/154032>\
**Category:** Ruby\
**Created:** [November 24, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-method/154032 "2008-11-24T18:45:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![equinox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/47e85d/32.png) [@equinox](https://www.ruby-forum.com/u/equinox)\
**Post date:** [November 24, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-method/154032/1 "2008-11-24T18:45:46Z")

</div>

can someone tell me what’s wrong with this method??

[http://pastie.org/322677](http://pastie.org/322677)

after that FRIENDS I print I want to iterate over the hash that I have  
and print the key, value of each separated by a “/”

---

<div class="post-metadata">

**Author:** ![equinox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/47e85d/32.png) [@equinox](https://www.ruby-forum.com/u/equinox)\
**Post date:** [November 24, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-method/154032/2 "2008-11-24T18:56:22Z")

</div>

equinox [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> can someone tell me what’s wrong with this method??
> 
> [http://pastie.org/322677](http://pastie.org/322677)
> 
> after that FRIENDS I print I want to iterate over the hash that I have  
> and print the key, value of each separated by a “/”

Your pastie consists of a two-line method definition, to\_out().

The first line of to\_out() is valid but does nothing; it defines a  
literal string whose value is then thrown away.

The second line will not compile because it tries to combine the “do”  
form of a block with the curly-braces ({}) form of a block. Pick one or  
the other. For example, delete “do”.

The second line will then work (well, it will do _something_, though  
what it does is pretty weird-looking) provided you have a @friends hash.  
There is no indication in your pastie to tell whether you do or you  
don’t.

m.

---

<div class="post-metadata">

**Author:** ![equinox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/47e85d/32.png) [@equinox](https://www.ruby-forum.com/u/equinox)\
**Post date:** [November 24, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-method/154032/3 "2008-11-24T18:56:55Z")

</div>

equinox wrote:

> can someone tell me what’s wrong with this method??
> 
> [http://pastie.org/322677](http://pastie.org/322677)
> 
> after that FRIENDS I print I want to iterate over the hash that I have  
> and print the key, value of each separated by a “/”

You have a syntax error, for a start. Is this the problem you mean?

@friends.each do {|key, value| puts(“#{key}/#{value} ,”) }

It’s EITHER { } OR do…end. Not both (or half of one)! 🙂

---

<div class="post-metadata">

**Author:** ![equinox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/47e85d/32.png) [@equinox](https://www.ruby-forum.com/u/equinox)\
**Post date:** [November 24, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-method/154032/4 "2008-11-24T18:57:33Z")

</div>

What is the “to\_out” method supposed to do? Is it supposed to return a  
string, or print stuff to stdout? The first line of code creates a  
string  
filled all sorts of interesting information about your object (username,  
name, sex, …). The second line calls #puts some number of times  
depending  
on how many elements are in the @friends hash.

–wpd

---

<div class="post-metadata">

**Author:** ![equinox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/47e85d/32.png) [@equinox](https://www.ruby-forum.com/u/equinox)\
**Post date:** [November 24, 2008, 8:25pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-method/154032/5 "2008-11-24T20:25:45Z")

</div>

On Nov 24, 10:52 am, Lee G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @friends.each do {|key, value| puts(“#{key}/#{value} ,”) }
> 
> ## It’s EITHER { } OR do…end. Not both (or half of one)! 🙂
> 
> Posted viahttp://www.ruby-forum.com/.

I just want it to return a string… and for the second line I want to  
get the string from the key value pair in the hash

---

<div class="post-metadata">

**Author:** ![equinox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/47e85d/32.png) [@equinox](https://www.ruby-forum.com/u/equinox)\
**Post date:** [November 24, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-method/154032/6 "2008-11-24T21:29:49Z")

</div>

equinox wrote:

> On Nov 24, 10:52ï¿½am, Lee G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > @friends.each do {|key, value| puts(“#{key}/#{value} ,”) }
> > 
> > ## It’s EITHER { } OR do…end. Not both (or half of one)! 🙂
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> I just want it to return a string… and for the second line I want to  
> get the string from the key value pair in the hash

Only the _LAST_ statement in a function is the return value. So put that  
string second after you’ve done a puts. If you haven’t alread done so,  
give this a read: [http://www.rubycentral.com/book/](http://www.rubycentral.com/book/)

---

<div class="post-metadata">

**Author:** ![equinox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/47e85d/32.png) [@equinox](https://www.ruby-forum.com/u/equinox)\
**Post date:** [November 24, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/whats-wrong-with-this-method/154032/7 "2008-11-24T21:41:23Z")

</div>

> I just want it to return a string… and for the second line I want to  
> get the string from the key value pair in the hash

That’s exactly what they gave you. Did you try it?
