Array#at(selector,...)

T

trans. (T. Onoma)

Is there good reason to have two different methods for this?

array.at(index)
array.values_at(selector,... )

And then

array.delete_at(index)

Can't this take selector instead?

I imagine there are other methods related as well.

T.
 
T

trans. (T. Onoma)

On Tuesday 14 December 2004 10:17 am, trans. (T. Onoma) wrote:
| Is there good reason to have two different methods for this?
|
| array.at(index)
| array.values_at(selector,... )

To help answer my own question. A problem arises with output on single element
results:

a = [1,2,3,4,5]
a.at(0) #=> 1
a.values_at(0) #=> [1]

The only alternatives I see for unification would be to have #at take an Array
or ignore nil:

a.at([0]) #=> [1]
a.at(0,nil) #=> [1]

But barring that (or a better solution), my REAL PROBLEM boils down to how
best to implement a method related to this but that doesn't have the same
difficulty. For example (completely contrived):

class Array
def part(n=nil)
if n
values_at(0..n)
else
at(0)
end
end
end

Is it better to have two methods instead --an #at_part and a #values_at_part?
Personally I prefer the single method but that seems to go against the
at/values_at grain. So that's why I'm inquiring into it.

| And then
|
| array.delete_at(index)
|
| Can't this take selector instead?

This still seems reasonable.

T.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Array#at(selector,...)"

|Is there good reason to have two different methods for this?
|
| array.at(index)
| array.values_at(selector,... )

Return value, the latter returns an array of specified values.

|And then
|
| array.delete_at(index)
|
|Can't this take selector instead?

The change would cause incompatibility in return values.

matz.
 
T

trans. (T. Onoma)

Hi,

Array#at(selector,...)"
|
| on Wed, 15 Dec 2004 00:17:25 +0900, "trans. (T. Onoma)"
| |Is there good reason to have two different methods for this?
| |
| | array.at(index)
| | array.values_at(selector,... )
|
| Return value, the latter returns an array of specified values.
|
| |And then
| |
| | array.delete_at(index)
| |
| |Can't this take selector instead?
|
| The change would cause incompatibility in return values.

Ah I see, same difficulty. Thank you.

So then, I am under the impression that the two method approach is best --a
#delete_values_at for instance.

T.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Array#at(selector,...)"

|So then, I am under the impression that the two method approach is best --a
|#delete_values_at for instance.

If you want both functionality.

matz.
 

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
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top