tags:

views:

140

answers:

3

Hi I have to made a generic function which is used for the purpose of calling many other functions depending upon the accesses im getting. For Ex:

def func1

   @access = ['public','private','protected']
   for acc in @access
       @temp = "get_"+acc[1]+"_data"
   end
end

def get_public_data
end

def get_private_data
end

def get_protected_data
end

but @temp is taking it as a string and assigning it to it. Please help in this ASAP.

Thank You

A: 

Don't know what language you are using, but some languages i.e. Perl have an eval() functions which does what you are trying.

If you use Perl or Python you might try something like

@temp = eval("get_"+acc[1]+"_data")

Peter
Thnx Buddy im developing it in Ruby On Rails
+2  A: 

In Ruby, methods are actually messages. So you can send the message name using, yup, the send method:

s = 'xyz'
i = s.send 'length'
# i = 3
Samir Talwar
+2  A: 

@temp = send "get_#{acc[1]}_data"

Use #{acc[1]} instead of "+"

aivarsak