how would *YOU* write this??

P

phil.swenson

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?
 
A

ara.t.howard

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
 
M

matt neuburg

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.
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top