Ruby Koans: will this method ever return :empty?

S

sl4m

[Note: parts of this message were removed to make it a legal post.]

I'm having trouble understanding the return statement for this particular
koan:

class MessageCatcher
def add_a_payload(*args)
return :empty unless args
args
end
end

mc = MessageCatcher.new
mc.add_a_payload # []
mc.add_a_payload(1) # [1]

Is it possible to have this method return :empty?


skim
 
G

Gennady Bystritsky

I'm having trouble understanding the return statement for this particular
koan:
=20
class MessageCatcher
def add_a_payload(*args)

In this case (with *) args is always an array that holds all the arguments =
passed to the method, it being empty when no arguments are given. So it sho=
uld be:
return :empty unless args
return :empty if args.empty?
args
end
end
=20
mc =3D MessageCatcher.new
mc.add_a_payload # []
mc.add_a_payload(1) # [1]
=20
Is it possible to have this method return :empty?
=20
=20
skim
 
A

Aldric Giacomoni

Steve said:
I'm having trouble understanding the return statement for this
particular
koan:

class MessageCatcher
def add_a_payload(*args)
return :empty unless args
args
end
end

mc = MessageCatcher.new
mc.add_a_payload # []
mc.add_a_payload(1) # [1]

Is it possible to have this method return :empty?


skim

Sure.. Building up a little on the previous post which gave you the
answer:

class MessageCatcher
def add_a_payload *args
args.empty? :empty : args
end
end

Although as far as form and style go, it's very bad manners to have a
method return either an array or a symbol ;-) It's better to always
return an array, or always return a symbol - so it's easier to use. For
instance:

args.empty? [:empty] : args

Cheers.
 
A

Aldric Giacomoni

Aldric said:
Steve Kim wrote:
class MessageCatcher
def add_a_payload *args
args.empty? :empty : args
end
end

args.empty? [:empty] : args

I'd appreciate it if no one mentioned that I forgot the '?' twice! I am
so ashamed.

args.empty? ? :empty : args
args.empty? ? [:empty] : args
 
S

sl4m

[Note: parts of this message were removed to make it a legal post.]

Thanks guys! It looks like there's a mistake in the koans then. I'll
report it to the author. I ran this piece of code over and over on irb, but
could not figure it out.


Cheers,
skim

Aldric said:
Steve Kim wrote:
class MessageCatcher
def add_a_payload *args
args.empty? :empty : args
end
end

args.empty? [:empty] : args

I'd appreciate it if no one mentioned that I forgot the '?' twice! I am
so ashamed.

args.empty? ? :empty : args
args.empty? ? [:empty] : args
 
G

Gennady Bystritsky

Thanks guys! It looks like there's a mistake in the koans then. I'll
report it to the author. I ran this piece of code over and over on irb, = but
could not figure it out.

Wasn't that the idea behind "koans"? I haven't heard about them until your =
post, however according to http://brandon.dimcheff.com/2009/02/05/how-to-le=
arn-ruby.html:

"... The Koans are essentially a series of failing test cases that you need=
to figure out how to fix in order to move on to the next step."

From that, my understanding is that a user is supposed to find problems in =
"koans" and fix them. You found one, good going ;-)

Regards,
Gennady.
=20
=20
Cheers,
skim
=20
=20
Aldric said:
Steve Kim wrote: =20
Is it possible to have this method return :empty? =20
=20
class MessageCatcher
def add_a_payload *args
args.empty? :empty : args
end
end
=20
args.empty? [:empty] : args
=20
=20
I'd appreciate it if no one mentioned that I forgot the '?' twice! I am
so ashamed.
=20
args.empty? ? :empty : args
args.empty? ? [:empty] : args
 
G

Gennady Bystritsky

=20
On Mar 31, 2010, at 8:36 AM, sl4m wrote:
=20
=20
Wasn't that the idea behind "koans"? I haven't heard about them until you=
r post, however according to http://brandon.dimcheff.com/2009/02/05/how-to-=
learn-ruby.html:

Darn, looks like ":" becomes the part of URL when you click the above link.=
Try just this:
http://brandon.dimcheff.com/2009/02/05/how-to-learn-ruby.html
=20
"... The Koans are essentially a series of failing test cases that you ne=
ed to figure out how to fix in order to move on to the next step."
=20
From that, my understanding is that a user is supposed to find problems i=
n "koans" and fix them. You found one, good going ;-)
=20
Regards,
Gennady.
=20
=20
=20
Cheers,
skim
=20
=20
Aldric Giacomoni wrote:
Steve Kim wrote:
=20
Is it possible to have this method return :empty?
=20
=20
class MessageCatcher
def add_a_payload *args
args.empty? :empty : args
end
end
=20
args.empty? [:empty] : args
=20
=20
I'd appreciate it if no one mentioned that I forgot the '?' twice! I am
so ashamed.
=20
args.empty? ? :empty : args
args.empty? ? [:empty] : args
=20
=20
 
S

sl4m

[Note: parts of this message were removed to make it a legal post.]

Yes it's true, you're suppose to fix the code and/or test, but only if it
has a '__' or a comment indicating to fix something. In this case, however,
it's asking what the method will return if the method was called with and
without arguments. Here's the test:

class MessageCatcher
def add_a_payload(*args)
return :empty unless args
args
end
end

def test_sending_a_message_with_arguments
mc = MessageCatcher.new

assert_equal __, mc.add_a_payload
assert_equal __, mc.send:)add_a_payload)

assert_equal __, mc.add_a_payload(3, 4, nil, 6)
assert_equal __, mc.send:)add_a_payload, 3, 4, nil, 6)
end

The return statement will never return :empty in the asserts. This is what
confused me. I wanted to know when the method will ever return :empty. The
test doesn't test that...because it will never return it ;-)


skim

On Wed, Mar 31, 2010 at 09:07, Gennady Bystritsky <
Wasn't that the idea behind "koans"? I haven't heard about them until
your post, however according to
http://brandon.dimcheff.com/2009/02/05/how-to-learn-ruby.html:

Darn, looks like ":" becomes the part of URL when you click the above link.
Try just this:
http://brandon.dimcheff.com/2009/02/05/how-to-learn-ruby.html
"... The Koans are essentially a series of failing test cases that you
need to figure out how to fix in order to move on to the next step."
From that, my understanding is that a user is supposed to find problems
in "koans" and fix them. You found one, good going ;-)
Regards,
Gennady.
Cheers,
skim

Aldric Giacomoni wrote:
Steve Kim wrote:

Is it possible to have this method return :empty?


class MessageCatcher
def add_a_payload *args
args.empty? :empty : args
end
end

args.empty? [:empty] : args


I'd appreciate it if no one mentioned that I forgot the '?' twice! I am
so ashamed.

args.empty? ? :empty : args
args.empty? ? [:empty] : args
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top