Hash#slice

A

ara howard

is this consistent with other peoples stdlib hacks?

class Hash
def slice *keys, &block
if block
each do |key, val|
boolean = block.call(key, val)
keys << key if boolean
end
end
hash = self
keys.inject({}){|returned, key| returned.update key => hash[key]}
end
end


??


a @ http://codeforpeople.com/
 
T

Trans

is this consistent with other peoples stdlib hacks?

class Hash
def slice *keys, &block
if block
each do |key, val|
boolean = block.call(key, val)
keys << key if boolean
end
end
hash = self
keys.inject({}){|returned, key| returned.update key => hash[key]}
end
end

??

Let see.... Facets:

class Hash

# Returns a new hash with only the given keys.

def slice(*keep_keys)
h = {}
keep_keys.each do |key|
h[key] = fetch(key)
end
h
end

# Replaces hash with a new hash having only the given keys.
# This return the hash of keys removed.

def slice!(*keep_keys)
removed = except(*keep_keys)
replace(slice(*keep_keys))
removed
end

end

T.
 
A

ara.t.howard

Let see.... Facets:

class Hash

# Returns a new hash with only the given keys.

def slice(*keep_keys)
h = {}
keep_keys.each do |key|
h[key] = fetch(key)
end
h
end

# Replaces hash with a new hash having only the given keys.
# This return the hash of keys removed.

def slice!(*keep_keys)
removed = except(*keep_keys)
replace(slice(*keep_keys))
removed
end

end

okay, you should add the block form ;-)

headers.slice{|k,| k =~ %r/^HTTP_/}

handy.....

cheers.

a @ http://codeforpeople.com/
 
R

Robert Dober

Let see.... Facets:

class Hash

# Returns a new hash with only the given keys.

def slice(*keep_keys)
h = {}
keep_keys.each do |key|
h[key] = fetch(key)
end
h
end

# Replaces hash with a new hash having only the given keys.
# This return the hash of keys removed.

def slice!(*keep_keys)
removed = except(*keep_keys)
replace(slice(*keep_keys))
removed
end

end

okay, you should add the block form ;-)

headers.slice{|k,| k =~ %r/^HTTP_/}

handy.....

cheers.



a @ http://codeforpeople.com/
Hmm maybe it is a more consistent approach not to add a block to slice.

FWIAC, I use hselect, kselect and vselect, the simplified code
goes like this (1) of course
def kselect &blk
hselect{ |k,| blk.call(k) }
end
def vselect &blk
hselect{ |_,v| blk.call(v) }
end
def hselect &blk
#almost Tom's code of course
end

You might write
h.slice(*[*1..10]){|k,v| whatever k, v }
which indeed I like

I would write
h.slice(*[*1..10]).hselect{|k,v| whatever k, v}
which indeed I like less, *but* I dislike the inconsistency of #select now.

The ideal solution might be to add a block to Enumerator#select too,
what about that?

Cheers
Robert


(1) simplified because the magic dot notation implementing proxy is of
no interest here.
 
T

Trans

On Apr 12, 2008, at 5:12 PM, Trans wrote:
Let see.... Facets:
class Hash
# Returns a new hash with only the given keys.
def slice(*keep_keys)
h = {}
keep_keys.each do |key|
h[key] = fetch(key)
end
h
end
# Replaces hash with a new hash having only the given keys.
# This return the hash of keys removed.
def slice!(*keep_keys)
removed = except(*keep_keys)
replace(slice(*keep_keys))
removed
end
end
okay, you should add the block form ;-)
headers.slice{|k,| k =~ %r/^HTTP_/}


a @http://codeforpeople.com/

Hmm maybe it is a more consistent approach not to add a block to slice.

FWIAC, I use hselect, kselect and vselect, the simplified code
goes like this (1) of course
def kselect &blk
hselect{ |k,| blk.call(k) }
end

def vselect &blk
hselect{ |_,v| blk.call(v) }
end
def hselect &blk
#almost Tom's code of course
end

Why not?

hash.keys.select
hash.values.select
You might write
h.slice(*[*1..10]){|k,v| whatever k, v }
which indeed I like

I would write
h.slice(*[*1..10]).hselect{|k,v| whatever k, v}
which indeed I like less, *but* I dislike the inconsistency of #select now.

The ideal solution might be to add a block to Enumerator#select too,
what about that?

Hmm... I don't understand Enumerator#select comes from
Enumerable#select and already takes a block.

T.
 
R

Robert Dober

On Apr 12, 2008, at 5:12 PM, Trans wrote:
Let see.... Facets:
class Hash
# Returns a new hash with only the given keys.
def slice(*keep_keys)
h = {}
keep_keys.each do |key|
h[key] = fetch(key)
end
h
end
# Replaces hash with a new hash having only the given keys.
# This return the hash of keys removed.
def slice!(*keep_keys)
removed = except(*keep_keys)
replace(slice(*keep_keys))
removed
end

okay, you should add the block form ;-)
headers.slice{|k,| k =~ %r/^HTTP_/}


a @http://codeforpeople.com/

Hmm maybe it is a more consistent approach not to add a block to slice.

FWIAC, I use hselect, kselect and vselect, the simplified code
goes like this (1) of course
def kselect &blk
hselect{ |k,| blk.call(k) }
end

def vselect &blk
hselect{ |_,v| blk.call(v) }
end
def hselect &blk
#almost Tom's code of course
end

Why not?

hash.keys.select
hash.values.select
We were talking about getting sliced hashes not sliced key or value
arrays. Maybe in the future there will be a saying
"the best thing since the invention of sliced hashes by Ara T. Howard"
the real inventor gets always forgotten sorry for Facets I can not
change future history ;)
You might write
h.slice(*[*1..10]){|k,v| whatever k, v }
which indeed I like

I would write
h.slice(*[*1..10]).hselect{|k,v| whatever k, v}
which indeed I like less, *but* I dislike the inconsistency of #select now.

The ideal solution might be to add a block to Enumerator#select too,
what about that?

Hmm... I don't understand Enumerator#select comes from
Enumerable#select and already takes a block.
Stupid me *again* I meant Array#slice. I feel that my brain is
terribly disconnected from the keyboard, (I believe that Scotty was
right when talking into the mouse LOL). Well proofreading my posts
would be an option though :).

I will express myself in Ruby

assert_nothing_raised ("You see it would be nice that Array#slice and
Hash#slice had (almost) the same interface") do
[*1..100].slice(1..10){ |x| x.zero? }
end


Sorry
R.
 
A

ara.t.howard

Hmm... I don't understand Enumerator#select comes from
Enumerable#select and already takes a block.
Stupid me *again* I meant Array#slice. I feel that my brain is
terribly disconnected from the keyboard, (I believe that Scotty was
right when talking into the mouse LOL). Well proofreading my posts
would be an option though :).

I will express myself in Ruby

assert_nothing_raised ("You see it would be nice that Array#slice and
Hash#slice had (almost) the same interface") do
[*1..100].slice(1..10){ |x| x.zero? }
end


now that does make sense ;-)

a @ http://codeforpeople.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top