Permalink
Browse files

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

… an alias for Array#<< [DHH]
  • Loading branch information...
1 parent bb4e790 commit 9482554f31f3ac7f941e6239890c60fcc01975e1 @dhh dhh committed Aug 15, 2011
@@ -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, :<<
@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
Member
fxn commented on 9482554 Aug 15, 2011

Please remember the AS guide!

@epinault

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

@michaelkoper

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

@ernie
Contributor
ernie commented on 9482554 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
Contributor

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

@augustl

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

@vijaydev
Member

Added in AS guide: rails/docrails@bd4800d

@yfeldblum

I'd DRYly vote for naming the file

active_support/core_ext/array/pend.rb
@luckydev
Contributor

awesome!

@peterc
Contributor
peterc commented on 9482554 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
Member
dhh commented on 9482554 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

+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
Contributor
peterc commented on 9482554 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

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

@jgaskins
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.

@yfeldblum

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

Please sign in to comment.