# How would \*YOU\* write this?

**URL:** <https://www.ruby-forum.com/t/how-would-you-write-this/71896>\
**Category:** Ruby\
**Created:** [October 12, 2006, 5:56am UTC](https://www.ruby-forum.com/t/how-would-you-write-this/71896 "2006-10-12T05:56:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [October 12, 2006, 5:56am UTC](https://www.ruby-forum.com/t/how-would-you-write-this/71896/1 "2006-10-12T05:56:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [October 12, 2006, 6:24am UTC](https://www.ruby-forum.com/t/how-would-you-write-this/71896/2 "2006-10-12T06:24:13Z")

</div>

Hi-

On 10/11/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [October 12, 2006, 7:04am UTC](https://www.ruby-forum.com/t/how-would-you-write-this/71896/3 "2006-10-12T07:04:31Z")

</div>

On Thu, 12 Oct 2006, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [October 12, 2006, 8:12am UTC](https://www.ruby-forum.com/t/how-would-you-write-this/71896/4 "2006-10-12T08:12:36Z")

</div>

removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
