Forum: Ruby Using a result as a constant object?

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.
555c1193071e97926fa67c038244c047?d=identicon&s=25 Phy Prabab (Guest)
on 2008-10-18 23:13
(Received via mailing list)
Hello,

I am trying to understand how I can use a return from a method as a
constant name for a class.  What I am trying to do is take a return I
find in a db table and use that name as a class name so that I can use
activerecord.  I admit my understanding of ActiveRecord is extremely
immature so perhaps I am using it wrong.
...
perMatrtix = subMatrix.find[:space].to.upcase

class perMatrix < ActiveRecord::Base; end

blah, blah, blah...

So the return of the find, which is a name (turn to upper) as the name
of a class so that I can access that table within the database.

Any help is greatly appreciated!  Cheers,
Phy
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2008-10-18 23:55
(Received via mailing list)
Phy Prabab wrote:
> So the return of the find, which is a name (turn to upper) as the name of a class so 
that I can access that table within the database.
>

If you're getting the class name at run-time how would you write code
that uses class after you've created it? I mean, you can't type the
class name into your program now, you don't know what it is?

I suspect that what you really want to do is simply create a class and
assign the result to a variable. Check out Class.new.

my_class = Class.new(ActiveRecord::Base)
my_inst = my_class.new
D7a447c6de706527716327f002f9b1f6?d=identicon&s=25 Artem Voroztsov (Guest)
on 2008-10-19 10:29
(Received via mailing list)
2008/10/19 Tim Hunter <TimHunter@nc.rr.com>:
>> perMatrtix = subMatrix.find[:space].to.upcase
> uses class after you've created it? I mean, you can't type the class name
>
> --
> RMagick: http://rmagick.rubyforge.org/
>
>

  my_class = Class.new(ActiveRecord::Base) {  set_table_name
subMatrix.find[:space].underscore.pluralize  }
  my_inst = my_class.new

Artem

PS:
Expression  "subMatrix.find[:space]" seems strange for me. What is it
about?
5acba71393125496e93b9e434d8b63c9?d=identicon&s=25 Sjoerd Andringa (s-andringa)
on 2008-10-19 12:05
If the ActiveRecord model already exists you can just call constantize
on the string containing the model name:

class SomeModel < ActiveRecord::Base; end

model_name = "SomeModel" # or in your case fetch this from a database
ar_model = model_name.constantize
ar.model.find(120)
5acba71393125496e93b9e434d8b63c9?d=identicon&s=25 Sjoerd Andringa (s-andringa)
on 2008-10-19 12:06
Sjoerd Andringa wrote:
> ar.model.find(120)
Should be: ar_model.find(120)
5acba71393125496e93b9e434d8b63c9?d=identicon&s=25 Sjoerd Andringa (s-andringa)
on 2008-10-19 12:09
Sjoerd Andringa wrote:
> Sjoerd Andringa wrote:
>> ar.model.find(120)
> Should be: ar_model.find(120)

Be aware that 'constantize' is a method added by ActiveSupport, no
problem if you're using Rails, but you need to require it manually
otherwise.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-19 19:04
Phy Prabab wrote:
> I am trying to understand how I can use a return from a method as a
> constant name for a class.

def get_name
  "Foo"
end

n = get_name
c = Class.new(ActiveRecord::Base)   # superclass
Object.const_set(n, c)
555c1193071e97926fa67c038244c047?d=identicon&s=25 Phy Prabab (Guest)
on 2008-10-19 20:22
(Received via mailing list)
Brian,

Ah, I see now (said the blind man)!  This cool!  Thanks for the example,
much appreciated!

Cheers,
Phy
555c1193071e97926fa67c038244c047?d=identicon&s=25 Phy Prabab (Guest)
on 2008-10-19 20:23
(Received via mailing list)
Sjoerd,

Thanks much for the clarification.  Indeed this works well.  Appreciate
the help!

Cheers,
Phy
555c1193071e97926fa67c038244c047?d=identicon&s=25 Phy Prabab (Guest)
on 2008-10-19 20:32
(Received via mailing list)
Artem,

Thank you much for the example.  This truly helps.  WRT you question,
its a typo and basically its a method that retrieves the various
submatrix of a giant sparse matrix.  I am using a db to organize all the
data so that I can run the calculations simultaneously across several
hundred machines.  The orchestration of data and control flow of
processing I am trying to do in ruby.

Again, thanks for help!
Cheers,
Phy
This topic is locked and can not be replied to.