Hi Raul,

Yes, I actually was aware of the instance methods...which is actually
why I came up with the question posted in the first place.  Where my
code calls:

result[element.first] = element.last

I would have thought element would only contain ONE element, thus not
needing to call element.last when there is only one element.  However,
in debug, it is clear that element contains the first array within the
array (which is :first_name and 'Shane').

When I was reading through explanations of how inject works, I read
that inject iterates through each element.  So in an example like
this:

nums = [1, 3, 5, 7]
sum = nums.inject(0) { |x,n| x+n }

Inject will iterate through each element of nums.  Thus in each
iteration, n will equal ONE element.

So for the initial code posted in my first post, I assumed, even
though it's an array of arrays, since there are 4 elements total,
through each iteration of inject, 'element' would only include one
element from the array of arrays.

However, I'm thinking, in the case of array of arrays, does inject
consider the array as a regular array (I do recall reading that Ruby
does not have direct support of array of arrays/multidimensional
arrays)?  If this is the case, I can see how the arrays inside are
just treated as elements as a whole (making [:first_name, 'Shane'] the
first element...which happens to be an array).

Does that sound right?


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