How would *YOU* write this?

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?

Hi-

On 10/11/06, [email protected] [email protected] wrote:

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…

class Image
attr_reader :height, :width, :url

def initialize(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
end
end

small_image = Image.new(item, “SmallImage”)
medium_image = Image.new(item, “MediumImage”)
large_image = Image.new(item, “LargeImage”)

Then you can access the url, height, and width via the appropriate
method on
each Image instance.

puts “SmallImage URL: #{small_image.url}”
puts “SmallImage Height: #{small_image.height}”
puts “SmallImage Width: #{small_image.width}”

I’m sure others will have much nicer ways, but this was the first thing
to
come to mind.

def image_attributes (item, image_name)

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?

HTH,
Michael G.

On Thu, 12 Oct 2006, [email protected] 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

[email protected] [email protected] 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.

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