acacia
acacia

Reputation: 1387

Syntax error using send method

I have a tiny but silly problem over here, i have a model action;

def self.price_change_up(network, currency, change )
    prices = Prices.where('network = ?', network) 
    prices.each do |price|
      price.send("#{currency}") = price.send("#{currency}") + change
      price.save
    end
  end

but the line

price.send("#{currency}") = price.send("#{currency}") + change

return a syntax error. What could be the problem.

Upvotes: 0

Views: 92

Answers (2)

Bijendra
Bijendra

Reputation: 10053

price.send("#{currency}") = price.send("#{currency}") + change

This is invalid, you can't use '=' to assign ,In case you are looking to use send with setter

 self.send("#{currency}=", prev_curr_value)

In the LHS, you are using price.send("#{currency}"), it calls a method dynamically and will return a value which is again being assigned to same value with some addition change which looks wierd. what are you trying to achieve here, explain with an updated qsn.

Upvotes: 1

Agis
Agis

Reputation: 33656

I suppose you want to do this instead:

price = price.send("#{currency}") + change

Your current example is invalid syntax because you are trying to assign something to a value, the return value of price.send("#{currency}") whatever may that be.

Upvotes: 1

Related Questions