Hi Raul,

I wrote a long reply to your message on the google groups discussion
page and not sure why it never posted.  I posted it yetserday.

So what I wrote briefly was that, yes, I did realize that the .first
and .last methods were instance methods of array type.  However, my
confusion was more of why 'element' would even have more than one
array element because inject is suppose to grab one element at a
time.  The only conclusion I could come up with is this.  I read that
Ruby does not support array of arrays.  If this is the case, I was
thinking that Ruby is treating the arrays within the array, each as an
element (containing elements)?  Is that why when inject grabs the
first element, it is an array of :first_name and 'Shane'?

On Nov 16, 3:50 pm, "raul parolari" <[EMAIL PROTECTED]> wrote:
> Hi, Derek
>
>    I don't think that the problem you have is on *inject*. Rather it is
> this:
>
> a) Array has many instance methods; two of them are:  first, last
>
>   arr =  [ 'jules', 'jim', 'catherine' ]
>
>   arr.first  # => "jules"
>   arr.last  # => "catherine"
>
> And, of course, if you have an array of arrays, and you apply those methods
> on each...
>
> but rather than continuing (and ruin the discovery for you), I would let you
> apply this on the code that you wrote, and see if you discover it (what you
> get is 5 times more valuable than if I continue).
>
> But if still something is not clear, let me know.
>
> Your question was excellent, by the way. I am sure that many will benefit
> from them,
>
> Raul
>
> On Sun, Nov 16, 2008 at 11:39 AM, Derek W <[EMAIL PROTECTED]> wrote:
> > HI guys,
>
> > Sorry to bring up an old topic as I found out about this class just
> > recently.  While going over exercise 4.4, it took me a while to figure out
> > everything that was going on.  I DO understand it all now (after some
> > research), but was hoping to find out more information about inject which I
> > wasn't able to find online.
>
> > Here is a sample code I found explaining inject.  While I understand the
> > usage, I am confused with how this code below works:
>
> > hash = [[:first_name, 'Shane'], [:last_name, 'Harvie']].inject({}) do
> > |result, element|
> >   result[element.first] = element.last
> >   result
> > end
>
> > From my reading, I understand that inject iterates through each element.
> > Now in this above example, you have an array of an array.  What I'm confused
> > about is why inject grabs the first two elements (:first_name and Shane).
> > Maybe I just do not understand correctly the concept of array of arrays?
> > Being that there is 4 elements total, I would have thought element would
> > only include the symbol :first_name the first time it was run.
>
> > Can someone explain this one to me?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "ruby-on-rails-programming-with-passion" group.
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to