Rails, Ruby, haml, metaprogramming problem

In HAML I have hundreds of lines like the following:

  - xyz = someFuncThatReturnsString('xyz')

and elsewhere

  %div{'id' => xyz}

The above lines work fine.

- - -

Attempting to keep things DRY (Don't repeat yourself) I want to do something like

  - eval(otherFuncThatReturnsString('xyz'))

where

  otherFuncThatReturnsString('xyz')

returns

  "xyz = someFuncThatReturnsString('xyz')"

When I do this, HAML no longer sees xyz as being defined when it attempts to interpret

  %div{'id' => xyz}

What am I doing wrong???

Defining variables on the views. You should define those variables in the controllers and then have them in the views or partials passing them as locals if necessary.

Ralph Shnelvar wrote in post #969835:

In HAML I have hundreds of lines like the following:

  - xyz = someFuncThatReturnsString('xyz')

and elsewhere

  %div{'id' => xyz}

The above lines work fine.

- - -

Attempting to keep things DRY (Don't repeat yourself) I want to do something like

  - eval(otherFuncThatReturnsString('xyz'))

where

  otherFuncThatReturnsString('xyz')

returns

  "xyz = someFuncThatReturnsString('xyz')"

When I do this, HAML no longer sees xyz as being defined when it attempts to interpret

  %div{'id' => xyz}

What am I doing wrong???

Almost everything. You're using eval (almost never necessary in Ruby -- send is usually what you want). You're using eval in the view (breaking MVC). You're using dynamic element IDs (questionable but sometimes necessary). You're using camelCase (poor style for Ruby).

And most importantly, you're asking a question that is so abstract that it has little to do with your actual goal.

So...what are you actually trying to achieve here? What's your *actual* code like? What errors or unexpected behavior are you getting?

Best,

it will help if you can post a 'gist'

https://gist.github.com

Marnen Laibow-Koser wrote in post #969841:

You're using eval (almost never necessary in Ruby -- send is usually what you want).

How in Ruby does one create a variable and then set a value for it without using eval?

That is, I have a string named "xyz" and I want to create a variable named xyz and assign the string "abc" to it.

Ralph Shnelvar wrote in post #970469:

Marnen Laibow-Koser wrote in post #969841:

You're using eval (almost never necessary in Ruby -- send is usually what you want).

How in Ruby does one create a variable and then set a value for it without using eval?

There are several methods to do just that. But I don't think that's actually what you want here.

That is, I have a string named "xyz" and I want to create a variable named xyz and assign the string "abc" to it.

As I said in my earlier post, I think you're approaching this wrong. Please give more information about what you're actually trying to achieve (rather than the surrogate goal you've set for yourself) and we'll see what we can come up with. (You probably don't need those variables in the first place.)

Best,