Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using structs in Ruby on Rails gives dynamic constant assignment (SyntaxError)

Tags:

ruby

struct

In my controller I have the following simplified code:

def index   @dashboard_items = []   DashItem = Struct.new(:name, :amount, :moderated)  # Error is here    [:page, :post].each do |c|     obj = c.to_s.capitalize.constantize     @dashboard_items << DashItem.new(c.to_s, obj.count, obj.count_moderated)   end end 

But Ruby gives the following error:

dynamic constant assignment (SyntaxError)

on the line marked above.

Which, AFAIK, means that the constant DashItem is already defined. Is this correct? And what to do about it?

like image 566
berkes Avatar asked Jan 12 '11 15:01

berkes


2 Answers

The error explains what the problem is - you have a constant being assigned in a context that's too dynamic - i.e. inside the index method.

The solution is to define it outside:

DashItem = Struct.new(:name, :amount, :moderated) def index   @dashboard_items = []   ... 
like image 62
gunn Avatar answered Sep 25 '22 01:09

gunn


If you want to keep the whole thing neatly inside your index method you could do this:

def index   @dashboard_items = []   # Set the name of your struct class as the first argument   Struct.new('DashItem', :name, :amount, :moderated)   ...   # Then when you want to create an instance of your structure   # you can access your class within the Struct class   @dashboard_items << Struct::DashItem.new(c.to_s, obj.count, obj.moderated) end 

As gunn said, you just can't explicitly assign a constant within a method like that...

This solution is explained more in the ruby docs here, second example on the page.

like image 38
Lexun Avatar answered Sep 23 '22 01:09

Lexun