Permalink
Browse files

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 22af62cf486721ee2e45bb720c42ac2f4121faf4
@@ -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]
@@ -20,6 +20,51 @@ def from(position)
def to(position)
self[0..position]
end
# Equal to self[1]

This comment has been minimized.

@mikehale

mikehale Nov 21, 2008

What’s with the redundant comments?

This comment has been minimized.

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

106 comments on commit 22af62c

@yaroslav

This comment has been minimized.

Contributor

yaroslav replied Nov 18, 2008

So many smart and helpful people in this thread, with original comments.

@NZKoz

This comment has been minimized.

Member

NZKoz replied Nov 18, 2008

Gee guys, your helpful attitude astounds me. Here’s an idea, don’t use the methods and they won’t bug you.

If you really think 10 methods equals bloat, you’d better go and remove every class and method in your application! Let me give you a quick head start:


 def entire_app(url)
  case url
  when "/hello_world.txt"
    "Hello WORLD!"
  end
 end

@jinzhu

This comment has been minimized.

jinzhu replied Nov 18, 2008

10 useless methods,another 10 useless methods,another 10 useless methods ……

@peter

This comment has been minimized.

peter replied Nov 18, 2008

+1 for this change

@dhh

This comment has been minimized.

Member

dhh replied Nov 18, 2008

Haha. I’m honored by the attention of this commit! It reminds me of the good ol’ days where we debated whether pluralization was worth it. Or whether 1.second was good. Thanks for the memories ;).

This particular patch was born from the daily work I do administrating the applications at 37signals. A lot of the support work requires going into script/console and investigating various objects. In the console, I got sick of not being able to do people.second when I had a collection and knew I needed the second record. first and last was so nice and fluid, but if I needed second or third, I had to drop back to array access. Didn’t like that, so here you go.

@DarkTatka

This comment has been minimized.

DarkTatka replied Nov 18, 2008

i completely agree with this commit, 3 of 5 my applications end with this as extension anyway.

@aizatto

This comment has been minimized.

aizatto replied Nov 18, 2008

If you needed this so much, why not stuff this into ~/.irbrc instead? Then its global to all your consoles! Personally I think that would have been a much more appreciated solution rather than stuffing it into activesupport.

@jorrel

This comment has been minimized.

jorrel replied Nov 18, 2008

I wanted ‘second’ when I’m iterating hashes. But ‘third’ to ‘tenth’ sugar doesn’t seem to taste that sweet. :)

@hgtesta

This comment has been minimized.

hgtesta replied Nov 18, 2008

+1 for this too. I always use Array#first and Array#last for arrays of size 2.

@radar

This comment has been minimized.

Contributor

radar replied Nov 18, 2008

Doesn’t anyone else think that typing array1 is faster than typing array.second? Surely by now it’s become natural.

@augustl

This comment has been minimized.

augustl replied Nov 18, 2008

I think the biggest problem with this is that when I see people.fifth in code, I won’t know what to do. Also, spotting people[4] is easier when skimming the code.

Then again, if the argument is that this was used a lot in console, it’s all good I guess.

@dhh

This comment has been minimized.

Member

dhh replied Nov 18, 2008

I’ve actually also used it in code. It’s particularly useful for command parameters. Think ARGV.second, ARGV.third. But yeah, not something you’d use every day. My most common use is with the console.

@dhh

This comment has been minimized.

Member

dhh replied Nov 18, 2008

Radar, you could make the same argument against the built-in first and last. They have 0 and [-1] equivalents. But the English versions read nicer. Ruby is full of stuff that merely improves on readability. I love that about the language.

@georgi

This comment has been minimized.

georgi replied Nov 18, 2008

I guess, this is just a diss to all the LOC counters ;)

@raganwald

This comment has been minimized.

Contributor

raganwald replied Nov 18, 2008

Would anyone prefer methods like caddr?

@NZKoz

This comment has been minimized.

Member

NZKoz replied Nov 18, 2008

perhaps cdaddaddr?

@divineforest

This comment has been minimized.

Contributor

divineforest replied Nov 18, 2008

Nice commit :). I suggest that those second…tenth methods to be added to ActiveRecord::Base as well so we could do just Person.second.

@bkerley

This comment has been minimized.

bkerley replied Nov 18, 2008

@raganwald: http://github.com/ivey/carpark provides it

@Peeja

This comment has been minimized.

Contributor

Peeja replied Nov 18, 2008

Re: LOC:


        [:second, :third, :fourth, :fifth, :sixth, :seventh, :eighth, :ninth, :tenth].each_with_index do |name, i|
            define_method(name) { self[i+1] }
        end

Et voila.

@Oshuma

This comment has been minimized.

Oshuma replied Nov 18, 2008

I think something like this should be included on a per-app basis, as opposed to being in core. How hard is it to just ‘cp array_stupid.rb ./lib’ for your projects?

-5 points for this, kthx.

@kris

This comment has been minimized.

kris replied Nov 18, 2008

Commits like this push me closer to Merb.

@ieure

This comment has been minimized.

ieure replied Nov 18, 2008

This is great, but could you please provide Japanese localization? It’s so much easier for me to use:

foo.二番目に, foo.第3, foo.第4

When I’m in the console instead of foo1, foo2…foo[N].

Thanks, I’d really appreciate it.

@georgi

This comment has been minimized.

georgi replied Nov 18, 2008

@Peeja: your code will not show in the documentation.

@maseb

This comment has been minimized.

maseb replied Nov 18, 2008

Or you could have some fancy-fun and alias :n(st/th) for 0 to your heart’s content. Then you get people.1st people.2nd… people.100th. If you’re feeling particularly courageous, you could toss the sucker in method_missing, and be able to do people.53485th.

@maseb

This comment has been minimized.

maseb replied Nov 18, 2008

Though I suppose that would cause confusion, since people expect “first” to be the “0th”. Well boo to that. Start at 1.

@maseb

This comment has been minimized.

maseb replied Nov 18, 2008

Or, I could learn Ruby, and realize that you can’t start a method with a number.

@georgi

This comment has been minimized.

georgi replied Nov 18, 2008

Seriously I cant understand the console use case. Typing the brackets is for me way faster than typing a method name, even with tab completion.

@dlisboa

This comment has been minimized.

dlisboa replied Nov 18, 2008

These methods are pointless. So are most method added for readability (Time.now instead of Time.new?). But I believe there are more important ways to spend our time, and more valuable discussions. Just leave it.

@ericallam

This comment has been minimized.

Contributor

ericallam replied Nov 18, 2008

Honestly the only arguments I can really see for being against this is the extra methods being in the Array#methods call, which I sometimes use in the console. But even then most of the time when I’m looking up a method I have a pretty good idea what its called so I usually end up doing #methods.grep /whatever/ anyways

@brennandunn

This comment has been minimized.

Contributor

brennandunn replied Nov 18, 2008

More pointless bloat for the docs. What’s next, revamping #succ to make ‘first’.next # => ‘second’ ?

@bkerley

This comment has been minimized.

bkerley replied Nov 18, 2008

Can we get an alternate version for C users who think of arrays as a0 being the “zeroth,” a1 being “first,” etc.? I’d like the choice in my code.

@Aupajo

This comment has been minimized.

Aupajo replied Nov 18, 2008

Sugar so sweet it makes my teeth ache.

@adkron

This comment has been minimized.

Contributor

adkron replied Nov 18, 2008

So what do things like this do to my load time, and my memory usage. I know that there was just the commit of using :only on resources to lower memory consumption of routes. Now that we saved that memory we can fill it with other things. I believe that I read an interview with you, David, where you were asked about what is going in the rails core in the next release and you said something along the lines of, ‘It is more like; what is coming out?’ Maybe that isn’t what I heard.

@defunkt

This comment has been minimized.

Contributor

defunkt replied Nov 18, 2008

If GitHub had a Hall of Fame, this thread would be the first entry.

@ryanb

This comment has been minimized.

Contributor

ryanb replied Nov 19, 2008

@defunkt lol, and this one could be second.

@mattly

This comment has been minimized.

mattly replied Nov 19, 2008

@binarylogic people get worked up for two reasons:
1) it makes rails more complex
2) it widens the gap between rails and “real” ruby, and if you work with non-rails ruby quite frequently, that gap becomes really annoying.

@yairgo

This comment has been minimized.

Contributor

yairgo replied Nov 19, 2008

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

A few times it was said that this was more for use in the console, which is a strong argument that it really shouldn’t be in the code base.

If you think Array1, Array2 aren’t readable maybe you should find a different hobby/profession.

@amerine

This comment has been minimized.

Contributor

amerine replied Nov 19, 2008

if you cannot discern between what ruby does and what rails adds then yes I can see it being a problem…. but maybe the problem is that you don’t know ruby well enough. Rails makes lots of stuff easier, and this just adds to the list.

Personally I don’t mind access to my arrays in that fashion. It makes sense and is pretty readable.

+4 Bonus points for making it even easier for my designer to handle small bits of code.

@chrislloyd

This comment has been minimized.

Contributor

chrislloyd replied Nov 19, 2008

I think we should paint it red.

http://www.freebsd.org/doc/en_US.ISO8859-1/books/faq/misc.html#BIKESHED-PAINTING

@robbyrussell

This comment has been minimized.

Contributor

robbyrussell replied Nov 19, 2008

Let the man have his cake. :-)

@adkron

This comment has been minimized.

Contributor

adkron replied Nov 19, 2008

Good point Chris. I like your answer.

@tedhenry10

This comment has been minimized.

tedhenry10 replied Nov 19, 2008

Rails 0.9 was better.

@boblmartens

This comment has been minimized.

boblmartens replied Nov 19, 2008

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

@rubyruy

This comment has been minimized.

Contributor

rubyruy replied Nov 19, 2008

I love this patch.

@timander

This comment has been minimized.

timander replied Nov 19, 2008

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

@brianmario

This comment has been minimized.

Contributor

brianmario replied Nov 19, 2008

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

This comment has been minimized.

Contributor

anildigital replied Nov 19, 2008

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

@johnwyles

This comment has been minimized.

johnwyles replied Nov 19, 2008

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

This comment has been minimized.

Contributor

janx replied Nov 19, 2008

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

@elisehuard

This comment has been minimized.

elisehuard replied Nov 19, 2008

@chrislloyd’s answer nails it.

@NZKoz

This comment has been minimized.

Member

NZKoz replied Nov 19, 2008

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

This comment has been minimized.

matthooks replied Nov 19, 2008

cough ibtl cough

@neilh

This comment has been minimized.

neilh replied Nov 19, 2008

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

@tidelake

This comment has been minimized.

tidelake replied Nov 19, 2008

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

This comment has been minimized.

ELLIOTTCABLE replied Nov 19, 2008

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

This comment has been minimized.

spiceee replied Nov 19, 2008

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

@nikosd

This comment has been minimized.

nikosd replied Nov 19, 2008

@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

This comment has been minimized.

Contributor

matthewrudy replied Nov 19, 2008

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

This comment has been minimized.

jamesu replied Nov 19, 2008

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

This comment has been minimized.

Contributor

gaffo replied Nov 19, 2008

@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

This comment has been minimized.

Contributor

dcrec1 replied Nov 19, 2008

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

@trevorturk

This comment has been minimized.

Contributor

trevorturk replied Nov 19, 2008

ActiveSupport::CoreExtensions::PradiptasRolodex?

@Yardboy

This comment has been minimized.

Yardboy replied Nov 19, 2008

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

This comment has been minimized.

ELLIOTTCABLE replied Nov 19, 2008

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

This comment has been minimized.

ELLIOTTCABLE replied Nov 19, 2008

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

@adelcambre

This comment has been minimized.

adelcambre replied Nov 19, 2008


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

Now it goes to eleventh.

@amerine

This comment has been minimized.

Contributor

amerine replied Nov 19, 2008

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

@pervel

This comment has been minimized.

pervel replied Nov 19, 2008

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

@toretore

This comment has been minimized.

Contributor

toretore replied Nov 19, 2008

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

This comment has been minimized.

georgi replied Nov 19, 2008

Programmers should just use US keyboard layout.

@unimatrixZxero

This comment has been minimized.

unimatrixZxero replied Nov 20, 2008

@georgi: Yes they should.

@ozataman

This comment has been minimized.

ozataman replied Nov 20, 2008

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

This comment has been minimized.

ashchan replied Nov 20, 2008

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

@stalcottsmith

This comment has been minimized.

stalcottsmith replied Nov 20, 2008

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

This comment has been minimized.

benatkin replied Nov 20, 2008

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

This comment has been minimized.

julesfern replied Nov 20, 2008

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

@julesfern

This comment has been minimized.

julesfern replied Nov 20, 2008

oh yeah and

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

/facepalm

@boof

This comment has been minimized.

boof replied Nov 21, 2008

This should be an initializer. -1

@henrik

This comment has been minimized.

Contributor

henrik replied Nov 21, 2008

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

This comment has been minimized.

bryanthompson replied Nov 21, 2008

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

This comment has been minimized.

redinger replied Nov 21, 2008

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

This comment has been minimized.

boblmartens replied Nov 21, 2008

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

@bak

This comment has been minimized.

Contributor

bak replied Nov 21, 2008

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

This comment has been minimized.

Contributor

henrik replied Nov 21, 2008

bak: Array#at is zero-indexed.

@bak

This comment has been minimized.

Contributor

bak replied Nov 22, 2008

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

@technohippy

This comment has been minimized.

technohippy replied Nov 22, 2008

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

@Aupajo

This comment has been minimized.

Aupajo replied Nov 22, 2008

@technohippy

Good god.

@ZhangHanDong

This comment has been minimized.

ZhangHanDong replied Nov 23, 2008

~/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

This comment has been minimized.

Member

dhh replied Nov 23, 2008

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

@activars

This comment has been minimized.

activars replied Nov 28, 2008

at least it’s readable

@justinko

This comment has been minimized.

Contributor

justinko replied Nov 28, 2008

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

@skrat

This comment has been minimized.

skrat replied Apr 3, 2009

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

This comment has been minimized.

skrat replied Apr 3, 2009

sorry: dhh_dumbideas1

@heycarsten

This comment has been minimized.

heycarsten replied Apr 3, 2009

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

@heycarsten

This comment has been minimized.

heycarsten replied Apr 3, 2009

Oops, textile.

@mihael

This comment has been minimized.

mihael replied Apr 4, 2009

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

This comment has been minimized.

edmundsalvacion replied Nov 18, 2013

wait.. what's going on?

@joneslee85

This comment has been minimized.

Contributor

joneslee85 replied Apr 30, 2014

@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

This comment has been minimized.

adek23 replied Aug 9, 2017

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

@ScionOfBytes

This comment has been minimized.

ScionOfBytes replied Aug 9, 2017

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

Please sign in to comment.