Controller Instance Variables

Hi djolley. You can also use class variables or global variables:

test.rb:

$b='global value' class MyTest   @@a='class value'   def class_val     @@a   end   def global_val     $b   end end

test = MyTest.new puts test.class_val puts test.global_val

$ ruby test.rb class value global value

These variables will not be reset when a new instance is created. Haven't tested @@class_val in a view but it should work.

Regards, Morgan

djolley wrote:

Doug Jolley wrote:

because that will set the instance variable in the wrong object. you could use controller.instance_variable_set but that's just disgusting.

Well now, hold on there a minute, Fred. :slight_smile: I kind of like it. In fact, it's exactly what I asked for. I don't find it all that disgusting. Anyway, I now know how to do it both ways and I can decide which way I like the better. Thanks to all for the input in this topic.

          ... doug

If you want to be a good Rails citizen, I'd recommend following Fred's advice. He have you a clean and conventional solution and I guarantee you he knows what he's talking about. I'd have given you the same advice if the original posting was stated slightly more clearly.