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
On Mar 30, 2010, at 11:38 PM, sl4m wrote:
I’m having trouble understanding the return statement for this particular
koan:
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 should be:
return :empty unless args
return :empty if args.empty?
Steve Kim wrote:
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.
Aldric G. 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
On Mar 31, 2010, at 8:36 AM, sl4m wrote:
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-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.
On Mar 31, 2010, at 8:57 AM, Gennady B. wrote:
On Mar 31, 2010, at 8:36 AM, sl4m wrote:
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-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
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
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 B. <
A link to the source of the koan you are referring to would have been
nice.
My two cents.
class MessageCatcher
def add_a_payload(*args)
return :empty unless args
args
end
def check(*args)
args.empty? ? :empty : args
end
end
p mc = MessageCatcher.new
p mc.add_a_payload # []
p mc.add_a_payload(1) # [1]
p mc = MessageCatcher.new
p mc.check
I’m not in irb so I process it, inspect it, or eval or whatever.
if FILE == $PROGRAM_NAME
do your thing here to preserve the code reuse.
end