Hash#values_at


(Ara.T.Howard) #1

what is the rational behind this?

({}.values_at :a).empty? #=> false

i would expect

({}.values_at :a) #=> nil

or

({}.values_at :a) #=> []

but not the actual

({}.values_at :a) #=> [nil]

there must be some reason for this, but i cannot see why returning an ‘array
full of nothing’ is preferable to returning an ‘emtpy array’… is there some
way of thinking about this that will make it seem less suprising?

-a

···

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: http://www.ngdc.noaa.gov/stp/
TRY :: for l in ruby perl;do $l -e “print “\x3a\x2d\x29\x0a””;done
===============================================================================


(Yukihiro Matsumoto) #2

Hi,

···

In message “Hash#values_at” on 04/02/16, “Ara.T.Howard” Ara.T.Howard@noaa.gov writes:

what is the rational behind this?

({}.values_at :a).empty? #=> false

i would expect

({}.values_at :a) #=> nil

or

({}.values_at :a) #=> []

but not the actual

({}.values_at :a) #=> [nil]

there must be some reason for this, but i cannot see why returning an ‘array
full of nothing’ is preferable to returning an ‘emtpy array’… is there some
way of thinking about this that will make it seem less suprising?

def values_at(*args)
args.collect do |i|
self[i]
end
end

						matz.

(Ara.T.Howard) #3

ah. also

irb(main):001:0> {:k=>:v}.values_at
=> []

thanks.

-a

···

On Mon, 16 Feb 2004, Yukihiro Matsumoto wrote:

Date: Mon, 16 Feb 2004 15:33:59 +0900
From: Yukihiro Matsumoto matz@ruby-lang.org
Newsgroups: comp.lang.ruby
Subject: Re: Hash#values_at

Hi,

In message “Hash#values_at” > on 04/02/16, “Ara.T.Howard” Ara.T.Howard@noaa.gov writes:

what is the rational behind this?

({}.values_at :a).empty? #=> false

i would expect

({}.values_at :a) #=> nil

or

({}.values_at :a) #=> []

but not the actual

({}.values_at :a) #=> [nil]

there must be some reason for this, but i cannot see why returning an ‘array
full of nothing’ is preferable to returning an ‘emtpy array’… is there some
way of thinking about this that will make it seem less suprising?

def values_at(*args)
args.collect do |i|
self[i]
end
end

  					matz.

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: http://www.ngdc.noaa.gov/stp/
TRY :: for l in ruby perl;do $l -e “print “\x3a\x2d\x29\x0a””;done
===============================================================================