On reading my first thought was "can you do that by defining to_proc on Array".

A brief play suggests one can:

class Array
  def to_proc
    Proc.new { |arg| self.inject(arg) { |memo, f| f.to_proc.call(memo) } }
  end
end

Example using primitives:

% [1, 2, 3].map &[:succ, :odd?, :to_s, :length]
=> [5, 4, 5]

Your example would I guess become

% orders.map &[:order_option, :item, :title]

Seems OK?

 --Anthony.

On 11/05/18 19:20, Alberto Almagro wrote:
Hi Rafael,

I'm glad to see you here. The reason I see its place here at first is because I think its use case fits better with the framework than with the language. When thinking on the Ruby language I would expect more to deal with collections which contain objets like String, Integer, and the likes, while in Ruby on Rails you are much likely to have related collections because of Active Record navigations that require this kind of map chaining.

Cheers,
Alberto Almagro

El viernes, 11 de mayo de 2018, 20:01:27 (UTC+2), Rafael Mendonça França escribió:
If you think that method would be useful there is no reason why it should be Rails specific. Please send a feature request to the Ruby issue tracker here https://bugs.ruby-lang.org/.

Rafael França

On May 11, 2018, 13:10 -0400, Alberto Almagro <alberto...@gmail.com>, wrote:
These days I have been comparing records in my daily job lots of times, which made me think about a better way to retrieve and compare them. When I want to navigate through several relations in a collection I often see myself writing code like the following:

Given orders as a collection of Order:
> orders.map(&:order_option).map(&:item).map(&:title)
=> ['Foo', 'Bar', 'Baz']

That is, chaining maps with Symbol to Proc coercions one after each other. Sharing my thoughts with my company's architect we came up with the alternative:
> orders.map { |order| order&.order_option&.item&.title }

But we agreed that the notation was awful and didn't improve what we had before. With this, I proposed what I think it is more like what we would expect Ruby to have. I would like to add a notation similar to the one we can find at Array#dig or Hash#dig in the following manner:
> orders.map(&:order_option, &:item, &:title)
The method doesn't necessarily need to be named map or collect, we can agree on a different name for it if you want. Please share your thoughts with me. If you like this, I would be very happy to write a PR to include it in Rails.

Cheers,
Alberto Almagro
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.




--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to