1

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

4
  • Please give us some more info and an example. Commented May 18, 2009 at 14:01
  • 1
    Its ruby, or at least seams to be with the @var_name for member variables of a class and with the end blocks and lack of colons Commented May 18, 2009 at 14:07
  • I hope you realize acc[1] will be "u" for "public" and "r" for both "private" and "protected". Perhaps you just meant acc. Commented May 18, 2009 at 18:10
  • Well, actually, for Ruby versions prior to 1.9, it will return 117 for "public" and 114 for the latter two, which is even more nonsensical. Commented May 18, 2009 at 18:13

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

0

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_"+acc1+"_data")

1 Comment

Thnx Buddy im developing it in Ruby On Rails

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.