# Meta variable substitution

**URL:** <https://www.ruby-forum.com/t/meta-variable-substitution/116870>\
**Category:** Ruby\
**Created:** [October 10, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/meta-variable-substitution/116870 "2007-10-10T16:46:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fcruxen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ecae2f/32.png) [@fcruxen](https://www.ruby-forum.com/u/fcruxen)\
**Post date:** [October 10, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/meta-variable-substitution/116870/1 "2007-10-10T16:46:47Z")

</div>

Hi,

I’m creating a meta script that can generate both a shell script and a  
Makefile. I want the script to be human readable, so I don’t want to  
substitute all the variables, so $(top\_srcdir) =  
“/my/obsenely/huge/directory/name” doesn’t get substituted in for  
example $(srcdir) = $(top\_srcdir)/foo.

The problem is that bash doesn’t use the same syntax for variable  
substitution.

I guess the easiest thing to do is just s/$()/${}/ but I was wondering  
if there’s a better way to do that.

Since I know Rubyists can always find better ways to write code I  
decided to ask here.

Is there a better way to implement meta variable substitution?

Best regards.

---

<div class="post-metadata">

**Author:** ![fcruxen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ecae2f/32.png) [@fcruxen](https://www.ruby-forum.com/u/fcruxen)\
**Post date:** [October 10, 2007, 5:01pm UTC](https://www.ruby-forum.com/t/meta-variable-substitution/116870/2 "2007-10-10T17:01:29Z")

</div>

On 10/10/07, Felipe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I guess the easiest thing to do is just s/$()/${}/ but I was wondering  
> if there’s a better way to do that.
> 
> Since I know Rubyists can always find better ways to write code I  
> decided to ask here.
> 
> Is there a better way to implement meta variable substitution?

Are you parsing the files? I had the impression that you are  
generating them. In that case I would just generate the variable names  
rather late and somehow factorize  
the code into

def var\_name name  
“${#{name}}”  
end

def var\_name name  
“$(#{name})”  
end

if this polymorphic approach is not suitable for your design an if  
statement shall do.

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![fcruxen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ecae2f/32.png) [@fcruxen](https://www.ruby-forum.com/u/fcruxen)\
**Post date:** [October 10, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/meta-variable-substitution/116870/3 "2007-10-10T19:28:11Z")

</div>

Felipe C. wrote:

> A polymorphic approach is fine, but I want this in my code:
> 
> foo="\<var\_start\>var\_name\<var\_end\>/bar"
> 
> So you are suggesting something like:
> 
> foo="#{pseudo\_var\_name}/bar"
> 
> Right?

Do you really want such a XML-like verbose syntax?

Actually, I’m not sure what exactly you want to achieve as output, using  
what syntax. Could you give some excamples?

mortee

---

<div class="post-metadata">

**Author:** ![fcruxen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ecae2f/32.png) [@fcruxen](https://www.ruby-forum.com/u/fcruxen)\
**Post date:** [October 10, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/meta-variable-substitution/116870/4 "2007-10-10T17:14:57Z")

</div>

On 10/10/07, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> rather late and somehow factorize  
> the code into

Yeah, I’m generating them.

> def var\_name name  
> “${#{name}}”  
> end
> 
> def var\_name name  
> “$(#{name})”  
> end
> 
> if this polymorphic approach is not suitable for your design an if  
> statement shall do.

A polymorphic approach is fine, but I want this in my code:

foo=“\<var\_start\>var\_name\<var\_end\>/bar”

So you are suggesting something like:

foo=“#{pseudo\_var\_name}/bar”

Right?
