From: Josh Cheek Date: 2012-01-27T12:20:08+09:00 Subject: Re: A little assistance please :) --f46d04447e2b0e7b6304b779f9b4 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Jan 25, 2012 at 9:35 PM, Chad Perrin wrote: > On Thu, Jan 26, 2012 at 03:43:59AM +0900, Dave Aronson wrote: > > On Tue, Jan 24, 2012 at 23:32, Chad Perrin wrote: > > > > > Because self already has meaning in Ruby, when you try to define a > class > > > like this: > > > > > > class self.Startup > > > > > > . . . Ruby is trying to call a Startup method on the Object object (no, > > > that repetition wasn't accidental -- there's an object called Object) > and > > > use the return value of that method as the name of the class you're > > > trying to define. > > > > So if I had a class with a method that returned a string, I could use > > that where the name is expected in a class declaration? That doesn't > > seem right. Tried it in irb, using both a class method (Foo.bar) and > > an instance method (foo.bar), and neither worked. > > No, you couldn't do that either. I'm explaining why you got the error > Ruby gave you, not giving you suggestions for how to name classes in > horrible programmatic ways. If you *did* want to name a class in a > horrible programmatic way, you'd have to do something like: > > bar = "Hello" + "World" > > foo = < class #{bar} > def initialize(name='World') > @name = name > end > > def greet > return "Hello, #{@name}!" > end > end > EOF > > eval(foo) > > hello = HelloWorld.new > hello.greet > > There may be easier ways to achieve the same end but, in general, don't > do it. It'll probably just get your code in trouble. > > -- > Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] > > It's incredibly rare to need to resort to string evaluation. This example can be rewritten as: bar = "Hello" + "World" klass = Class.new do def initialize(name='World') @name = name end def greet return "Hello, #{@name}" end end Object.const_set bar, klass --f46d04447e2b0e7b6304b779f9b4--