Forum: Ruby what's wrong with this method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
equinox (Guest)
on 2008-11-24 19:45
(Received via mailing list)
can someone tell me what's wrong with this method??

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 "/"
matt neuburg (Guest)
on 2008-11-24 19:56
(Received via mailing list)
equinox <removed_email_address@domain.invalid> wrote:

> can someone tell me what's wrong with this method??
>
> 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.
Pod P. (Guest)
on 2008-11-24 19:56
equinox wrote:
> can someone tell me what's wrong with this method??
>
> 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)! :)
Patrick D. (Guest)
on 2008-11-24 19:57
(Received via mailing list)
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
equinox (Guest)
on 2008-11-24 21:25
(Received via mailing list)
On Nov 24, 10:52 am, Lee G. <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
Pod P. (Guest)
on 2008-11-24 22:29
equinox wrote:
> On Nov 24, 10:52�am, Lee G. <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/
Mark T. (Guest)
on 2008-11-24 22:41
(Received via mailing list)
> 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?
This topic is locked and can not be replied to.