[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Define a hash using %q?

Joe Ruby

10/29/2006 5:26:00 AM

Is there a way for us lazy typists to define a hash using %q? I tried
this, but it doesn't create a hash:

h = %q(a => b, c => d)

Thanks,
Joe

--
Posted via http://www.ruby-....

15 Answers

snacktime

10/29/2006 5:32:00 AM

0

On 10/28/06, Joe Ruby MUDCRAP-CE <joeat303@yahoo.com> wrote:
> Is there a way for us lazy typists to define a hash using %q? I tried
> this, but it doesn't create a hash:
>
> h = %q(a => b, c => d)

h = {:a => b, :c => d}
h = {'a' => b, 'c' => d}

Joel VanderWerf

10/29/2006 5:35:00 AM

0

Joe Ruby MUDCRAP-CE wrote:
> Is there a way for us lazy typists to define a hash using %q? I tried
> this, but it doesn't create a hash:
>
> h = %q(a => b, c => d)

irb(main):006:0> Hash[*%w{ a b c d }]
=> {"a"=>"b", "c"=>"d"}

More decadent:

irb(main):008:0> def mkh arg; Hash[*arg]; end
=> nil
irb(main):009:0> mkh %w{ a b c d }
=> {"a"=>"b", "c"=>"d"}

Ultra decadent:

irb(main):017:0> class Array; def -@; Hash[*self]; end; end
=> nil
irb(main):018:0> -%w{ a b c d }
=> {"a"=>"b", "c"=>"d"}

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

snacktime

10/29/2006 5:36:00 AM

0

Hehe, never mind, no I don't know how to do that...


On 10/28/06, snacktime <snacktime@gmail.com> wrote:
> On 10/28/06, Joe Ruby MUDCRAP-CE <joeat303@yahoo.com> wrote:
> > Is there a way for us lazy typists to define a hash using %q? I tried
> > this, but it doesn't create a hash:
> >
> > h = %q(a => b, c => d)
>
> h = {:a => b, :c => d}
> h = {'a' => b, 'c' => d}
>

Logan Capaldo

10/29/2006 6:21:00 AM

0

On Sun, Oct 29, 2006 at 02:35:02PM +0900, Joel VanderWerf wrote:
> Joe Ruby MUDCRAP-CE wrote:
> >Is there a way for us lazy typists to define a hash using %q? I tried
> >this, but it doesn't create a hash:
> >
> >h = %q(a => b, c => d)
>
> irb(main):006:0> Hash[*%w{ a b c d }]
> => {"a"=>"b", "c"=>"d"}
>
> More decadent:
>
> irb(main):008:0> def mkh arg; Hash[*arg]; end
> => nil
> irb(main):009:0> mkh %w{ a b c d }
> => {"a"=>"b", "c"=>"d"}
>
> Ultra decadent:
>
> irb(main):017:0> class Array; def -@; Hash[*self]; end; end
> => nil
> irb(main):018:0> -%w{ a b c d }
> => {"a"=>"b", "c"=>"d"}
>
Alternative decandence:

class Array
def to_hsh
require 'enumerator'
to_enum(:each_slice, 2).to_a.inject({}) { |h, (k, v)| h.update(k=>v) }
end
end

%w( a b c d ).to_hsh
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf

10/29/2006 6:57:00 AM

0

Logan Capaldo wrote:
...
> Alternative decandence:
> class Array
> def to_hsh
> require 'enumerator'
> to_enum(:each_slice, 2).to_a.inject({}) { |h, (k, v)| h.update(k=>v) }

Injecting hash may be your idea of decadence, but it's too much hard
work for me. I just splat myself:

def to_hsh
Hash[*self]

> end
> end

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Trans

10/29/2006 9:52:00 AM

0


Logan Capaldo wrote:
> Alternative decandence:
>
> class Array
> def to_hsh
> require 'enumerator'
> to_enum(:each_slice, 2).to_a.inject({}) { |h, (k, v)| h.update(k=>v) }
> end
> end
>
> %w( a b c d ).to_hsh

Logan, I love you! Just the kind of example I've been looking for
--another potential use for a #to_h.

See: http://rcrchive.net/rc....

T.

Robert Klemme

10/29/2006 11:23:00 AM

0

Logan Capaldo wrote:
> Alternative decandence:
>
> class Array
> def to_hsh
> require 'enumerator'
> to_enum(:each_slice, 2).to_a.inject({}) { |h, (k, v)| h.update(k=>v) }
> end
> end

This seems a bit inefficient. If you write a method then I'd prefer

require 'enumerator'
module Enumerable
def to_hash
h = {}
to_enum(:each_slice, 2).each {|k,v| h[k]=v}
h
end
end

irb(main):021:0> %w( a b c d ).to_hash
=> {"a"=>"b", "c"=>"d"}
irb(main):022:0> (1..10).to_hash
=> {5=>6, 1=>2, 7=>8, 3=>4, 9=>10}

Kind regards

robert

David Vallner

10/29/2006 1:13:00 PM

0

Robert Klemme wrote:
> Logan Capaldo wrote:
>> Alternative decandence:
>>
>> class Array
>> def to_hsh
>> require 'enumerator'
>> to_enum(:each_slice, 2).to_a.inject({}) { |h, (k, v)|
>> h.update(k=>v) }
>> end
>> end
>
> This seems a bit inefficient. If you write a method then I'd prefer
>
> require 'enumerator'
> module Enumerable
> def to_hash
> h = {}
> to_enum(:each_slice, 2).each {|k,v| h[k]=v}
> h
> end
> end
>
> irb(main):021:0> %w( a b c d ).to_hash
> => {"a"=>"b", "c"=>"d"}
> irb(main):022:0> (1..10).to_hash
> => {5=>6, 1=>2, 7=>8, 3=>4, 9=>10}
>

Well, if Ruby had clean blocks, the method using #inject might be more
efficient - I don't know how expensive the implicit hash construction
for method parameters is. Smalltalk muscle memory working there?

David Vallner

Logan Capaldo

10/29/2006 4:41:00 PM

0

On Sun, Oct 29, 2006 at 03:56:34PM +0900, Joel VanderWerf wrote:
> Logan Capaldo wrote:
> ...
> >Alternative decandence:
> >class Array
> > def to_hsh
> > require 'enumerator'
> > to_enum(:each_slice, 2).to_a.inject({}) { |h, (k, v)| h.update(k=>v) }
>
> Injecting hash may be your idea of decadence, but it's too much hard
> work for me. I just splat myself:
>
> def to_hsh
> Hash[*self]
>
See when I usually write this code it's because it's coming out of a
#zip, and then you must flatten it to use the splat, and flatten is too
greedy if you nested arrays. So it was force of habit. In this case you
are absolutely correct.
> > end
> >end
>
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Louis J Scoras

10/29/2006 4:57:00 PM

0

On 10/29/06, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> Ultra decadent:
>
> irb(main):017:0> class Array; def -@; Hash[*self]; end; end
> => nil
> irb(main):018:0> -%w{ a b c d }
> => {"a"=>"b", "c"=>"d"}

Nice, but one little edit:

%s/decadent/obfuscated/g

Still neat though.


--
Lou.