how would *YOU* write this??

Discussion in 'Ruby' started by phil.swenson@gmail.com, Oct 12, 2006.

  1. Guest

    small_image_url = XPath.first(item, "SmallImage/URL").text
    small_image_height = XPath.first(item, "SmallImage/Height
    Units").text
    small_image_width = XPath.first(item, "SmallImage/Width
    Units").text
    medium_image_url = XPath.first(item, "MediumImage/URL").text
    medium_image_height = XPath.first(item, "MediumImage/Height
    Units").text
    medium_image_width = XPath.first(item, "MediumImage/Width
    Units").text
    large_image_url = XPath.first(item, "LargeImage/URL").text
    large_image_height = XPath.first(item, "LargeImage/Height
    Units").text
    large_image_width = XPath.first(item, "LargeImage/Width
    Units").text

    Instead of writing this with this redundant style, how would a clever
    rubyist write this? Could write a method to take the image name as an
    input and return 3 params...

    def image_attributes (item, image_name)
    height = XPath.first(item, image_name + "/Height Units").text
    width = XPath.first(item, image_name + "/Width Units").text
    url = XPath.first(item, image_name + "/URL").text
    return height, width, url
    end

    small_image_height, small_image_width, small_image_url =
    image_attributes(item, "SmallImage")
    medium_image_height, medium_image_width, medium_image_url =
    image_attributes(item, "MediumImage")
    large_image_height, large_image_width, large_image_url =
    image_attributes(item, "LargeImage")

    However, I'm thinking there are much nicer ways to do it...

    any thoughts?
    , Oct 12, 2006
    #1
    1. Advertising

  2. Guest

    On Thu, 12 Oct 2006, wrote:


    > small_image_url = XPath.first(item, "SmallImage/URL").text
    > small_image_height = XPath.first(item, "SmallImage/Height Units").text
    > small_image_width = XPath.first(item, "SmallImage/Width Units").text


    > medium_image_url = XPath.first(item, "MediumImage/URL").text
    > medium_image_height = XPath.first(item, "MediumImage/Height Units").text
    > medium_image_width = XPath.first(item, "MediumImage/Width Units").text


    > large_image_url = XPath.first(item, "LargeImage/URL").text
    > large_image_height = XPath.first(item, "LargeImage/Height Units").text
    > large_image_width = XPath.first(item, "LargeImage/Width Units").text



    key4 = lambda{|key| key.downcase.gsub(%r/[^a-zA-Z0-9]+/,'_')}

    images =
    [ "Small", "Medium", "Large" ].inject({}) do |h, which|
    h.update key4[which] =>

    [ "URL", "Height Units", "Width Units" ].inject({}) do |h, attr|
    h.update key4[attr] =>

    XPath.first(item, "#{ which }Image/#{ attr }").text
    end
    end


    p images['medium']['height_units']
    p images['large']['url']

    -a
    --
    my religion is very simple. my religion is kindness. -- the dalai lama
    , Oct 12, 2006
    #2
    1. Advertising

  3. matt neuburg Guest

    <> wrote:

    > Could write a method to take the image name as an
    > input and return 3 params...
    >
    > def image_attributes (item, image_name)
    > height = XPath.first(item, image_name + "/Height Units").text
    > width = XPath.first(item, image_name + "/Width Units").text
    > url = XPath.first(item, image_name + "/URL").text
    > return height, width, url
    > end


    def image_attributes(item, image_name)
    ["/Height Units", "/Width Units", "/URL"].collect do |s|
    XPath.first(item, image_name + s).text
    end
    end

    > small_image_height, small_image_width, small_image_url =
    > image_attributes(item, "SmallImage")
    > medium_image_height, medium_image_width, medium_image_url =
    > image_attributes(item, "MediumImage")
    > large_image_height, large_image_width, large_image_url =
    > image_attributes(item, "LargeImage")


    ["small", "medium", "large"].each do |s|
    eval %{ $#{s}_image_height, $#{s}_image_width, $#{s}_image_url =
    image_attributes(item, "#{s.capitalize}Image") }
    end

    Sorry, I ended up creating globals, $small_image_height etc. m.

    --
    matt neuburg, phd = , http://www.tidbits.com/matt/
    Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
    AppleScript - http://www.amazon.com/gp/product/0596102119
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt neuburg, Oct 12, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SklNLkgu?=

    Q: how would you write this

    =?Utf-8?B?SklNLkgu?=, Feb 17, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    295
    William F. Robertson, Jr.
    Feb 17, 2005
  2. Michael B Allen

    Error handling - How would you write it?

    Michael B Allen, Jul 9, 2005, in forum: C Programming
    Replies:
    13
    Views:
    500
    Christopher Benson-Manica
    Jul 11, 2005
  3. Jeff Rush
    Replies:
    4
    Views:
    298
    Michele Simionato
    Apr 26, 2007
  4. aleksa

    How would you write this?

    aleksa, Apr 24, 2010, in forum: C Programming
    Replies:
    13
    Views:
    421
    Tim Rentsch
    Apr 29, 2010
  5. Frank Meyer
    Replies:
    12
    Views:
    162
    Gregory Brown
    Aug 12, 2007
Loading...

Share This Page