Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Calling a Class Method with a class name
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Torsten Robitzki  
View profile  
(1 user)  More options Jun 8 2006, 3:51 pm
Newsgroups: comp.lang.ruby
From: Torsten Robitzki <MyFirstn...@Robitzki.de>
Date: Thu, 08 Jun 2006 21:51:40 +0200
Local: Thurs, Jun 8 2006 3:51 pm
Subject: Calling a Class Method with a class name
Hi,
I'm looking for a way to call a Class Method with the class name being a
variable string and the method name is fixed. Something like :
s = "ClassName"
eval ("#{s}.name")

But unfortunately I have to call two such functions in chain and both
have a block as parameter, so eval would be my last choice. So I'm
looking for a way to call the function like I could call them if it
would be an Object Method:
Class.get_objet_by_name("ClassName").send(:name)

any suggestions?

best regards


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
premshree.pil...@gmail.com  
View profile  
(1 user)  More options Jun 8 2006, 4:14 pm
Newsgroups: comp.lang.ruby
From: premshree.pil...@gmail.com
Date: 8 Jun 2006 13:14:30 -0700
Local: Thurs, Jun 8 2006 4:14 pm
Subject: Re: Calling a Class Method with a class name

Torsten Robitzki wrote:
> But unfortunately I have to call two such functions in chain and both
> have a block as parameter, so eval would be my last choice. So I'm
> looking for a way to call the function like I could call them if it
> would be an Object Method:
> Class.get_objet_by_name("ClassName").send(:name)

Not sure if I understood correctly, but anyway...

   str = String.new("ruby")
   obj_name = "str"
   eval(obj_name).send("length")
   => 4

Would something like this work?

Premshree


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Torsten Robitzki  
View profile  
 More options Jun 8 2006, 4:38 pm
Newsgroups: comp.lang.ruby
From: Torsten Robitzki <MyFirstn...@Robitzki.de>
Date: Thu, 08 Jun 2006 22:38:28 +0200
Local: Thurs, Jun 8 2006 4:38 pm
Subject: Re: Calling a Class Method with a class name

premshree.pil...@gmail.com wrote:

Hi Premshree,

> Not sure if I understood correctly, but anyway...

For sure my fault ;-)

>    str = String.new("ruby")
>    obj_name = "str"
>    eval(obj_name).send("length")
>    => 4

> Would something like this work?

Jep, that's exactly what I was looking for. I wondered what Type the
expression /eval("str")/ would have and instead of asking here I
thought: "Hey that's ruby why not asking the result itself" =>
So :
puts eval("Test").inspect
Test

So am I'm right that eval("Classname") returns some kind of "default
initialized" Object of type Classname?

best regards and thanks,
Torsten


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Schierbeck  
View profile  
 More options Jun 8 2006, 5:22 pm
Newsgroups: comp.lang.ruby
From: Daniel Schierbeck <daniel.schierb...@gmail.com>
Date: Thu, 08 Jun 2006 23:22:43 +0200
Local: Thurs, Jun 8 2006 5:22 pm
Subject: Re: Calling a Class Method with a class name

Torsten Robitzki wrote:
> Hi,
> I'm looking for a way to call a Class Method with the class name being a
> variable string and the method name is fixed. Something like :
> s = "ClassName"
> eval ("#{s}.name")

> But unfortunately I have to call two such functions in chain and both
> have a block as parameter, so eval would be my last choice. So I'm
> looking for a way to call the function like I could call them if it
> would be an Object Method:
> Class.get_objet_by_name("ClassName").send(:name)

   Object.const_get("ClassName").foo

Cheers,
Daniel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Torsten Robitzki  
View profile  
 More options Jun 9 2006, 10:17 am
Newsgroups: comp.lang.ruby
From: Torsten Robitzki <MyFirstn...@Robitzki.de>
Date: Fri, 09 Jun 2006 16:17:14 +0200
Local: Fri, Jun 9 2006 10:17 am
Subject: Re: Calling a Class Method with a class name
Hi Daniel,

Daniel Schierbeck wrote:
>   Object.const_get("ClassName").foo

ri const_get
------------------------------------------------------- Module#const_get
      mod.const_get(sym)    => obj
------------------------------------------------------------------------
      Returns the value of the named constant in _mod_.

         Math.const_get(:PI)   #=> 3.14159265358979

Is a class name treated like a constant in ruyb? Or is my documentation
not up to date? Or is it just an undocumented feature?

Thanks,
Torsten


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Schierbeck  
View profile  
 More options Jun 9 2006, 12:14 pm
Newsgroups: comp.lang.ruby
From: Daniel Schierbeck <daniel.schierb...@gmail.com>
Date: Fri, 09 Jun 2006 18:14:45 +0200
Local: Fri, Jun 9 2006 12:14 pm
Subject: Re: Calling a Class Method with a class name

Torsten Robitzki wrote:
> Is a class name treated like a constant in ruyb? Or is my documentation
> not up to date? Or is it just an undocumented feature?

Both classes and modules are constants -- this is valid:

   Foo = Class.new{}
   Bar = Module.new{}

If you want to make sure the object returned by Module#const_get is a
class, you can just test its class:

   class Module
     def class_get(name)
       cl = const_get(name)
       unless cl.kind_of? Class
         raise TypeError, "`#{cl.inspect}' must be a Class"
       end
       return cl
     end
   end

of course, you could also just accept all objects that respond to #new,
which would be quackier:

   class Module
     def class_get(name)
       cl = const_get(name)
       unless cl.respond_to? :new
         raise TypeError, "`#{cl.inspect}' must respond to `new'"
       end
       return cl
     end
   end

Cheers,
Daniel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Torsten Robitzki  
View profile  
 More options Jun 9 2006, 5:32 pm
Newsgroups: comp.lang.ruby
From: Torsten Robitzki <MyFirstn...@Robitzki.de>
Date: Fri, 09 Jun 2006 23:32:56 +0200
Local: Fri, Jun 9 2006 5:32 pm
Subject: Re: Calling a Class Method with a class name

Daniel Schierbeck wrote:
> Torsten Robitzki wrote:

>> Is a class name treated like a constant in ruyb? Or is my
>> documentation not up to date? Or is it just an undocumented feature?

> Both classes and modules are constants -- this is valid:

>   Foo = Class.new{}
>   Bar = Module.new{}
> ...

Thank you for your detailed and very helpful reply. Tomorrow I will have
to travel by train, so that's a good opportunity to read my "Programming
Ruby" book ;-)

best regards
Torsten


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Schierbeck  
View profile  
 More options Jun 9 2006, 7:51 pm
Newsgroups: comp.lang.ruby
From: Daniel Schierbeck <daniel.schierb...@gmail.com>
Date: Sat, 10 Jun 2006 01:51:34 +0200
Local: Fri, Jun 9 2006 7:51 pm
Subject: Re: Calling a Class Method with a class name

Torsten Robitzki wrote:
> Thank you for your detailed and very helpful reply.

No problem at all :)

Daniel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google