[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how would *YOU* write this??

phil swenson

10/12/2006 3:54:00 AM

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?

2 Answers

Ara.T.Howard

10/12/2006 5:03:00 AM

0

matt

10/12/2006 6:07:00 AM

0

phil.swenson@gmail.com <phil.swenson@gmail.com> 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 = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...