Forum: Ruby how can I give a name to an anonymous class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
14b7344777a56ac9a1fad849f5d6da2a?d=identicon&s=25 unknown (Guest)
on 2006-01-11 16:40
(Received via mailing list)
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
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2006-01-11 17:01
(Received via mailing list)
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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-01-11 17:07
(Received via mailing list)
On Thu, 12 Jan 2006 sayoyo@yahoo.com wrote:

> Does someone know is there another way to do this????
> 		return genClass
> 	end
>
> end

   harp:~ > cat a.rb
   class A
     c = Class::new do
       def foo
         42
       end
     end

     ::B = c
     Object::const_set 'B2', c
   end

   p B::new.foo
   p B2::new.foo


   harp:~ > ruby a.rb
   42
   42


-a
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-01-11 20:25
(Received via mailing list)
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.  ;-)
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-11 20:56
(Received via mailing list)
Christian Neukirchen wrote:

>>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
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-01-12 14:16
(Received via mailing list)
David Vallner <david@vallner.net> writes:

>>>>
>>
>>
> 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.
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-12 14:26
(Received via mailing list)
On Thu, 12 Jan 2006 14:15:38 +0100, Christian Neukirchen
<chneukirchen@gmail.com> wrote:

>>>
>>>
>> 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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-12 15:13
(Received via mailing list)
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.thinkgeek.com
:-)

    robert
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-01-12 16:08
(Received via mailing list)
On Thu, 12 Jan 2006, 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.

it's why i __always__ use

   "#{ obj }"       # no useless after all

instead of obj.to_s or obj.to_str.  it works - forget about it.  same
goes for

   [ obj ].flatten

when a list is needed.

-a
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-12 16:17
(Received via mailing list)
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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-12 16:29
(Received via mailing list)
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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-01-12 16:48
(Received via mailing list)
On Fri, 13 Jan 2006, 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:

ya know - you are right.  for some reason i seemed to remember the
default
to_s going away with the default to_a - it seems it did not.

time to make the coffee!

-a
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-12 17:00
(Received via mailing list)
On Jan 12, 2006, at 9:47 AM, ara.t.howard@noaa.gov wrote:

> ya know - you are right.  for some reason i seemed to remember the
> default
> to_s going away with the default to_a - it seems it did not.

Even if it did, "#{...}" doesn't call to_str().  ;)

James Edward Gray II
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-01-12 17:49
(Received via mailing list)
"David Vallner" <david@vallner.net> writes:

> helmets  to prevent severe trauma when banging heads against walls.
Sorry.  I really wonder what I was thinking when I wrote that.
It has to be like that, of course:

    Object.const_set(className.to_sym, genClass)

This allows for passing of strings and symbols.  (And Fixnums, oh well.)
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-12 23:11
(Received via mailing list)
On Thu, 12 Jan 2006 17:48:35 +0100, Christian Neukirchen
<chneukirchen@gmail.com> wrote:

>
> Sorry.  I really wonder what I was thinking when I wrote that.
> It has to be like that, of course:
>
>     Object.const_set(className.to_sym, genClass)
>
> This allows for passing of strings and symbols.  (And Fixnums, oh well.)
>

Hoolay! Look, mom, no type coercion! The little Java demons in the back
of
my skull cackle with glee at yet another potential obscure bug avoided.
(Happily ignoring any potential obscure bugs introduced, of course).

David Vallner
This topic is locked and can not be replied to.