Petty Theft from Python #
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.
MenTaLguY
Yeah, I think so too.
However, the existing
Hash::[]
is good for hacks like that HOSTS thing from hoodlum/mouseHole.So, I’m a bit torn. I kind of wish we could have both, but there would be some uncomfortable ambiguities.
Daniel Berger
why
Heh.
Okay.
Dave Burt
Like this?
h[*keys] = vals
or
*h[*keys] = *vals
MenTaLguY
Daniel: Yes, that would be good too. And very practical.
Does that look about right?
Array slices would be nice too, but I’m not sure how well that would work with the two-argument forms of
Array#[]
andArray#[]=
.MenTaLguY
Bloody heck. I get up to take a break before posting and someone steals my brain.
Good show Dave.
:)
MenTaLguY
Dave: how about we use my
Hash#[]
and yourHash#[]=
? You forgot to returnrhs
in yours though…MenTaLguY
Scary how close we both came, though. Variables and all.
In RedHanded, has why discovered the resonant frequency of the human brain!?
Are you prepared for the terrible secret of RUBY !?
MenTaLguY
Thinking about
Array#[]
, the way you would have to use it for slicing would besomearray[[1, 2]]
to avoid ambiguity withsomearray[1, 2]
.I wonder whether there’s a more consistent way to manage slicing between arrays and hashes?
MenTaLguY
If only we could have additional arguments for non-indexing setters… (a la
somearray.section(1, 2) = 'a', 'b'
)Daniel Berger
It’s not my code, btw. The “hashslice” package is by Michael Granger, available on the RAA .
Dave Burt
I like ours better than Granger’s, MenTaLguY. Of course, the only real differences returning rhs (which Granger’s hashslice fails to do) and error throwing (I think a 0-length slice should be allowed, despite my []= code).
I just have to add that I think it’s cool you can do
obj[foo, bar] = baz, quux
andbaz, quux
will be arrayified.flgr
http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/8cd6f94516d054ac
flgr
Regarding the original: I think it is commonly done as Hash[*pairs.flatten] in Ruby.
why
flgr:
Dave Burt, MenTaLguY: You are miracles.
trans
Nano Methods has just about all of these concepts, from slicingg with hash#[]= and array#[[]] to the orginal request in Hash.zipnew.
Improvements to implementations are great esteemed.
Thijs
That’s funny! I just added a patch for this to rails the other day..
flgr
why: I dunno, I suggested Hash.from_assoc a while ago, IIRC .
jot
Also kind of start/end-with: ar = [“abcc”, “abc”]; ar.delete_if { |i| /^a.c$/.match(i) }; puts ar (may be interesting to Ruby beginners)
nil
jot: Sometimes it’s better to use [^ ] instead of . (which will also match a space)!
jot
And don’t forget this one as well: rxp = Regexp.new(/ruby/, “i”); puts rxp.inspect
Comments are closed for this entry.