Forum: Ruby Nested blocks

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.
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-03-05 16:21
(Received via mailing list)
I am trying to nest 2 blocks, it's wrong but but I cannot find why ..
need some help.. I get only one resulting item

I get  info in geocodes Array (3 items) , need to get some of them into
'places' another Array

      places = [ {:search => adresse}]

      geocodes.each {  | geocode |
           places.collect { | place |
                place[:address] = geocode[:address]
                place[:latitude] = geocode[:latitude]
                place[:longitude] = geocode[:longitude]
          }
      }

geocodes.nitems => 3

geocodes[0]
=> #<struct Ym4r::GmPlugin::Geocoding::Placemark address="Rue
Caulaincourt, 75018 18ème Arrondissement, Paris, France",
country_code="FR", administrative_area="Ile-de-France",
sub_administrative_area="Paris", locality="Paris",
dependent_locality="18ème Arrondissement", thoroughfare="Rue
Caulaincourt", postal_code="75018", longitude=2.333944,
latitude=48.888226

geocodes[1]
=> #<struct Ym4r::GmPlugin::Geocoding::Placemark address="Lamarck -
Caulaincourt, France", country_code="FR",
administrative_area="Ile-de-France", sub_administrative_area="Paris",
locality="Paris", dependent_locality="18ème Arrondissement",
thoroughfare="Rue Caulaincourt", postal_code="75018",
longitude=2.333944, latitude=48.888226>

geocodes[2]
=> #<struct Ym4r::GmPlugin::Geocoding::Placemark address="Square
Caulaincourt, 75018 18ème Arrondissement, Paris, France",
country_code="FR", administrative_area="Ile-de-France",
sub_administrative_area="Paris", locality="Paris",
dependent_locality="18ème Arrondissement", thoroughfare="Rue
Caulaincourt", postal_code="75018", longitude=2.333944,
latitude=48.888226

places.nitems => 1

places[0]
=> {:longitude=>2.333944, :latitude=>48.888226,
:search=>"caulaincourt,75018, france", :address=>"Square Caulaincourt,
75018 18ème Arrondissement, Paris, France"}
87e41d0d468ad56a3b07d9a6482fd6d5?d=identicon&s=25 Hemant Kumar (gnufied)
on 2007-03-05 16:38
(Received via mailing list)
On 3/5/07, Josselin <josselin@wanadoo.fr> wrote:
>                 place[:address] = geocode[:address]
>                 place[:latitude] = geocode[:latitude]
>                 place[:longitude] = geocode[:longitude]
>           }
>       }
>
> geocodes.nitems => 3

#collect doesn't modify the existing Enumerable  and normally returns
a Enumerable/Array.
In other words, your inner block doesn't modify places array at all.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-05 17:10
(Received via mailing list)
On 05.03.2007 16:37, hemant wrote:
>>            places.collect { | place |
> In other words, your inner block doesn't modify places array at all.
No need for collect or collect! at all since he is modifying the
Hash(es) inside places.  And since places contains only 1 element it
still does so after the iteration.  Josselin, what do you expect to get?

Kind regards

  robert
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-03-05 17:26
(Received via mailing list)
On 2007-03-05 17:00:58 +0100, Robert Klemme <shortcutter@googlemail.com>
said:

>>>       geocodes.each {  | geocode |
>> a Enumerable/Array.
>> In other words, your inner block doesn't modify places array at all.
>
> No need for collect or collect! at all since he is modifying the
> Hash(es) inside places.  And since places contains only 1 element it
> still does so after the iteration.  Josselin, what do you expect to get?
>
> Kind regards
>
>   robert

Thanks  for your comments..

well I expect to get 3 items (one for each geocodes item)
where   places[i][:longitude] = geocodes[i][:longitude], .....
I know that I could do an iteration (as in C) but I tried to do it
without using an indexation ...

places[i]
=> {:longitude=> geocodes[i][longitude],
:latitude=>geocodes[i][longitude], :address=>geocodes[i][address]}

joss
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-05 17:36
(Received via mailing list)
On 05.03.2007 17:21, Josselin wrote:
>>>>
>>>> geocodes.nitems => 3
>>
> => {:longitude=> geocodes[i][longitude],
> :latitude=>geocodes[i][longitude], :address=>geocodes[i][address]}
>
> joss
>
>
result = geocodes.map do |geocode|
   {:search => adresse, :address => geocode[:address], ...}
end

  robert
6b2144362fffd4f71cca755d4045846f?d=identicon&s=25 Josselin (Guest)
on 2007-03-05 17:51
(Received via mailing list)
On 2007-03-05 17:31:31 +0100, Robert Klemme <shortcutter@googlemail.com>
said:

>>>>>
>>>>> geocodes.nitems => 3
>>>
>> => {:longitude=> geocodes[i][longitude],
>> :latitude=>geocodes[i][longitude], :address=>geocodes[i][address]}
>>
>> joss
>>
>>
> result = geocodes.map do |geocode|
>    {:search => adresse, :address => geocode[:address], ...}
> end
>
>   robert

thanks..   so map and collect give the same result... not easy to
forget iteration model ... so powerful methods w Ruby...
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-03-05 17:55
(Received via mailing list)
Hi --

On Tue, 6 Mar 2007, Josselin wrote:

> thanks..   so map and collect give the same result... not easy to forget
> iteration model ... so powerful methods w Ruby...

They're actually the same method -- just two names bound to exactly
the same thing.


David
This topic is locked and can not be replied to.