Permalink
Browse files

Added Array#prepend as an alias for Array#unshift and Array#append as…

… an alias for Array#<< [DHH]
  • Loading branch information...
dhh committed Aug 15, 2011
1 parent bb4e790 commit 9482554f31f3ac7f941e6239890c60fcc01975e1
View
@@ -1,5 +1,7 @@
*Rails 3.2.0 (unreleased)*
* Added Array#prepend as an alias for Array#unshift and Array#append as an alias for Array#<< [DHH]
* Removed support for using Module#delegate to delegate to non-public methods [Jon Leighton]
* The definition of blank string for Ruby 1.9 has been extended to Unicode whitespace.
@@ -5,3 +5,4 @@
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/array/grouping'
require 'active_support/core_ext/array/random_access'
require 'active_support/core_ext/array/prepend_and_append'
@@ -0,0 +1,7 @@
class Array
# The human way of thinking about adding stuff to the end of a list is with append
alias_method :append, :<<

This comment has been minimized.

Show comment
Hide comment
@pokonski

pokonski Dec 8, 2011

Contributor

Double sad face :<<

@pokonski

pokonski Dec 8, 2011

Contributor

Double sad face :<<

# The human way of thinking about adding stuff to the beginning of a list is with prepend
alias_method :prepend, :unshift
end
@@ -465,3 +465,13 @@ def test_wrap_does_not_complain_if_to_ary_does_not_return_an_array
assert_equal DoubtfulToAry.new.to_ary, Array.wrap(DoubtfulToAry.new)
end
end
class ArrayPrependAppendTest < Test::Unit::TestCase
def test_append
assert_equal [1, 2], [1].append(2)
end
def test_prepend
assert_equal [2, 1], [1].prepend(2)
end
end

17 comments on commit 9482554

@fxn

This comment has been minimized.

Show comment
Hide comment
@fxn

fxn Aug 15, 2011

Member

Please remember the AS guide!

Member

fxn replied Aug 15, 2011

Please remember the AS guide!

@epinault

This comment has been minimized.

Show comment
Hide comment
@epinault

epinault Aug 15, 2011

append breaks the whole idea of writing duck typed code using << and reading it that way...

epinault replied Aug 15, 2011

append breaks the whole idea of writing duck typed code using << and reading it that way...

@danielgrippi

This comment has been minimized.

Show comment
Hide comment
@danielgrippi

danielgrippi replied Aug 15, 2011

wooo

@michaelkoper

This comment has been minimized.

Show comment
Hide comment
@michaelkoper

michaelkoper Aug 15, 2011

Nice idea, Never liked the name unshift but wouldnt it be better to alias_method :append, :push instead of << ??

michaelkoper replied Aug 15, 2011

Nice idea, Never liked the name unshift but wouldnt it be better to alias_method :append, :push instead of << ??

@ernie

This comment has been minimized.

Show comment
Hide comment
@ernie

ernie Aug 15, 2011

Contributor

This is a nice idea, and I agree with the logic behind the wording, but this sort of thing doesn't belong in ActiveSupport, in my opinion. It doesn't add significant syntactical sugar, no matter how much I dislike the Perl cruft of "unshift". I'd sooner see a change like this in Ruby core, maybe 2.x.

I know that arguments made from code are the most convincing, but this is one that is readily made by the comments right before the aliases themselves.

Also, in before the suggestion that we need Array#unprepend and Array#unappend, or similar. :)

Contributor

ernie replied Aug 15, 2011

This is a nice idea, and I agree with the logic behind the wording, but this sort of thing doesn't belong in ActiveSupport, in my opinion. It doesn't add significant syntactical sugar, no matter how much I dislike the Perl cruft of "unshift". I'd sooner see a change like this in Ruby core, maybe 2.x.

I know that arguments made from code are the most convincing, but this is one that is readily made by the comments right before the aliases themselves.

Also, in before the suggestion that we need Array#unprepend and Array#unappend, or similar. :)

@goncalossilva

This comment has been minimized.

Show comment
Hide comment
@goncalossilva

goncalossilva Aug 16, 2011

Contributor

Great to have prepend. However, I agree with @epinault-ttc when it comes to append.

Contributor

goncalossilva replied Aug 16, 2011

Great to have prepend. However, I agree with @epinault-ttc when it comes to append.

@augustl

This comment has been minimized.

Show comment
Hide comment
@augustl

augustl Aug 16, 2011

As long as people don't start using these in generic, portable library code, we're all good.

augustl replied Aug 16, 2011

As long as people don't start using these in generic, portable library code, we're all good.

@vijaydev

This comment has been minimized.

Show comment
Hide comment
@vijaydev

vijaydev Aug 16, 2011

Member

Added in AS guide: rails/docrails@bd4800d

Member

vijaydev replied Aug 16, 2011

Added in AS guide: rails/docrails@bd4800d

@yfeldblum

This comment has been minimized.

Show comment
Hide comment
@yfeldblum

yfeldblum Sep 10, 2011

I'd DRYly vote for naming the file

active_support/core_ext/array/pend.rb

yfeldblum replied Sep 10, 2011

I'd DRYly vote for naming the file

active_support/core_ext/array/pend.rb
@luckydev

This comment has been minimized.

Show comment
Hide comment
@luckydev

luckydev Sep 24, 2011

Contributor

awesome!

Contributor

luckydev replied Sep 24, 2011

awesome!

@peterc

This comment has been minimized.

Show comment
Hide comment
@peterc

peterc Dec 7, 2011

Contributor

The human way of thinking about an assertion for this would be something like:

[1].prepend(2).must_equal [2, 1]
Contributor

peterc replied Dec 7, 2011

The human way of thinking about an assertion for this would be something like:

[1].prepend(2).must_equal [2, 1]
@dhh

This comment has been minimized.

Show comment
Hide comment
@dhh

dhh Dec 8, 2011

Member

@peterc, letting testing concerns seep into the core object like this gravely offends my sense of aesthetics. Big part of the reason why I dislike rspec syntax so much.

Member

dhh replied Dec 8, 2011

@peterc, letting testing concerns seep into the core object like this gravely offends my sense of aesthetics. Big part of the reason why I dislike rspec syntax so much.

@epinault

This comment has been minimized.

Show comment
Hide comment
@epinault

epinault Dec 8, 2011

+1 with @peterc. @dhh Even if you dislike it, there are plenty of people who like it. I see more specs tests, than unit test in gems lately (Wether I like it or not). I would love to see Rails being more open in supporting more framework at generation time, the way Padrino does it. Let the people choose what works best for them or based on their needs/constraints.

epinault replied Dec 8, 2011

+1 with @peterc. @dhh Even if you dislike it, there are plenty of people who like it. I see more specs tests, than unit test in gems lately (Wether I like it or not). I would love to see Rails being more open in supporting more framework at generation time, the way Padrino does it. Let the people choose what works best for them or based on their needs/constraints.

@peterc

This comment has been minimized.

Show comment
Hide comment
@peterc

peterc Dec 8, 2011

Contributor

@dhh: I was really letting a little British sarcasm slip into the thread ;-) I understand your motivations for using Test::Unit and I'm quite a fan of it too (although MiniTest::Spec is gradually winning me over).

What I really take umbrage with is jamming aliases into core classes like Array. This is hardly a new practice in ActiveSupport, of course, but it's annoying when people write "Rails plugins" that, in reality, don't need to be Rails only at all.. they've just used stuff that's in ActiveSupport that one doesn't necessarily want polluting one's non-Rails apps. I appreciate that this is a reasonably old argument but I can still joke about it ;-)

@epinault-ttc: There's probably more that could be done but I think Rails 3 does pretty well with RSpec. Despite DHH's preference and the default settings, RSpec seems to be the most popular testing library used against Rails apps and it's pretty trivial to get running with it.

Contributor

peterc replied Dec 8, 2011

@dhh: I was really letting a little British sarcasm slip into the thread ;-) I understand your motivations for using Test::Unit and I'm quite a fan of it too (although MiniTest::Spec is gradually winning me over).

What I really take umbrage with is jamming aliases into core classes like Array. This is hardly a new practice in ActiveSupport, of course, but it's annoying when people write "Rails plugins" that, in reality, don't need to be Rails only at all.. they've just used stuff that's in ActiveSupport that one doesn't necessarily want polluting one's non-Rails apps. I appreciate that this is a reasonably old argument but I can still joke about it ;-)

@epinault-ttc: There's probably more that could be done but I think Rails 3 does pretty well with RSpec. Despite DHH's preference and the default settings, RSpec seems to be the most popular testing library used against Rails apps and it's pretty trivial to get running with it.

@epinault

This comment has been minimized.

Show comment
Hide comment
@epinault

epinault Dec 8, 2011

@peterc: Totally agree with you. I actually meant more than Rspec in terms of support though ;) DM, Sequel, etc...

epinault replied Dec 8, 2011

@peterc: Totally agree with you. I actually meant more than Rspec in terms of support though ;) DM, Sequel, etc...

@jgaskins

This comment has been minimized.

Show comment
Hide comment
@jgaskins

jgaskins Dec 8, 2011

Contributor

@dhh Isn't making it easier for the person reading it the very definition of aesthetic? I don't want to get into an testing-framework debate, but I'm not sure you chose the right word there.

Contributor

jgaskins replied Dec 8, 2011

@dhh Isn't making it easier for the person reading it the very definition of aesthetic? I don't want to get into an testing-framework debate, but I'm not sure you chose the right word there.

@yfeldblum

This comment has been minimized.

Show comment
Hide comment
@yfeldblum

yfeldblum Dec 8, 2011

There are multiple aesthetics at play: readability as English and purity are two aesthetics that sometimes seem to clash.

yfeldblum replied Dec 8, 2011

There are multiple aesthetics at play: readability as English and purity are two aesthetics that sometimes seem to clash.

Please sign in to comment.