|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
||||
|
||||
|
Code:
#!/usr/bin/env ruby
def woo
puts "W00t!!"
end
send "woo"
eval "woo"
Quote:
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:
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. |
|
#3
|
|||
|
|||
|
Thanks for your help with the first part.
Quote:
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 |
|
#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
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Calling a function from a variable. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|