New, but this seems odd

Ok, granted that I'm pretty new to ruby, but this seems odd to me:

irb(main):001:0> x = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> x.delete 2
=> 2
irb(main):003:0> x
=> [1, 3]

With this behavior, wouldn't "delete!" be a more appropriate name?

This came up a few days ago:

9857c220286cc01c05bd3fc163a7654a@ruby-forum.com

David A. Black's answer covers it nicely, I think.

Dan

···

On Wed, Jul 07, 2010 at 10:01:35PM +0900, Andrew Wagner wrote:

Ok, granted that I'm pretty new to ruby, but this seems odd to me:

irb(main):001:0> x = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> x.delete 2
=> 2
irb(main):003:0> x
=> [1, 3]

With this behavior, wouldn't "delete!" be a more appropriate name?

--
Daniel Bye
                                                                     _
                                              ASCII ribbon campaign ( )
                                         - against HTML, vCards and X
                                - proprietary attachments in e-mail / \

Read http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist

Is there some kind of interpretation of "delete" that doesn't imply the receiving object will be changed?

Compare Hash#update and Hash#merge, for example. Hash#update changes the receiving hash, which Hash#merge creates a new hash with extra/overwritten keys from the argument hash. It shouldn't be too surprising that Hash#merge! is an alias of Hash#update.

-Rob

Rob Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.com/

···

On Jul 7, 2010, at 9:01 AM, Andrew Wagner wrote:

Ok, granted that I'm pretty new to ruby, but this seems odd to me:

irb(main):001:0> x = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> x.delete 2
=> 2
irb(main):003:0> x
=> [1, 3]

With this behavior, wouldn't "delete!" be a more appropriate name?

After some reflection, I can certainly see how you would expect a
bang! there... when working with sets, you often want to manipulate a
set to form a different one.

guests = ['joe@person.test', 'jane@person.test', 'jules@person.test',
'me']
invitations_to_send = guests.delete('me')

and in fact there are enumerable operators that work that way...

guest_emails = ['joe@person.test', 'jane@person.test',
'jules@person.test', 'me']
guests = guest_emails.map{|email| Person.find_by_email( email ) }

whereas

guest_emails.map!{|email| Person.find_by_email( email ) }

So, yes, inconsistent. Sorry, not much that can be done now... :slight_smile:

The way to do this would be ...

invitations_to_send = guests - ['me']

or just overwrite delete for your app... :slight_smile:

jw

···

On Jul 7, 8:01 am, Andrew Wagner <wagner.and...@gmail.com> wrote:

With this behavior, wouldn't "delete!" be a more appropriate name?

On Jul 7, 8:25 am, Rob Biedenharn <R...@AgileConsultingLLC.com> wrote:

Is there some kind of interpretation of "delete" that doesn't imply
the receiving object will be changed?

----------
Johnathon Wright
Web Application Consultant

http://www.mustmodify.com/

That article is helpful, thanks. Somehow I had exactly that idea in my mind,
that the ! implies side effects. I was expecting to get back a new array [1,
3], and that there would be a delete! method defined with the below
behavior. That seems more consistent to me than some vague description that
! means "dangerous" for some value of "dangerous". I guess I still have
plenty to learn. Thanks for the link!

···

On Wed, Jul 7, 2010 at 9:25 AM, Rob Biedenharn <Rob@agileconsultingllc.com>wrote:

On Jul 7, 2010, at 9:01 AM, Andrew Wagner wrote:

Ok, granted that I'm pretty new to ruby, but this seems odd to me:

irb(main):001:0> x = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> x.delete 2
=> 2
irb(main):003:0> x
=> [1, 3]

With this behavior, wouldn't "delete!" be a more appropriate name?

Read
http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist

Is there some kind of interpretation of "delete" that doesn't imply the
receiving object will be changed?

Compare Hash#update and Hash#merge, for example. Hash#update changes the
receiving hash, which Hash#merge creates a new hash with extra/overwritten
keys from the argument hash. It shouldn't be too surprising that
Hash#merge! is an alias of Hash#update.

-Rob

Rob Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.com/

x = "123"
x.delete '2' # => "13"
x # => "123"

···

On Wed, Jul 7, 2010 at 8:25 AM, Rob Biedenharn <Rob@agileconsultingllc.com>wrote:

On Jul 7, 2010, at 9:01 AM, Andrew Wagner wrote:

Ok, granted that I'm pretty new to ruby, but this seems odd to me:

irb(main):001:0> x = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> x.delete 2
=> 2
irb(main):003:0> x
=> [1, 3]

With this behavior, wouldn't "delete!" be a more appropriate name?

Read
http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist

Is there some kind of interpretation of "delete" that doesn't imply the
receiving object will be changed?