Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #6567

Re: what's up with return *splat ?

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: what's up with return *splat ?
Date 2012-06-16 15:04 +0200
Message-ID <a43eidFbv2U1@mid.individual.net> (permalink)
References <0fae13bb-3468-4883-b855-17950bee1117@t2g2000pbl.googlegroups.com> <a3pq3mFm27U1@mid.individual.net> <2a77ca16-20d5-4700-bf7d-0a746f4730d6@e7g2000pbg.googlegroups.com>

Show all headers | View raw


On 13.06.2012 02:10, Phlip wrote:
>> Can you please show the code you used for testing?  Thank you.
>
> My assert_latest() stopped working. Here's an independent test case:
>
> p RUBY_VERSION
>
> def assert(x);  x or raise 'broke!';  end
>
> def splat(*wat); return *wat;  end
>
> assert nil == splat()
> assert 'yo' == splat('yo')
> assert ['yo', 'dude'] == splat('yo', 'dude')
>
> assert [] == splat()
> assert ['yo'] == splat('yo')
> assert ['yo', 'dude'] == splat('yo', 'dude')
>
> The top three assertions pass in 1.8.7, and the bottom three pass in
> 1.9.2.

I get different results:

$ ruby x.rb
"1.8.7"
broke! x.rb:12
broke! x.rb:13
$ ruby19 x.rb
"1.9.3"
broke! x.rb:8:in `<main>'
broke! x.rb:9:in `<main>'
$ cat -n x.rb
      1
      2  p RUBY_VERSION
      3
      4  def assert(x);  x or warn "broke! #{caller[0]}";  end
      5
      6  def splat(*wat); return *wat;  end
      7
      8  assert nil == splat()
      9  assert 'yo' == splat('yo')
     10  assert ['yo', 'dude'] == splat('yo', 'dude')
     11
     12  assert [] == splat()
     13  assert ['yo'] == splat('yo')
     14  assert ['yo', 'dude'] == splat('yo', 'dude')
     15
     16  a,b=splat(1,2)
     17  assert a == 1
     18  assert b == 2
     19
     20  a,b=splat(1)
     21  assert a == 1
     22  assert b == nil
     23
     24  a,b=splat
     25  assert a == nil
     26  assert b == nil

And the interesting bits (lines 16ff) are treated identical.

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?

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

what's up with return *splat ? Phlip <phlip2005@gmail.com> - 2012-06-12 11:48 -0700
  Re: what's up with return *splat ? Robert Klemme <shortcutter@googlemail.com> - 2012-06-12 23:19 +0200
    Re: what's up with return *splat ? Phlip <phlip2005@gmail.com> - 2012-06-12 17:10 -0700
      Re: what's up with return *splat ? Robert Klemme <shortcutter@googlemail.com> - 2012-06-16 15:04 +0200
        Re: what's up with return *splat ? Phlip <phlip2005@gmail.com> - 2012-06-21 16:24 -0700
          Re: what's up with return *splat ? Robert Klemme <shortcutter@googlemail.com> - 2012-06-22 18:24 +0200
            Re: what's up with return *splat ? Phlip <phlip2005@gmail.com> - 2012-06-22 10:02 -0700
              Re: what's up with return *splat ? Robert Klemme <shortcutter@googlemail.com> - 2012-06-22 23:36 +0200
                Re: what's up with return *splat ? Phlip <phlip2005@gmail.com> - 2012-06-24 16:27 -0700

csiph-web