SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    strange thing w/ array.map

    Hello,

    I'm writing a method that builds rss and atom feeds and I've noticed something strange when I try updating the content array by using a map.

    I'm new to ruby and must confess that I don't know how array maps work so well.

    I am formatting some of the rss content array by updating it's contents w/ a map like so:

    Code:
    @entities = Space.find :all
    
    @new_entities = @entities.map do |entity|  
      open_closed = entity.complete? ? "[x] " : "[ ] "  
      entity.title = open_closed + entity.title 
      entity.title += (' due on ' + local_date_time(entity.due_on)) if entity.due_on
      entity.title += " (#{entity.status_title})"
    end if @entities
    then when I try to use the @new_entities array in the feed, I get an error

    Code:
    for entity in @new_entities  #-->>error!
      xml.item do
        xml.title(entity.title)
        xml.description(entity.description)
        xml.pubDate(entity.date.strftime("%a, %d %b %Y %H:%M:%S %z"))
        xml.link("http://www.recentrambles.com/pragmatic/view/" + entity.id.to_s)
        xml.guid("http://www.recentrambles.com/pragmatic/view/" + entity.id.to_s)
      end
    end
    but, if I use the original @entities array it works perfectly
    Code:
    for entity in @entities   #-->>works!
      xml.item do
        xml.title(entity.title)
        xml.description(entity.description)
        xml.pubDate(entity.date.strftime("%a, %d %b %Y %H:%M:%S %z"))
        xml.link("http://www.recentrambles.com/pragmatic/view/" + entity.id.to_s)
        xml.guid("http://www.recentrambles.com/pragmatic/view/" + entity.id.to_s)
      end
    end

    How, exactly, is that @entities array getting updated by the map?

    Thanks for your help!

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it were me, I wouldn't be using map at all for this. Map is more useful for a scenario like this:

    Code:
    numbers = [1,2,3,4,5]
    new_nums = numbers.map { |num|
         num * 12
         #or some other math or complex string logic
    }
    You are just trying to iterate over a collection, and do "something" to each element as you go, I'd do it more like this:

    Code:
    new_entities = entities.dup #create a full copy of entities, since you seem to not want to work on the original
    
    new_entities.each do |entity|
       #your entity stuff here
    end
    It's slightly less memory efficient, but a lot more readable and usable than what you're trying to do currently.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sqarissta is right, but I'd put the code in the view instead.

    Your map code doesn't work because of the last line:

    Code:
    entity.title += " (#{entity.status_title})"
    This returns a string. So the map will create an array of strings, and not an array of entities.

    If you add this code after this line:

    Code:
    entity
    (on the next line). This way the entity is returned.

    But note that Sqarissta's solution is better.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Html formatting w/ Atom feeds

    Hello,
    I'm having a great time building some rss/atom feeds and I've been quite successful until I hit the html formatting line of the Atom feed.

    Here's an example:

    Code:
    xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
    
      xml.feed "xml:lang" => "en-US", "xmlns" => 'http://www.w3.org/2005/Atom ' do
        xml.title("flavor: #{@flavor} *title1: " + @feed.title)
        xml.id( "tag:#{request.host},#{Time.now.utc.year}:#{@feed.title}/#{@feed.title.downcase}" )
        xml.link('url: ' + @ feed.guid) if @feed.guid
        xml.author  { xml.name "Author Name" }
        
         @entities.each do |entity|
           xml.entry do
             xml.title('title: ' + entity.title )
             xml.link("rel" => "alternate", "href" => url_for(url_for :only_path => false, :controller => @this_controller, :action => @this_action, :id => entity.id))
             xml.id(url_for(:only_path => false, :controller => @this_controller, :action => @this_action, :id => entity.id))
             xml.updated("#{Time.now.utc.year}")
             
             xml.content "type" => "html" do
               xml.name entity.body   #-->>tried this - doesn't work
               xml.text! render(:partial => "shared/feed/entity", :entity => entity)-->>Tried this - doesn't work
               
             end
           end
         end
      end
    If I could just get the
    Code:
           
    xml.content "type" => "html" do
    end
    section to work I would be home free.

    Help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •