The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Petty Theft from Python

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
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Petty Theft from Python Posted: Sep 6, 2005 2:56 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Petty Theft from Python
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

Lucas Carlson has lifted a few method ideas from Python: starts_with? and ends_withs?, as well as in?. Which got me thinking about an unfinished thought from FOSCON. I’d really like Python’s dict method.

Python’s Dictzip maneuvre:

 >> keys = ['a', 'b', 'c']
 >> values = [1, 2, 3]
 >> dict(zip(keys, values))
 => {'a': 1, 'b': 2, 'c': 3} 

A Ruby translation might go like:

 >> keys = ['a', 'b', 'c']
 >> values = [1, 2, 3]
 >> Hash[keys.zip(values)]
 => {'a' => 1, 'b' => 2, 'c' => 3}

Here’s the code:

  def Hash.[]( enum )
    enum.inject({}) do |hsh,(k,v)| 
      hsh[k] = v
      hsh
    end
 end

I know there’s already a Hash::[] but I really like this better. An array of pairs makes so much more sense than a flattened array of arbitrary length.

Read: Petty Theft from Python

Topic: Just a Few MouseHole Tips Previous Topic   Next Topic Topic: MouseHole 1.1 in Plain View

Sponsored Links



Google
  Web Artima.com   

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