Nested blocks

J

Josselin

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"}
 
H

hemant

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

#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.
 
R

Robert Klemme

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

#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.

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
 
J

Josselin

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

#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.

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[:longitude] = geocodes[:longitude], .....
I know that I could do an iteration (as in C) but I tried to do it
without using an indexation ...

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

joss
 
R

Robert Klemme

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

#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.

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[:longitude] = geocodes[:longitude], .....
I know that I could do an iteration (as in C) but I tried to do it
without using an indexation ...

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

joss

result = geocodes.map do |geocode|
{:search => adresse, :address => geocode[:address], ...}
end

robert
 
J

Josselin

On 05.03.2007 16:37, hemant wrote:
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

#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.

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[:longitude] = geocodes[:longitude], .....
I know that I could do an iteration (as in C) but I tried to do it
without using an indexation ...

places
=> {:longitude=> geocodes[longitude],
:latitude=>geocodes[longitude], :address=>geocodes[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...
 
D

dblack

Hi --

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,236
Messages
2,571,185
Members
47,820
Latest member
HortenseKo

Latest Threads

Top