Hi, Derek
the problem is very simple (and again, I do not think it is *inject*).
When you have an array of arrays, and you iterate, each element is the
subarray; forget inject and use a simple 'each' loop:
# the result here is obvious
[ "Calypso", "Ulysses", "Poseidon"].each { |elementt| p element }
# now, a bit more complex
[ [ "Ulysses", "mortal"], [ "Calypso", "nymph"], [ "Poseidon", "god"] ].each
do |element}
p element
end
Run the examples. Do you see? in an iteration, the element is always
the *element
of the collection*:
a) if the collection is a simple collection of strings, the element will be
the string
b) if the collection is a collection of arrays, the element is an array
c) if the collection is a collection of hash, the element is a hash
-----------
One tip: do small experiments, continuously; have always some shells open
ready to run irb, or ri. Often, it is useful run ri in one, and parallel
experiments in other 2; in seconds, you can discover (or remember)
something.
It is impossible to learn Ruby just by reading (let alone that you end up
finding that "*Ruby does not handle arrays of arrays*" and other nonsense
like that).
Intelligent books are important, but one also needs to establish a constant
communication with the language, until it becomes your friend...
Raul
On Sun, Nov 16, 2008 at 7:09 PM, derekmw <[EMAIL PROTECTED]> wrote:
>
> 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
-~----------~----~----~----~------~----~------~--~---