Hash#values_at

A

Ara.T.Howard

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
===============================================================================
 
Y

Yukihiro Matsumoto

Hi,

In message "Hash#values_at"
|
|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
end
end

matz.
 
A

Ara.T.Howard

Date: Mon, 16 Feb 2004 15:33:59 +0900
From: Yukihiro Matsumoto <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: Hash#values_at

Hi,

In message "Hash#values_at"
|
|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
end
end

matz.


ah. also

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

thanks.

-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
===============================================================================
 

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

Similar Threads

narray on windows? 1
Class::name 0
idiot's guide to druby using ssh tunnels 4
parent of TrueClass, FalseClass 9
drb with udp 1
pthread 2
pretty exceptions 0
to_yaml and options hash 0

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top