# 3-second-n00b question

**URL:** <https://www.ruby-forum.com/t/3-second-n00b-question/183368>\
**Category:** Rails\
**Created:** [February 12, 2010, 3:35pm UTC](https://www.ruby-forum.com/t/3-second-n00b-question/183368 "2010-02-12T15:35:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2010, 3:35pm UTC](https://www.ruby-forum.com/t/3-second-n00b-question/183368/1 "2010-02-12T15:35:34Z")

</div>

Hi there,

my short question:

Assume I have these 2 cases in my code :

============================  
:name =\> h(agent.firstname)

[…]

# :name =\> h(agent.sharedname)

To stay DRY, I want this to become just 1 line, by replacing  
“first”/“shared” by a variable that I would set at the beginning.

How would the code of this 1 new line and the initiation of the variable  
look like?

Thanks for your help!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2010, 3:41pm UTC](https://www.ruby-forum.com/t/3-second-n00b-question/183368/2 "2010-02-12T15:41:16Z")

</div>

I think that’s overdoing the DRY thing a little bit, but if you want:

name\_attr = :"#{some\_condition ? ‘first’ : ‘shared’}name"

:name =\> h(agent.send(name\_attr))

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2010, 3:43pm UTC](https://www.ruby-forum.com/t/3-second-n00b-question/183368/3 "2010-02-12T15:43:02Z")

</div>

On Feb 12, 2:35 pm, Tom Ha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> To stay DRY, I want this to become just 1 line, by replacing  
> “first”/“shared” by a variable that I would set at the beginning.
> 
> How would the code of this 1 new line and the initiation of the variable  
> look like?

Well you could either use send, but to be quite honest I really  
wouldn’t - You’ll end up with something that is probably longer and  
certainly more complicated.

Fred

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2010, 3:50pm UTC](https://www.ruby-forum.com/t/3-second-n00b-question/183368/4 "2010-02-12T15:50:38Z")

</div>

On 12 February 2010 14:39, Mat B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think that’s overdoing the DRY thing a little bit, but if you want:
> 
> name\_attr = :“#{some\_condition ? ‘first’ : ‘shared’}name”
> 
> :name =\> h(agent.send(name\_attr))

That’s putting business logic in the view - avoid wherever possible  
for sleep-filled nights…

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2010, 3:49pm UTC](https://www.ruby-forum.com/t/3-second-n00b-question/183368/5 "2010-02-12T15:49:28Z")

</div>

On 12 February 2010 14:35, Tom Ha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How would the code of this 1 new line and the initiation of the variable  
> look like?

Difficult to suggest an ideal solution with such a little info, but  
maybe add a “first\_or\_shared\_name” method to your Agent model, which  
determines which name to show, and then call that in the views instead  
of either firstname or sharedname:

# agent.rb

def first\_or\_shared\_name  
case some\_method\_or\_variable\_that\_determines\_which\_name  
when :first  
self. firstname  
when :shared  
self.sharedname  
else  
raise “invalid value passed to my cool new method”  
end  
end

# view

:name =\> h(agent.first\_or\_shared\_name)

then if you want/need to, you could pass the variable to the model  
method from the view if it’s likely to change lots:

# agent.rb

def first\_or\_shared\_name(first\_or\_shared)  
case first\_or\_shared  
… etc  
end  
end

# view

:name =\> h(agent.first\_or\_shared\_name :shared)

:name =\> h(agent.first\_or\_shared\_name :first)

Coming back to the point, you might just be making work for the sake  
of it, but I’ve used approaches like this when, for instance, I want  
to show “firstname lastname” or “title lastname” depending on whether  
I have the firstname or not.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/3-second-n00b-question/183368/6 "2010-02-12T15:54:09Z")

</div>

While I agree that your solution of having a method in the model that  
delegates to the appropriate attribute is elegant (if the model has  
access to the appropriate data to make that decision), I wonder what  
reason we have to believe this is going on in the view?

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/3-second-n00b-question/183368/7 "2010-02-12T15:57:05Z")

</div>

On 12 February 2010 14:52, Mat B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> While I agree that your solution of having a method in the model that  
> delegates to the appropriate attribute is elegant (if the model has  
> access to the appropriate data to make that decision), I wonder what  
> reason we have to believe this is going on in the view?
> 
> > > :name =\> h(agent.send(name\_attr))

the call to “h” gave me a clue, but if it’s not a view (and he’s  
calling view helpers in models or controllers), the principle of  
keeping model logic in the model is fairly sound.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2010, 4:06pm UTC](https://www.ruby-forum.com/t/3-second-n00b-question/183368/8 "2010-02-12T16:06:48Z")

</div>

Thanks a lot for your quick help, guys !

Actually, all of your input is very helpful (some of it right away, some  
of it later)!

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [February 12, 2010, 3:58pm UTC](https://www.ruby-forum.com/t/3-second-n00b-question/183368/9 "2010-02-12T15:58:50Z")

</div>

On 12 February 2010 14:52, Mat B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (if the model has  
> access to the appropriate data to make that decision)

that’s what occurred to me as I wrote it, hence the suggestion to pass  
the determination in as a parameter to the method.

Like I said, with such little info from the OP, it’s hard to be  
conclusive. If we’ve given him a few different choices, one of them  
might be what he needs  
🙂
