Forum: Ruby on Rails Is there a concise way?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3320715fa6264e6309503d5bd089fad2?d=identicon&s=25 Mufaddal Khumri (mkhumri)
on 2006-01-24 17:34
(Received via mailing list)
Hi,

Is there a concise/ruby way to do the following:
[Note: 1 and 2 below are not related.]

(1)
            for p in 0...@order.line_items[k].shipment_lines.length
                 shipline_qty = shipline_qty +
@order.line_items[k].shipment_lines[p].quantity
            end

(2)
            ts = Array.new
            for tk in s.trackings
                ts.push(tk)
            end


Thanks.
D707194949c58eb4076f040adf116508?d=identicon&s=25 Bruno Celeste (Guest)
on 2006-01-24 17:43
(Received via mailing list)
Hi, I'm not an expert so :)


(1)
           for p in 0...@order.line_items[k].shipment_lines.length
                shipline_qty +=
@order.line_items[k].shipment_lines[p].quantity
           end

(2)
           ts = Array.new
           s.trackings.each { |tk| ts << tk }

Hope that helps!

2006/1/24, Mufaddal Khumri <mkhumri@allegromedical.com>:
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-24 17:43
(Received via mailing list)
Mufaddal Khumri wrote:
> Hi,
>
> Is there a concise/ruby way to do the following:
> [Note: 1 and 2 below are not related.]
>
> (1)
>            for p in 0...@order.line_items[k].shipment_lines.length
>                 shipline_qty = shipline_qty +
> @order.line_items[k].shipment_lines[p].quantity
>            end
shipline_qty = @order.line_items[k].shipment_lines.inject(0){|m, line|
   m+line.quantity }

> (2)
>            ts = Array.new
 >            for tk in s.trackings
>                ts.push(tk)
>            end
I don't get it... What do you want to do with ts that you can't already
do with s.trackings?
9b7647d55df4584d7031130915556040?d=identicon&s=25 Will Briggs (Guest)
on 2006-01-24 17:49
(Received via mailing list)
On 1/24/06, Mufaddal Khumri <mkhumri@allegromedical.com> wrote:
>             end
I'm sure there's a better way to do this one also but I'm a relative
Ruby
newb myself...  maybe someone else can come up with something;
maybe something like:

(0...@order.line_items[k].shipment_lines.length).each do |p|
shipline_qty +=
@order.line_items[k].shipment_lines[p].quantity



(2)
>             ts = Array.new
>             for tk in s.trackings
>                 ts.push(tk)
>             end
>
>
> Thanks.



ts = s.trackings.map { |tk| tk }
This topic is locked and can not be replied to.