7stud -- wrote:
You didn't learn your lessons very well. Look up what the method to_s
does. Does calling to_s on a string do anything? Experiment.
You're right, no sense to use it there - it was a leftover from my
previous experiments that I forgot to remove.
This isn't a Rails forum.
The question was purely ruby-related, the point is that my classes _are_
defined, and class definition is not a problem in this case.
I came across .constantize after searching for const_get as phrogz
suggested.
If you are discarding the ruby solutions that were proffered in favor of
rails specific solutions, then why not just go to the rails forum?
In any case, if you look at the definition of the constantize method, it
just calls the ruby method module_eval, and module_eval seems to act
like const_get in your situation, although module_eval can also do some
other things.
Thanks for clarifying that - however, i was not discarding the Ruby
solution, but rather picking one that seemed to be more suitable in my
situation.... I could be wrong, of course - hence the "learning"
part....
You don't seem to understand the difference between a variable name and
a string. The most obvious difference is that a string has quotes
around it.
Yes, that was exactly one of the problems, which is why i had to use
eval() - otherwise my subsequent .length method returned the number of
characters in the string 'foo' as opposed as the number of items in the
array foo.
The eval method says to treat a string as if it is ruby code.
Ah! that makes sense now. thanks.
However, you should avoid using eval whenever possible. First, it's
slow. Second, someone could enter a string that contains a command to
erase your hard drive. When you eval that string, poof!
Good to know... i'm trying to rewrite it without using eval... it's just
the only thing so far that seems to turn 'foo' into foo...
By the way, are you aware that your arrays can contain the class objects
themselves rather than strings:
Yes, this is how i initially started. but then i ran into a reverse
issue: i had to turn that class' name into a name of the array, one
item of which would be passed as an option to each new instance of the
class:
class Item
def initialize(x)
@property = x
end
end
class Dog < Item
end
class Flower < Item
end
class Circle < Item
end
#here are the arrays holding the future values of @property
dog = ['jump', 'bark']
flower = ['red', 'yellow', 'blue', 'green']
circle = ['large', 'medium', small']
#and here's the array that holds class names
arr = [Dog, Flower, Circle]
#now, create instances of the 3 classes,
#while reading the value for @property from the corresponding array,
#to be set on each object's instantiation.
#the result should be (in this case) 2 instances of Dog, 4 of Flower,
and 3 of Circle, each instance with its own @property.
arr.each do |a_class|
length = 0
until length == a_class.length #<==== here's the problem.
obj = a_class.new(:x => a_class[length]) #<=== and again.
length += 1
end
end
so, my issue here then is translating a_class into its corresponding
variable name for each iteration of arr.each, so that it reads the item
number [length] from its corresponding array. the only way i could
think of doing this is by using a_class.to_s.downcase, but of course ran
into the problem of 1) a_class being a class, so to_s method won't
return what I need, and 2) even if I were to somehow get the name of
this class and managed to .to_s.downcase it, i'd have to use eval()
again to make it a variable....
Thus, i had to populate my arr with lowercase strings, instead of the
(more elegant) class names, like you suggest.....
···
--
Posted via http://www.ruby-forum.com/\.