windinhishair
windinhishair

Reputation: 13

are there smarter ways to compse string for eval in ruby

Say some looks like below

def fun s1,s2
  #code to process s1, s2
end

var1 = "value for s1"
var2 = "value for s2"
str_to_run = "fun '#{var1}', '#{var2}'"
eval str_to_run

My question is: are there any other way to compose string for dynamically run?

Upvotes: 1

Views: 60

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118261

Just do use #public_send.

str_to_run = public_send "fun", var1, var2

Upvotes: 2

Related Questions