Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array named setters #6979

Closed
wants to merge 1 commit into from
Closed

Add array named setters #6979

wants to merge 1 commit into from

Conversation

lexmag
Copy link
Contributor

@lexmag lexmag commented Jul 5, 2012

For instance:

a = [*1..5]
a.third = 0  # Now `a` equal [1, 2, 0, 4, 5]

@steveklabnik
Copy link
Member

You forgot forty_two=

@lexmag
Copy link
Contributor Author

lexmag commented Jul 5, 2012

Done.

For instance:

    a = [*1..5]
    a.third = 0  # Now `a` equal [1, 2, 0, 4, 5]
@rafaelfranca
Copy link
Member

For me is ok to use named accessor, but setter seems very odd.

@lexmag
Copy link
Contributor Author

lexmag commented Jul 5, 2012

Alright. Although Array#last= is useful.

@rafaelfranca
Copy link
Member

Good point. I'll reopen to get more feedback.

@rafaelfranca rafaelfranca reopened this Jul 5, 2012
@carlosantoniodasilva
Copy link
Member

Except that it does not work for all cases: when the array is empty, last will throw you an error:

>> a = []
=> []
>> a[a.length - 1] = 'last!'
IndexError: index -1 too small for array; minimum: 0
...
>> a[-1] = 'last!'
IndexError: index -1 too small for array; minimum: 0

I personally disagree with the idea of adding similar helpers to Active Support, since these helpers are probably not going to be used internally by the framework (and possibly by most users). It looks like a method I'd have to look at the implementation to see what's going on. Anyway, thanks for the contribution @lexmag :)

@vijaydev
Copy link
Member

vijaydev commented Jul 6, 2012

-1. What @carlosantoniodasilva said.

@lexmag
Copy link
Contributor Author

lexmag commented Jul 6, 2012

@carlosantoniodasilva, thanks for the feedback. I could fix last method and replace [1/2/3] = internally by these helpers, but you are right - "if it ain't broke, don't fix it".

@lexmag lexmag closed this Jul 6, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants