The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Splat is good for you

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ryan Davis

Posts: 651
Nickname: zenspider
Registered: Oct, 2004

Ryan Davis is a ruby nerd.
Splat is good for you Posted: Dec 2, 2005 5:48 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Ryan Davis.
Original Post: Splat is good for you
Feed Title: Polishing Ruby
Feed URL: http://blog.zenspider.com/index.rdf
Feed Description: Musings on Ruby and the Ruby Community...
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Ryan Davis
Latest Posts From Polishing Ruby

Advertisement
A recent post on projectionist shows two code examples:
  def foo(*args)
    [args].flatten.map do |arg|
      # ...
    end
  end

  def foo(*args)
    Array(args).map do |arg|
      # ...
    end
  end
and argues their (ugly) necessity. I think both examples fail to hit the mark (or there is a typo?) as they've ensured the initial codition simply by using *args. Allowing them to state the problem in a straightforward fashion:
  def foo(*args)
    args.flatten.map do |arg| # if you are trying to combine a bunch of arrays
      # ...
    end
  end

  def foo(*args)
    args.map do |arg|
      # ...
    end
  end
which, is much more natural. If I had to guess, I'd say they meant to not put the splat in front of args. I find using splat to be a better fit.

Read: Splat is good for you

Topic: Try Ruby: Resets and Chapter Hops Previous Topic   Next Topic Topic: New Toy

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use