Forum: Ruby can it be shorter?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6c382202075fdcd2522d34633d7ecd9b?d=identicon&s=25 Dorren (Guest)
on 2007-01-24 23:45
(Received via mailing list)
doing access control on rails controller,

------ I have this input --------------
hash = {"index" => "list",
          ["edit", "update"] => "manage_one",
          ["new", "create", "destroy"] => "manage_all"}

------- I want this output ----------
{"index"=>"list",
 "edit"=>"manage_one",
 "update"=>"manage_one",
 "new"=>"manage_all",
 "create"=>"manage_all",
 "destroy"=>"manage_all"
}

------ I have this code ------------
  hash = Hash[*hash.to_a.collect{|x|
                       Array === x[0] ? x[0].zip([x[1]]*x[0].size) : x
                    }.flatten]

----- I want shorter code ------

thanks.
6c382202075fdcd2522d34633d7ecd9b?d=identicon&s=25 Dorren (Guest)
on 2007-01-25 00:06
(Received via mailing list)
a little clearer than previous one.

  hash = Hash[*hash.inject([]){|arr, (k, v)|
                               arr += Array === k ? k.zip([v]* k.size)
: [k, v]
                             }.flatten]
Ea24c17719a975fb38c107a60f4b3802?d=identicon&s=25 Vincent Fourmond (Guest)
on 2007-01-25 00:15
(Received via mailing list)
Dorren wrote:
>  "update"=>"manage_one",
> ----- I want shorter code ------
What about this:

h = {}
for k,v in hash
  [k].flatten.each {|l| h[l] = v}
end
hash = h
> => {"new"=>"manage_all", "edit"=>"manage_one",
"destroy"=>"manage_all", "create"=>"manage_all", "index"=>"list",
"update"=>"manage_one"}

  Cheers,

  Vincent
Ea24c17719a975fb38c107a60f4b3802?d=identicon&s=25 Vincent Fourmond (Guest)
on 2007-01-25 00:22
(Received via mailing list)
Vincent Fourmond wrote:
> h = {}
> for k,v in hash
>   [k].flatten.each {|l| h[l] = v}
> end
> hash = h

  Actually, I've got shorter ;-):

h = {}
for k,v in hash
  [*k].each {|l| h[l] = v}
end
hash = h

  Vince
6c382202075fdcd2522d34633d7ecd9b?d=identicon&s=25 Dorren (Guest)
on 2007-01-25 00:31
(Received via mailing list)
better, thank tou.

On Jan 24, 6:14 pm, Vincent Fourmond <vincent.fourm...@9online.fr>
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2007-01-25 02:55
(Received via mailing list)
On Jan 24, 5:01 pm, "Dorren" <dorrenc...@gmail.com> wrote:
> a little clearer than previous one.
>
>   hash = Hash[*hash.inject([]){|arr, (k, v)|
>                                arr += Array === k ? k.zip([v]* k.size)
> : [k, v]
>                              }.flatten]

Hash[ *hash.map{|k,v| k=[*k]; k.zip([v]*k.size) }.flatten ]
Cf6d0868b2b4c69bac3e6f265a32b6a7?d=identicon&s=25 Daniel Martin (Guest)
on 2007-01-25 03:30
(Received via mailing list)
Vincent Fourmond <vincent.fourmond@9online.fr> writes:

>   Actually, I've got shorter ;-):
>
> h = {}
> for k,v in hash
>   [*k].each {|l| h[l] = v}
> end
> hash = h

Well, if we're golfing:

h={};hash.map{|k,v|[*k].map{|t|h[t]=v}};h
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-01-25 11:06
(Received via mailing list)
On 25.01.2007 03:29, Daniel Martin wrote:
> Well, if we're golfing:
>
> h={};hash.map{|k,v|[*k].map{|t|h[t]=v}};h

Not really shorter but I though there should be at least one solution
with #inject:

 >> hash.inject({}){|h,(k,v)| k.to_a.each {|x| h[x]=v};h}
=> {"new"=>"manage_all", "edit"=>"manage_one", "destroy"=>"manage_all",
"create"=>"manage_all", "index"=>"list", "update"=>"manage_one"}

 >> hash.inject({}){|h,(k,v)| k.each {|x| h[x]=v} rescue h[k]=v;h}
=> {"new"=>"manage_all", "edit"=>"manage_one", "destroy"=>"manage_all",
"create"=>"manage_all", "index"=>"list", "update"=>"manage_one"}

:-)

  robert
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-01-25 11:28
(Received via mailing list)
On Jan 24, 2007, at 15:20, Vincent Fourmond wrote:
> for k,v in hash
>   [*k].each {|l| h[l] = v}
> end
> hash = h

but fastest (I assume all entries worked correctly):

$ ruby bm.rb
Rehearsal --------------------------------------------------------
original              10.440000   0.010000  10.450000 ( 10.492738)
original clean        11.480000   0.040000  11.520000 ( 11.632237)
vince                  5.210000   0.010000   5.220000 (  5.264394)
william               12.760000   0.020000  12.780000 ( 12.814230)
daniel                 6.900000   0.010000   6.910000 (  6.924297)
robert inject          6.560000   0.000000   6.560000 (  6.588160)
robert inject rescue   6.080000   0.010000   6.090000 (  6.094140)
---------------------------------------------- total: 59.530000sec

                            user     system      total        real
original              10.450000   0.010000  10.460000 ( 10.467971)
original clean        11.490000   0.030000  11.520000 ( 11.600672)
vince                  5.210000   0.000000   5.210000 (  5.219863)
william               12.820000   0.010000  12.830000 ( 12.839722)
daniel                 6.930000   0.010000   6.940000 (  6.945329)
robert inject          6.580000   0.010000   6.590000 (  6.591095)
robert inject rescue   6.080000   0.000000   6.080000 (  6.117226)
6b0967f63d03e99b6c07a3f5ed224c77?d=identicon&s=25 Erik Veenstra (Guest)
on 2007-01-25 12:49
(Received via mailing list)
There's a glitch, which is not yet mentioned...

Consider this old hash: {["A", "B"]=>"C", ["B", "A"]=>"D"}

What's the value of new_hash["A"]? Is it "C", or is it "D"?

That depends on the order on which you build the new hash.
Which depends on the order in which you walk through the old
hash. Which isn't deterministic, AFAIK. Maybe it is
determinstic if you know in which order the old hash was built.
But, given _a_ hash, you simply don't know the order in which
you walk through it, so you don't know the order in which the
new hash will be built, so you don't know what the result is
going to be. Cool...

Adding a sort to the algorithm fixes this.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

----------------------------------------------------------------

 hash.collect do |k,v|
   [[k].flatten, v]
 end.sort.inject({}) do |h,(ks,v)|
   ks.each do |k|
     h[k] = v
   end
   h
 end
This topic is locked and can not be replied to.