Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 15th, 2008, 08:58 AM
g_hadgraft g_hadgraft is online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 169 g_hadgraft User rank is Private First Class (20 - 50 Reputation Level)g_hadgraft User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 10 h 51 m 14 sec
Reputation Power: 4
Calling a function from a variable.

Hi i am trying to do wsomething in rails i assume is possible as it is in many other languages i want to call a function using a variable if that makes sense. e.g.

Code:
def helloWorld
  puts "Hello World"
end 

func_name = "helloWorld"

func_name();


should result in helloworld being run.

I have had a limited amount of success with this.

Code:
@object = NewsController.new()
@snippet = @object.send $2, @variables


Now i may want to run methods in a variaty of controller. In fact i have a variable which endsup holding news how could i create an object using the variable to find which oject to create?

I hope that all makes sense?

Reply With Quote
  #2  
Old July 15th, 2008, 12:40 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 642 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 48 m 20 sec
Reputation Power: 102
Send a message via AIM to L7Sqr
Code:
#!/usr/bin/env ruby


def woo
        puts "W00t!!"
end

send "woo"
eval "woo"


Quote:
Originally Posted by output
W00t!!
W00t!!


I would not suggest the second form (eval) - since it is inherently dangerous. However, if you do not know the context in which the method is defined, you may not be able to send it.

As for your second question
Code:
class Junk
    def me
        puts self.class
    end
end

j = Junk.new

class Trunk < j.class
end

f = Trunk.new
j.me
f.me


Quote:
Originally Posted by output
Junk
Trunk


That might give you want you want. If not, then you can always revert back to the ever dangerous eval
Code:
f = eval "#{j.class}.new"
__________________
-- I'll provide you with reference points; if they dont work, refer to something else.

If you process text, this might make your life a little easier.

Reply With Quote
  #3  
Old July 15th, 2008, 02:51 PM
g_hadgraft g_hadgraft is online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 169 g_hadgraft User rank is Private First Class (20 - 50 Reputation Level)g_hadgraft User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 10 h 51 m 14 sec
Reputation Power: 4
Thanks for your help with the first part.

Quote:
Originally Posted by L7Sqr
As for your second question
Code:
class Junk
    def me
        puts self.class
    end
end

j = Junk.new

class Trunk < j.class
end

f = Trunk.new
j.me
f.me

[/code]


As for this unless im missing something, which is definatly possible. I dont see how this helps me. As again it look like i create the functions without using a variable. Let me see if i can explain what i want to do.

Using your example let me try to explain what i want. I have some logic which will may give me two different strings. e.g.

Code:
if logged in
  var = "Trunk"
else 
  var = "Junk"
end


using the variable i want to create an object of Trunk or Junk. I could use an if else statement but due to the nature of what i am trying to do this is impractical as this is primarily to call a class for a plugin system, so everytime a new plugin is created a new elsif statement would need to be added so i want to create an object whose name is stored in a variable.

im not really seeing how your example achieves this. I could be completely wrong and thats exactly what it is doing but i cant see it.

Thanks for your help so far though L7Sqr

Reply With Quote
  #4  
Old July 15th, 2008, 03:53 PM
g_hadgraft g_hadgraft is online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 169 g_hadgraft User rank is Private First Class (20 - 50 Reputation Level)g_hadgraft User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 10 h 51 m 14 sec
Reputation Power: 4
Thanks for your help L7Sqr

Using your pointers i was able to get it working, I had to use eval to create the statement but now it works.

heres what i eneded up with.

Code:
	@object = eval "#{namecase($1)}Controller.new"
	@snippet = @object.send $2, @variables

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Calling a function from a variable.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT