I’m trying to ‘name’ a new class, using i varaible.
From antoher thread i got som starters regarding the use och ‘const_set’,
and it works fine while i try this:
name = "Person"
attributes = [:fname, :age]
klass = Object.const_set name, Struct.new(*attributes)
p = Person.new("John Doe", 42) # => #<struct Person name="John Doe", age=42>
But now i’d like to use the ‘name’-varible a bit differently, like changing the value it holds, etc. (which actually will be a Constant..?)
I’m retreiving a new value externally to a string called titleString..
But when i try to pass it’s value to ‘name’
name = "#{titleString}"
attributes = [:fname, :age]
klass = Object.const_set name, Struct.new(*attributes)
i end up with an “wrong constant name”-error..
How come?
Are you getting titleString from an input such as gets? There may be a newline hanging out at the end of titleString.