#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    173
    Rep Power
    10

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Code:
    #!/usr/bin/env ruby
    
    
    def woo
            puts "W00t!!"
    end
    
    send "woo"
    eval "woo"
    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
    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"
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    173
    Rep Power
    10
    Thanks for your help with the first part.

    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    173
    Rep Power
    10
    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

IMN logo majestic logo threadwatch logo seochat tools logo