Simple Array Issue - HELP

Ok, I am interfacing with the del.icio.us API and I want to collect the
links in an @instance variable for my view. But I’m having trouble with
that part. Here’s what I have:

def get_authenticated(path, http)
request = Net::HTTP::Get.new(path)
request.basic_auth ‘user’, ‘pass’
response = http.request(request)
response.value
response.body
end

@links = Array.new
http = Net::HTTP.new(‘api.del.icio.us’, 443)
http.use_ssl = true
http.start do |http|
xml = get_authenticated(’/v1/posts/recent?count=10’, http)
REXML:ocument.new(xml).root.get_elements(‘post’) .each do |post|
link = post.attributes[‘href’]
@links = link
end
end

See where I’m saying “@links = link”? I’m trying to get that into an
array. Then iterate over the array in the view, but it’s not working. It
only displays the last of the 10 links returned. Can someone please help
with this???

Thanks a lot!!!

Oh yeah, and I’ve also tried “@links[i] = link” and then “i++” to go to
the next index. Not working still.

i would try changing

link = post.attributes[‘href’]

to

@links = post.attributes[‘href’]

i think that would work, but i’m not a genius, clearly.

Jon Mr wrote:

i would try changing

link = post.attributes[‘href’]

to

@links = post.attributes[‘href’]

i think that would work, but i’m not a genius, clearly.

Thanks, but I’ve tried that as well. Still only displays the last one
out of the ten links. Isn’t there a way to “collect” the links? Such
as “.collect{post.attributes[‘href’]}”? I’ve tried variations of this,
but couldn’t get anything to work out.

So, I’m still stuck.

On Tue, Jul 18, 2006, Ryan Heath wrote:

See where I’m saying “@links = link”? I’m trying to get that into an
array. Then iterate over the array in the view, but it’s not working. It
only displays the last of the 10 links returned. Can someone please help
with this???

@links << link

Should work like a champ :slight_smile:

Ben

On 7/18/06, Ben B. [email protected] wrote:

Ben
Also, worth noting that there is no auto-increment operator in ruby.
So i++ should do some weird things but probably won’t throw an error.
The << is equivalent to Array.push in other languages if that helps
you understand at all. In ruby it seems like counter variables are
largely unused which is a mammoth change but it’s really exciting once
you really get into the language.

Cheers,
Chuck V.

@links = []
REXML:ocument.new(xml).root.get_elements(‘post’) .each do |post|
link = post.attributes[‘href’]
@links << link
end

        - dan


Dan K. mailto:[email protected]
http://www.dankohn.com/ tel:+1-415-233-1000

Chuck V. wrote:

On 7/18/06, Ben B. [email protected] wrote:

Ben
Also, worth noting that there is no auto-increment operator in ruby.
So i++ should do some weird things but probably won’t throw an error.
The << is equivalent to Array.push in other languages if that helps
you understand at all. In ruby it seems like counter variables are
largely unused which is a mammoth change but it’s really exciting once
you really get into the language.

Cheers,
Chuck V.

Thanks a lot everyone, and for the explanation - that clears things up
tremendously!

On 19/07/2006, at 6:17 AM, Dan K. wrote:

@links = []
REXML:ocument.new(xml).root.get_elements(‘post’) .each do |post|
link = post.attributes[‘href’]
@links << link
end

Or, to be a little more Ruby-ish about it:

@links = REXML:document.new(xml).root.get_elements(‘post’).map {|
post| post.attributes[‘href’] }

On 7/18/06, Pete Y. [email protected] wrote:

@links = REXML:document.new(xml).root.get_elements(‘post’).map {|
post| post.attributes[‘href’] }

Hot. And your domainname is awesome!

Why is it that Rails app names so often start with a number though?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs