keruilin
keruilin

Reputation: 17502

Variable method calls - proper Ruby syntax?

I want to do this:

for u in users
  ['four', 'five', 'six', 'seven', 'eight'].each do |n|
    win_streak = u."#{n}_win_streak"
  end
end

But am get this: syntax error, unexpected tSTRING_BEG.

What is the proper syntax?

Upvotes: 2

Views: 75

Answers (1)

hammar
hammar

Reputation: 139830

Use the Object#send method:

win_streak = u.send("#{n}_win_streak")

Upvotes: 3

Related Questions