2

Since I hate calling methods with boolean literals that don't say anything about their intent I tried defining a constant in my eruby template as follows: <% NO_NIL_PADDING = false %> for a call to in_groups_of. However this resulted in a dynamic constant assignment error. Any way to get around this? I could probably define the constant in the environment somewhere, but atm I'd prefer to keep the constant definition as close to its usage as possible until it starts getting necessary to move its definition to a higher level.

Sam
  • 14,642
  • 6
  • 27
  • 39

1 Answers1

0

I don't know what else you're doing in the template that might be causing it, but just setting the constant works for me:

ruby-1.8.7-p249 > template = ERB.new "<% DONT_SET_CONSTANTS_IN_VIEWS = false %>"
 => #<ERB:0x100485890 @src="_erbout = '';  DONT_SET_CONSTANTS_IN_VIEWS = false ; _erbout", @filename=nil, @safe_level=nil> 
ruby-1.8.7-p249 > template.result(binding)
 => "" 

on a side note, however, I'd strongly recommend not doing this. As someone who's inherited a lot of code in the last year, you're making serious headaches for yourself and potentially someone else. Is an option like that actually likely to change per business logic? I would try to pick a sane default and just use it. If its not being used anywhere else, why create a constant?

Keith Gaddis
  • 4,113
  • 23
  • 20
  • 1
    I just want to write this `@some_list.in_groups_of(2, NO_NIL_PADDING)` instead of `@some_list.in_groups_of(2, false)` – Sam Nov 29 '10 at 15:16
  • 1
    I'd create yourself a class in an initializer that contains constants for options like that. That way you always know where to go change it and it stays maintainable, and you bypass the dynamic constant assignment problem. – Keith Gaddis Nov 29 '10 at 17:44