Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added Array#second through Array#tenth as aliases for Array#[1] throu…
…gh Array#[9] [DHH]
  • Loading branch information
dhh committed Jun 17, 2008
1 parent 7650ff8 commit 22af62c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*

* Added Array#second through Array#tenth as aliases for Array#[1] through Array#[9] [DHH]

* Added test/do declaration style testing to ActiveSupport::TestCase [DHH via Jay Fields]

* Added Object#present? which is equivalent to !Object#blank? [DHH]
Expand Down
45 changes: 45 additions & 0 deletions activesupport/lib/active_support/core_ext/array/access.rb
Expand Up @@ -20,6 +20,51 @@ def from(position)
def to(position)
self[0..position]
end

# Equal to self[1]

This comment has been minimized.

Copy link
@mikehale

mikehale Nov 21, 2008

What’s with the redundant comments?

This comment has been minimized.

Copy link
@henrik

henrik Nov 21, 2008

Contributor

mikehale: The redundant comments are likely for RDoc.

def second
self[1]
end

# Equal to self[2]
def third
self[2]
end

# Equal to self[3]
def fourth
self[3]
end

# Equal to self[4]
def fifth
self[4]
end

# Equal to self[5]
def sixth
self[5]
end

# Equal to self[6]
def seventh
self[6]
end

# Equal to self[7]
def eighth
self[7]
end

# Equal to self[8]
def ninth
self[8]
end

# Equal to self[9]
def tenth
self[9]
end
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions activesupport/test/core_ext/array_ext_test.rb
Expand Up @@ -13,6 +13,20 @@ def test_to
assert_equal %w( a b c ), %w( a b c d ).to(2)
assert_equal %w( a b c d ), %w( a b c d ).to(10)
end

def test_second_through_tenth
array = (1..10).to_a

assert_equal array[1], array.second
assert_equal array[2], array.third
assert_equal array[3], array.fourth
assert_equal array[4], array.fifth
assert_equal array[5], array.sixth
assert_equal array[6], array.seventh
assert_equal array[7], array.eighth
assert_equal array[8], array.ninth
assert_equal array[9], array.tenth
end
end

class ArrayExtToParamTests < Test::Unit::TestCase
Expand Down

106 comments on commit 22af62c

@adkron
Copy link
Contributor

@adkron adkron commented on 22af62c Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point Chris. I like your answer.

@tedhenry10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rails 0.9 was better.

@boblmartens
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, am I wrong, or do you not have to use this if you don’t want to?

@clarabstract
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this patch.

@timander
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the bright side, Git lets you undo commits as if they never even happened.

@brianmario
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the love of god, you’re all spamming my github dashboard :P

Those of you who hate this patch and want to move to another framework, go ahead. There’s 10x more about to start using Rails anyway.

Those of you who love this patch (and still love rails with it, for that matter) – do you really need to justify yet another spoonful of sugar?

@dhh – keep up the good work, I personally don’t mind if there are a few more methods I can optionally use to make my life easier

@anildigital
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the kind of commit is really useful. But should it be part of framework or not, not very sure!

@johnwyles
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why stop at ten? Why not make some dynamic methods for converting numerics to flattened strings so we can do this for all elements in the array? I for one welcome our new 30+ character representational format!

It is hard not to get excited about such a thing: hella_long_array.one_million_seven_hundred_fifty_five_thousand_two_hundred_sixty_third

Error: Unclosed “” tag.

@janx
Copy link
Contributor

@janx janx commented on 22af62c Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

car, cadr, caddr, cadddr, caddddr, cadddddr ..

@elisehuard
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrislloyd’s answer nails it.

@NZKoz
Copy link
Member

@NZKoz NZKoz commented on 22af62c Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, guys, the amount of effort you’ve expended here rivals only the penis extension thread on trac.

@defunkt: You need to add a STFU button which closes a thread and tells people to take a walk, hug their loved ones, or read a book.

@matthooks
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cough ibtl cough

@neilh
Copy link

@neilh neilh commented on 22af62c Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“This is awesome!! Do you think we can add in second_to_last, third_to_last… tenth_to_last??” – lol

@listenlight
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Here is my first github comment. I love this change. It may not be “useful” in the Cartesian sense, but it does subtract some hurt. Love it!

@ELLIOTTCABLE
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not going to argue against this — this is essentially, at it’s core, Rails-ish. This is the very reason I don’t use Rails, and I know there’s many others who think like me. There’s a thin line between sugar (Ruby) and bloat (Rails), and it’s useless to complain about Rails crossing it. I agree that Chris should add a feature to GitHub just so he can close this thread and we can all get on with life /=

@spiceee
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some people should go back to Java, where such a crime would never be perpetrated.

@nikosd
Copy link

@nikosd nikosd commented on 22af62c Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@defunkt: Ok, this commit is one of the reasons I’ve come to lurve github a little more! Don’t ever put a STFU button! (Although it would be useful – to be honest :D)

@matthewrudy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.
I think this is great.
A great discussion,
a great argument.

I read the commit logs every day,
but this just makes me happy in a way I never expected.

Thankyou Github.

@jamesu
Copy link

@jamesu jamesu commented on 22af62c Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you also add the following methods:

second_from_last, third_from_last, fourth_from_last, fifth_from_last, sixth_from_last, seventh_from_last, eighth_from_last, ninth_from_last, and tenth_from_last.

@gaffo
Copy link
Contributor

@gaffo gaffo commented on 22af62c Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DHH, as you said to me earlier on Lighthouse,

“While I applaud the effort, I think this is not really don’t enough to be worth a [commit] in Rails code. If you use this a lot, I would definitely recommend making a plugin out of it, though.”
-DHH : http://rails.lighthouseapp.com/projects/8994/tickets/1303-scriptgenerate-library

NZKoz : these types of arguments are exactly why rails is a good framework, people are allowed to make comments on things they don’t like.

defunkt: keep it up.

@dcrec1
Copy link
Contributor

@dcrec1 dcrec1 commented on 22af62c Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be in a plugin, maybe with more other aliases

@trevorturk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ActiveSupport::CoreExtensions::PradiptasRolodex?

@Yardboy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got into a discussion of this over lunch one day at RubyConf. I’m reminded of George Carlin’s rant about some religious schlub not liking what he heard on the radio. “The radio has two knobs, Reverend… one turns it off, and the other changes the channel!” In other words, if you don’t like it, don’t use it.

That being said, I can’t wait for someone to add #penultimate.

@ELLIOTTCABLE
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s not about “if you don’t like it, don’t use it”. It’s about it being bloat. It’d be nice if computers could download, upload, transfer, run, and keep in memory an infinite amount of code. They can’t. There’s tons of slightly useful things you could add to Ruby that would be kinda nice, but they’d also be yet more bloat – YMB. Such things shouldn’t be added to Rails.

Wait, what am I saying? This is what Rails is all about. Thar she blows, men! Man the harpoons!

@ELLIOTTCABLE
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On another note, this epic thread has spawned it’s own meta-commentary: http://tr.im/19fd?stackoverflow

@adelcambre
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


+ # Equal to self[10]
+ def eleventh
+   self[10]
+ end

Now it goes to eleventh.

@amerine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elliottcable Comments about comments? I love the tubes sometimes.

@pervel
Copy link

@pervel pervel commented on 22af62c Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think DHH is using a Danish keyboard. Typing brackets on a Danish keyboard is actually quite cumbersome. :)

@toretore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://pastie.org/317766

Joking aside, I’ve sometimes typed users.second just because my brain thought it’d work, and yes, [ and ] are a bit of a PITA to type on non-US keyboards.

@georgi
Copy link

@georgi georgi commented on 22af62c Nov 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Programmers should just use US keyboard layout.

@unimatrixZxero
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@georgi: Yes they should.

@ozataman
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I myself use similar patches on a per-app basis, these kinds of extensions are a pure bloat to what otherwise is a great framework.

I understand that Rails is all about agile programming, but there needs to be a level of elegance and succinct quality we should not be willing to go below.

@ashchan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ieure: We programmers from China also need these especially for testers:
foo.第二, foo.第三, foo.第四…

@stalcottsmith
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a bunch of whiny bitches! I personally vote for .second_from_last or .next_to_last as well. Readability first. Keep it up.

@benatkin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was curious how many comments there were, and rather than looking through my history, I started looking for this commit in the repository. I must say that the rails directory structure is well organized. Everything’s broken down into little bits. Nice use of include.

@julesfern
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All my arrays have length of eleven, thus this change is useless to me.

@julesfern
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah and

%w(second third fourth fifth sixth seventh eighth ninth tenth).each {|n| define_method…..

/facepalm

@boof
Copy link

@boof boof commented on 22af62c Nov 21, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an initializer. -1

@henrik
Copy link
Contributor

@henrik henrik commented on 22af62c Nov 21, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Peeja, danski: When I saw this commit back in June, I liked about it that it didn’t involve metaprogramming. If it only spans 2–10, I find this more readable. Also good for RDoc.

@bryanthompson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what Zed has to say about this.

“kris- Commits like this push me closer to Merb.”

+1, and I wonder if Rails could just be split into a Rails-Core and a Rails-More, as well as a Rails-More-dumb_array_methods_and_other_gibberish

@redinger
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s get Dave Thomas to give a RailsConf Keynote about how we should fork Rails for the people that don’t want convenience methods.

@boblmartens
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this entire section of comments fascinating … the say the least.

@bak
Copy link
Contributor

@bak bak commented on 22af62c Nov 21, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only objection to this is that we already have myarray.at built into Ruby. It doesn’t start at zero if that sort of thing bothers you, it works no matter how large your array is, it’s less typing, and it doesn’t even require square brackets.

@henrik
Copy link
Contributor

@henrik henrik commented on 22af62c Nov 21, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bak: Array#at is zero-indexed.

@bak
Copy link
Contributor

@bak bak commented on 22af62c Nov 22, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

henrik: so it is. Don’t know where that blip came from.

@technohippy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi all,
I pushed ahead with this idea: http://d.hatena.ne.jp/technohippy/20081121#1227285803

@Aupajo
Copy link
Contributor

@Aupajo Aupajo commented on 22af62c Nov 22, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@technohippy

Good god.

@ZhangHanDong
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~/work/test_222>rails -v
Rails 2.2.2
~/work/test_222>cs
Loading development environment (Rails 2.2.2)

> > arr = [1,2,3,4,5,6,7,8,9,10]
> > => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> > arr.first
> > => 1
> > arr.second
> > => 2
> > arr.third
> > => 3
> > arr.fourth
> > => 4
> > arr.fifth
> > => 5
> > arr.sixth
> > NoMethodError: undefined method `sixth’ for [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:Array
> > from (irb):7
> > arr.seventh
> > NoMethodError: undefined method`seventh’ for [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:Array
> > from (irb):8
> > arr.eighth
> > NoMethodError: undefined method `eighth’ for [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:Array
> > from (irb):9
> > arr.ninth
> > NoMethodError: undefined method`ninth’ for [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:Array
> > from (irb):10
> > arr.tenth
> > NoMethodError: undefined method `tenth’ for [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:Array
> > from (irb):11

why ???

ps, cs command = ./script/console

@dhh
Copy link
Member Author

@dhh dhh commented on 22af62c Nov 23, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zhang: http://github.com/rails/rails/commit/e50530ca3ab5db53ebc74314c54b62b91b932389

@j1n6
Copy link

@j1n6 j1n6 commented on 22af62c Nov 28, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at least it’s readable

@justinko
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this. What’s wrong with more readable???

@skrat
Copy link

@skrat skrat commented on 22af62c Apr 3, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To DHH: stop programming, start writing poetry. you are too good for this Ruby thing, because replacing

dhh_dumbideas[2]

with

dhh_dumbideas.second

is the most stupid thing I’ve ever seen. It surely makes the code more readable as you have to read the word instead of a number. thumbs up , just please, don’t become a Ruby core developer

@skrat
Copy link

@skrat skrat commented on 22af62c Apr 3, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry: dhh_dumbideas1

@heycarsten
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skrat, I think you want dhh_dumbideas1 for the second item.

@heycarsten
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, textile.

@mihael
Copy link

@mihael mihael commented on 22af62c Apr 4, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bla.blunth … did You all see http://kitschmaster.com. it is still built with Rails. I still do not know why, though. Rails is changing faster than my sons teeth are growing. I am continually being challenged to read fresh mostly unusable code. Is this a lack of real communication between all You Souls?

Does any of You guys think a blink about Ruby 4.0?

@edmundsalvacion
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait.. what's going on?

@runlevel5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhh I welcome such changes to make Rails more human readable, more convenient and more aesthetic..hold on wait, but the assertion test are not English readable at all, reconsidering minitest/spec?

@adek23
Copy link

@adek23 adek23 commented on 22af62c Aug 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Comment remove by DHH for violating our Code of Conduct on using "sexualized language"]

@BadgerBadgerBadgerBadger

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love how I've found myself here despite the passage of all these years.

Please sign in to comment.