Hello
Is there a method similar to Array.delete which returns the modified
array instead of modifing the array by itself?
Why aren’s there two methods:
Array.delete
and
Array.delete!
???
Thanx for any hint!
Eric.
Hello
Is there a method similar to Array.delete which returns the modified
array instead of modifing the array by itself?
Why aren’s there two methods:
Array.delete
and
Array.delete!
???
Thanx for any hint!
Eric.
“E.-R. Bruecklmeier” news01@eric-bruecklmeier.de schrieb im Newsbeitrag
news:400559B5.8020908@eric-bruecklmeier.de…
Hello
Is there a method similar to Array.delete which returns the modified
array instead of modifing the array by itself?
irb(main):009:0> a=%w{a b c}
=> [“a”, “b”, “c”]
irb(main):010:0> a.reject{|s|s>“b”}
=> [“a”, “b”]
irb(main):011:0> a
=> [“a”, “b”, “c”]
Why aren’s there two methods:
Array.delete
and
Array.delete!
Historical reasons, I guess.
Regards
robert
E.-R. Bruecklmeier wrote:
Is there a method similar to Array.delete which returns the modified
array instead of modifing the array by itself?Why aren’s there two methods:
Array.delete
and
Array.delete!
???
if Array.delete was duplicating the array before removing the value, it
would be a huge performance killer for big arrays.
you can simply do:
oldArray = myArray.dup
myArray.delete(obj)
emmanuel
Moin!
Sorry for bringing up such an old topic, but I stumbled over the same issue today.
E.-R. Bruecklmeier wrote on 01/14/04: > Why aren's there two methods: Array.delete and Array.delete! ???
I don't see any reason, why the name of the method is Array.delete instead of Array.delete!, since Array.delete modifies the Array and returns the removed object. Any chance that that is changed, for the sake of the principle of least surprise?
Johannes
--
http://www.rubyforen.de - Das deutsche Rubyforum
“Emmanuel Touzery” emmanuel.touzery@wanadoo.fr schrieb im Newsbeitrag
news:40055BD8.90405@wanadoo.fr…
E.-R. Bruecklmeier wrote:
Is there a method similar to Array.delete which returns the modified
array instead of modifing the array by itself?Why aren’s there two methods:
Array.delete
and
Array.delete!
???
if Array.delete was duplicating the array before removing the value, it
would be a huge performance killer for big arrays.
That’s why the OP asked why there were not two methods: delete! (inplace
deletion) and delete (copy without the deleted elements).
Regards
robert
Robert Klemme schrieb:
Is there a method similar to Array.delete which returns the modified
array instead of modifing the array by itself?
irb(main):010:0> a.reject{|s|s>“b”}
=> [“a”, “b”]
irb(main):011:0> a
=> [“a”, “b”, “c”]
sure, that works. I did is this way:
(a.collect {|x| x if x != “c”}).compact
but delete and delete! would be nicer!
Thanks anyway!
Eric.
the reason is that matz thinks delete (and String#concat and many
others) have modification implicit. the bang (!) is to distinguish
dangerous methods from safe ones with the same name.
I don't really agree but this has been brought up many times, see even
rcrchive for a recent RCR
il Mon, 26 Jul 2004 01:26:08 +0900, Johannes Barre <igel@igels.net> ha scritto::
Moin!
Sorry for bringing up such an old topic, but I stumbled over the same
issue today.E.-R. Bruecklmeier wrote on 01/14/04: > >> Why aren's there two methods: Array.delete and Array.delete! ???
I don't see any reason, why the name of the method is Array.delete
instead of Array.delete!, since Array.delete modifies the Array and
returns the removed object. Any chance that that is changed, for the
sake of the principle of least surprise?
“E.-R. Bruecklmeier” news01@eric-bruecklmeier.de schrieb im Newsbeitrag
news:400567BA.3070903@eric-bruecklmeier.de…
Robert Klemme schrieb:
Is there a method similar to Array.delete which returns the modified
array instead of modifing the array by itself?irb(main):010:0> a.reject{|s|s>“b”}
=> [“a”, “b”]
irb(main):011:0> a
=> [“a”, “b”, “c”]sure, that works. I did is this way:
(a.collect {|x| x if x != “c”}).compact
but delete and delete! would be nicer!
Here are more versions, slightly more efficient if the array is large and
only a small portion is selected:
irb(main):008:0> a=%w{a b c}
=> [“a”, “b”, “c”]
irb(main):009:0> a.inject(){|arr,s| arr<“b”; arr}
=> [“a”, “b”]
irb(main):010:0> a.select{|s| s<=“b”}
=> [“a”, “b”]
Personally I prefer the select variant because it clearly explains what’s
going on. I
Ruby is so much fun - especially since the advent of “inject”.
Cheers
robert
I think the theory (which I support) goes: ‘delete’ sounds like a
destructive method, just like ‘concat’.
#delete is just a special case of #reject!
class Array
def delete(obj)
reject! { |e| e == obj }
end
end
So use the non-destructive version yourself to do a non-destructive
deletion. Instead of that hideous code you have above, use
a.reject { |e| e == ‘c’ }
Cheers,
Gavin
On Thursday, January 15, 2004, 3:02:41 AM, E.-R. wrote:
Robert Klemme schrieb:
Is there a method similar to Array.delete which returns the modified
array instead of modifing the array by itself?
irb(main):010:0> a.reject{|s|s>“b”}
=> [“a”, “b”]
irb(main):011:0> a
=> [“a”, “b”, “c”]
sure, that works. I did is this way:
(a.collect {|x| x if x != “c”}).compact
but delete and delete! would be nicer!