# Help me make this prettier and more ruby-ish

**URL:** <https://www.ruby-forum.com/t/help-me-make-this-prettier-and-more-ruby-ish/100072>\
**Category:** Ruby\
**Created:** [July 27, 2006, 12:26am UTC](https://www.ruby-forum.com/t/help-me-make-this-prettier-and-more-ruby-ish/100072 "2006-07-27T00:26:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [July 27, 2006, 12:26am UTC](https://www.ruby-forum.com/t/help-me-make-this-prettier-and-more-ruby-ish/100072/1 "2006-07-27T00:26:06Z")

</div>

Hi,

I know this can be done much cleaner and with prettier code…but I  
was having trouble getting my head around doing it with collect.

“infobar\_link” creates hyperlinks - it takes a 1st parameter of a  
string denoting a lookup value, and an optional second string  
parameter for the link text.

So “infobar\_sites” is a list of sites I want to create links to, and  
as you can see they can either be just a lookup value (single string),  
or an array with the lookup and a title to override the default. Then  
I just enumerate thru and create a list of the links.

infobar\_sites = [[“usmarket”, “The Market”],  
“internet”,  
[“etf”, “ETFs”],  
“china”,  
“energy”,  
[“ce”, “Electronics”],  
“media”,  
“gold”,  
“telecom”,  
“biotech”,  
“retail”,  
“japan”,  
“india” ]  
output = “”  
infobar\_sites.each do |args|  
link = nil  
if args.is\_a? String  
link = infobar\_link(args)  
else  
link = infobar\_link(args[0], args[1])  
end  
output += “

- #{ link }
”  
end  
output

any help greatly appreciated…

thanks

- Rob

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [July 27, 2006, 12:39am UTC](https://www.ruby-forum.com/t/help-me-make-this-prettier-and-more-ruby-ish/100072/2 "2006-07-27T00:39:06Z")

</div>

How about something like this:

infobar\_sites = [[“usmarket”, “The Market”],  
“internet”,  
[“etf”, “ETFs”],  
“china”,  
“energy”,  
[“ce”, “Electronics”],  
“media”,  
“gold”,  
“telecom”,  
“biotech”,  
“retail”,  
“japan”,  
“india” ]

def infobar\_link(\*args)  
“

- #{args.join(’, ')}
”  
end

puts infobar\_sites.map { |m| infobar\_link(\*m.to\_a) }

Which outputs

- usmarket, The Market
- internet
- etf, ETFs
- china
- energy
- ce, Electronics
- media
- gold
- telecom
- biotech
- retail
- japan
- india

Tom

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [July 27, 2006, 12:57am UTC](https://www.ruby-forum.com/t/help-me-make-this-prettier-and-more-ruby-ish/100072/3 "2006-07-27T00:57:41Z")

</div>

On 7/26/06, Tom W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> "telecom",
> 
> ```
> 
> - telecom
> \> I know this can be done much cleaner and with prettier code...but I \> \> "biotech", \> end
> 
> –  
> Tom W.  
> Helmets to Hardhats  
> Software Developer  
> removed\_email\_address@domain.invalid  
> [www.helmetstohardhats.org](http://www.helmetstohardhats.org)

Hm, I think that will work, I’ll just have to change the infobar\_link  
to use variable args. Right now its signature is this:

def infobar\_link(slug, link\_text = nil)

# if link\_text is nil, get a default from the db record found via slug

# …

So I suppose I could change it to just test like this:

def infobar\_link(\*args)  
if args[1] # use the supplied link\_text, else is must be nil so use  
the default  
# …

thanks …just kinda thinking out loud here.

- Rob
