help me make this prettier and more ruby-ish

R

Rob Sanheim

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 += "<li>#{ link }</li>"
end
output

any help greatly appreciated...

thanks
- Rob
 
T

Tom Werner

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)
"<li>#{args.join(', ')}</li>"
end

puts infobar_sites.map { |m| infobar_link(*m.to_a) }

Which outputs

<li>usmarket, The Market</li>
<li>internet</li>
<li>etf, ETFs</li>
<li>china</li>
<li>energy</li>
<li>ce, Electronics</li>
<li>media</li>
<li>gold</li>
<li>telecom</li>
<li>biotech</li>
<li>retail</li>
<li>japan</li>
<li>india</li>

Tom

Rob said:
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 += "<li>#{ link }</li>"
end
output

any help greatly appreciated...

thanks
- Rob


--
Tom Werner
Helmets to Hardhats
Software Developer
(e-mail address removed)
www.helmetstohardhats.org
 
R

Rob Sanheim

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)
"<li>#{args.join(', ')}</li>"
end

puts infobar_sites.map { |m| infobar_link(*m.to_a) }

Which outputs

<li>usmarket, The Market</li>
<li>internet</li>
<li>etf, ETFs</li>
<li>china</li>
<li>energy</li>
<li>ce, Electronics</li>
<li>media</li>
<li>gold</li>
<li>telecom</li>
<li>biotech</li>
<li>retail</li>
<li>japan</li>
<li>india</li>

Tom

Rob said:
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 += "<li>#{ link }</li>"
end
output

any help greatly appreciated...

thanks
- Rob


--
Tom Werner
Helmets to Hardhats
Software Developer
(e-mail address removed)
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top