Patryk
Patryk

Reputation: 47

Ruby calling method from a variable [value(string)]

Ruby calling method from a variable [value(string)]

obj.get("GET A")

Method get opens a folder and checks the value for 'A' But if there is no folder I'd like to create it and call the same method again There is a lot of functions as this, so I created a lambda

$s1_exists = lambda do |inp|
 File.open(%Q{session-1.txt}, "w")
 puts "Method called on empty Databse, try again"
end

But instead of putting the information I want it to call:

obj.get("GET A")

Of course I could copy/paste the code for each method but it's bad practice... I tried somethig like this (in lambda):

1. obj_m = inp.downcase.split[0]
self.obj_m(inp)

2. obj_m = inp.downcase.split[0]
self.(:obj_m, inp)

3. obj_m = inp.downcase.split[0]
self.method(%Q{#{obj_m(inp)}})

I tried to use "GET" as a method, I believe, I've tried few more but I can't remember now. If you'v got any idead pls let me now Btw. I'm new to Ruby so any tips are appreciated.

Upvotes: 2

Views: 70

Answers (1)

Patryk
Patryk

Reputation: 47

Thank you @max pleaner For me worked

    $s1_exists = lambda do |inp=0|
        File.open(%Q{session-1.txt}, "w")
        meth_call = inp.downcase.split[0]
        obj = Db.new
        obj.public_send(meth_call, inp)
    end

Upvotes: 1

Related Questions