Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.ruby Subject: Re: what's up with return *splat ? Date: Fri, 22 Jun 2012 18:24:10 +0200 Lines: 71 Message-ID: References: <0fae13bb-3468-4883-b855-17950bee1117@t2g2000pbl.googlegroups.com> <2a77ca16-20d5-4700-bf7d-0a746f4730d6@e7g2000pbg.googlegroups.com> <9040131a-0ad1-4736-a0e3-693b9cf58a27@g4g2000pbn.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net fVu3aOC5UxUnGd66s4Cw/A4VxpAnddONiX87oiU8dhCPbnxG14e7qAezRY3n3+H0U= Cancel-Lock: sha1:lab/w0cNSZ2OG+PGspggzvPiyj4= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 In-Reply-To: <9040131a-0ad1-4736-a0e3-693b9cf58a27@g4g2000pbn.googlegroups.com> Xref: csiph.com comp.lang.ruby:6573 On 22.06.2012 01:24, Phlip wrote: > Tx for the experiment; it confirmed mine. Strange. I said > I get different results: > ... >> I think you use a function in one of two ways: either you expect one >> result and that can be nil or not, or you expect multiple replies and >> assign them to different variables which can either be nil or not. >> >> What practical use case is impaired by the difference? > > When I write an assertion, I know the ordinality of the expected > result. > > note1, note2 = assert_latest User.notes do > User.create_notes() > end > > assert{ user.notes == [note1, note2] } > > frob = assert_latest Frob do > production_code_creating_frobs() > end > > assert{ frob.member == 42 } > > I want the assertions to break, with simple syntax errors, if the > ordinality is wrong. You cannot get syntax errors for this. This is checked at runtime and not at parse time. > If assert_latest only returned arrays, that would > cause clutter like this: > > frobs = assert_latest Frob do > production_code_creating_frobs() > end > > assert{ frobs.count == 1 and frobs[0].member == 42 } > > Instead of testing the count, I want to simply use the result as if > it's what I expect, and then fail as early as possible if it isn't. Note that you can do this in 1.9.* and 1.8.7 (the comma): irb(main):002:0> def f(*a) a end => nil irb(main):003:0> f 1,2 => [1, 2] irb(main):004:0> x=f 1,2 => [1, 2] irb(main):005:0> x => [1, 2] irb(main):006:0> x,=f 1,2 => [1, 2] irb(main):007:0> x => 1 Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/