[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how can I give a name to an anonymous class

sayoyo Sayoyo

1/11/2006 3:36:00 PM

Hi,

I'm try to create a generic anonymous class, and I want to set the
class name of the class, however I can't find a function which allow me
to do this, the only way I can do this is actually assigner the newly
create class to a constant. the classname will be (in my case)
GenActRec::className.

Does someone know how to remove the "GenActRec::" ???

Does someone know is there another way to do this????

this is my generic class:

class GenActRec < ActiveRecord::Base

def self.buildNewTableClass(className, tableName)
genClass = Class.new(GenActRec)
const_set("#{className.to_s}", genClass)
genClass.set_table_name tableName
genClass.reset_column_information()
return genClass
end

end

Thanks you very much

Sayoyo

14 Answers

Gary Wright

1/11/2006 3:59:00 PM

0


On Jan 11, 2006, at 10:38 AM, sayoyo@yahoo.com wrote:
> const_set("#{className.to_s}", genClass)

Just change this to:

Object.const_set("#{className.to_s}", genClass)

So that the constant is placed in the top-level instead of
in your class.


Gary Wright





Ara.T.Howard

1/11/2006 4:05:00 PM

0

Christian Neukirchen

1/11/2006 7:25:00 PM

0

gwtmp01@mac.com writes:

> On Jan 11, 2006, at 10:38 AM, sayoyo@yahoo.com wrote:
>> const_set("#{className.to_s}", genClass)
>
> Just change this to:
>
> Object.const_set("#{className.to_s}", genClass)
Object.const_set(className.to_s, genClass)

I think we should have "useless use of #{}"-awards. ;-)

> So that the constant is placed in the top-level instead of
> in your class.
>
>
> Gary Wright
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


David Vallner

1/11/2006 7:55:00 PM

0

Christian Neukirchen wrote:

>gwtmp01@mac.com writes:
>
>
>
>>On Jan 11, 2006, at 10:38 AM, sayoyo@yahoo.com wrote:
>>
>>
>>>const_set("#{className.to_s}", genClass)
>>>
>>>
>>Just change this to:
>>
>> Object.const_set("#{className.to_s}", genClass)
>>
>>
> Object.const_set(className.to_s, genClass)
>
>I think we should have "useless use of #{}"-awards. ;-)
>
>
>
And useless #to_s awards. If anything, #to_str is somewhat useful to get
a type check, but coercing types with reckless abandon you'll shoot
yourself in the foot when you least expect it.

David Vallner


Christian Neukirchen

1/12/2006 1:16:00 PM

0

David Vallner <david@vallner.net> writes:

> Christian Neukirchen wrote:
>
>>gwtmp01@mac.com writes:
>>
>>
>>
>>>On Jan 11, 2006, at 10:38 AM, sayoyo@yahoo.com wrote:
>>>
>>>
>>>>const_set("#{className.to_s}", genClass)
>>>>
>>>>
>>>Just change this to:
>>>
>>> Object.const_set("#{className.to_s}", genClass)
>>>
>>>
>> Object.const_set(className.to_s, genClass)
>>
>>I think we should have "useless use of #{}"-awards. ;-)
>>
>>
>>
> And useless #to_s awards. If anything, #to_str is somewhat useful to
> get a type check, but coercing types with reckless abandon you'll
> shoot yourself in the foot when you least expect it.

...except Symbol doesn't have #to_str.

> David Vallner
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


David Vallner

1/12/2006 1:23:00 PM

0

On Thu, 12 Jan 2006 14:15:38 +0100, Christian Neukirchen
<chneukirchen@gmail.com> wrote:

> David Vallner <david@vallner.net> writes:
>
>> Christian Neukirchen wrote:
>>
>>> gwtmp01@mac.com writes:
>>>
>>> Object.const_set(className.to_s, genClass)
>>>
>>> I think we should have "useless use of #{}"-awards. ;-)
>>>
>>>
>>>
>> And useless #to_s awards. If anything, #to_str is somewhat useful to
>> get a type check, but coercing types with reckless abandon you'll
>> shoot yourself in the foot when you least expect it.
>
> ...except Symbol doesn't have #to_str.
>

Oh, joy. Not like that matters in this specific case, but I can well
imagine that use. I guess it's time to dig out the metaprogramming helmets
to prevent severe trauma when banging heads against walls.

David Vallner


Robert Klemme

1/12/2006 2:11:00 PM

0

David Vallner wrote:
> Oh, joy. Not like that matters in this specific case, but I can well
> imagine that use. I guess it's time to dig out the metaprogramming
> helmets to prevent severe trauma when banging heads against walls.

Um, where do we get those? Couldn't find any at http://www.thi...
:-)

robert

Ara.T.Howard

1/12/2006 3:07:00 PM

0

James Gray

1/12/2006 3:15:00 PM

0

On Jan 12, 2006, at 9:06 AM, ara.t.howard@noaa.gov wrote:

> it's why i __always__ use
>
> "#{ obj }" # no useless after all

As far as I know, thats always equivalent to obj.to_s, which I think
is better looking:

>> class MyString
>> def to_s; "to_s called" end
>> def to_str; "to_str called" end
>> end
=> nil
>> my_string = MyString.new
=> to_s called
>> "#{my_string}"
=> "to_s called"
>> class MyString
>> undef to_s
>> end
=> nil
>> "#{my_string}"
NoMethodError: undefined method `to_s' for #<MyString:0x31d59c>
from /usr/local/lib/ruby/1.8/irb.rb:154:in `inspect'
from /usr/local/lib/ruby/1.8/irb.rb:154:in `eval_input'
from /usr/local/lib/ruby/1.8/irb.rb:259:in `signal_status'
from /usr/local/lib/ruby/1.8/irb.rb:147:in `eval_input'
from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:244:in
`each_top_level_statement'
from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:230:in
`each_top_level_statement'
from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:229:in
`each_top_level_statement'
from /usr/local/lib/ruby/1.8/irb.rb:146:in `eval_input'
from /usr/local/lib/ruby/1.8/irb.rb:70:in `start'
from /usr/local/lib/ruby/1.8/irb.rb:69:in `start'
from /usr/local/bin/irb:13
Maybe IRB bug!!

James Edward Gray II



Robert Klemme

1/12/2006 3:25:00 PM

0

James Edward Gray II wrote:
> On Jan 12, 2006, at 9:06 AM, ara.t.howard@noaa.gov wrote:
>
>> it's why i __always__ use
>>
>> "#{ obj }" # no useless after all
>
> As far as I know, thats always equivalent to obj.to_s, which I think
> is better looking:

.... and more efficient - especially if the object is a string already. :-)

robert