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

Custom array class #7

Closed
jaylevitt opened this issue Sep 30, 2011 · 3 comments
Closed

Custom array class #7

jaylevitt opened this issue Sep 30, 2011 · 3 comments

Comments

@jaylevitt
Copy link

I'd like to be able to cast the values to an Array subclass with named getter methods, instead of an actual Array. That will let me turn this:

Scores.all.values_of(:user_id, :scale_id, :score).each do |s|
  if s[0] == current_user.id and s[1] * s[2] = 5 then put "magic numbers!"
 end

into this:

Scores.all.values_of(:user_id, :scale_id, :score).each do |s|
  if s.user_id == current_user.id and s.scale_id * s.score = 5 then put "literate code!"
 end

Would you accept such a patch?

@ernie
Copy link
Owner

ernie commented Sep 30, 2011

I considered going that route at first, but it adds a bit of overhead, and I'm not really sure what the benefit would be in the simplest use cases such as outlined above, when you can do:

Scores.values_of(:user_id, :scale_id, :score).each do |user_id, scale_id, score|
  if user_id == current_user.id and scale_id * score = 5 then put "literate code!"
end

@jaylevitt
Copy link
Author

...

I did not know you could flatten arrays into block parameters! Never mind...

@ernie
Copy link
Owner

ernie commented Sep 30, 2011

Yeah, it's a neat trick, and really handy. :) Glad I could help!

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

No branches or pull requests

2 participants