While I am new to Ruby, I have written a number of plugins for Ruby through trial and error.
I tried a number of different syntax variations which result in:
Unexpected else or end, expecting }- The variable not getting evaluated properly, i.e.
@reUrlwill end up ="@reUrl2"
Any help would be appreciated.
Here is an excerpt of my code:
def initialize(config)
self.reip1 = config["reip1"]
self.reip2 = config["reip2"]
@reUrl1 = "#{self.reip1}:8080/redeye/rooms/0/devices/2/commands/send?commandId="
@reUrl2 = "#{self.reip2}:8080/redeye/rooms/0/devices/2/commands/send?commandId="
@reUrl = @reUrl2
end
.
.
.
.
def change_redeye(redeye)
redeye = "#{redeye}".downcase
redeye = "#{redeye}".strip
redeyeid = "#{@redeyeId["#{redeye}"]}".to_i
if redeyeid > 0
say "OK. Changing to RedEye #{redeye}."
# results in resel = "@reUrl2" Does what I want.
resel = "@reUrl#{redeyeid}"
# FIXIT: need to end up with the URL of the device, but can't find the right syntax.
@reUrl = "#{#{resel}}"
else
say "Sorry, I am not programmed to control RedEye #{redeye}."
end
request_completed
end
tl;dr I finally figured out what you’re trying to do. While the below answer is still completely accurate in regards to your misuse of string interploation, what you’re actually after is
instance_variable_get. I was failing to grasp that you were trying to access the value of a variable whose name was stored in a string.There is a lot wrong here with your use of strings and interpolation. You only need to use
"#{}"when you want to embed a variable inside a string. You’re using it incorrectly in virtually every case. A few specific examples below.First, don’t do this:
For one, you’re converting to a string twice. It’s already a string after the first use of
#{}. But you shouldn’t be doing that in the first place. If you want to convert a variable to a string, useto_s, and chain your subsequent method calls on the return value of the previous method:This line is really broken, and (as far as I can tell) doesn’t even need string interpolation:
If
@redeyeIdis an array of integers, you just need this:This line might be syntactically correctly but it’s also pretty broken:This line is correct, if your intent is to useinstance_variable_getto access the value of@reUrl1or@reUrl2:You need to use
#{}to interpolate@variablemember variables as well as regular variables. The line should probably beThis line is also really broken:
What is this even supposed to do? I can’t for the life of me figure out what your intent is here. What you’re actually doing is, inside a string, opening a Ruby context which immediately opens a comment, resulting in the rest of the line being ignored and a syntax error on the following line. I can’t tell what you think this does, but it doesn’t do anything useful.
Because there is so much wrong here, I’m going to try to write the method as it should be written, and hope you can gain something from it. Before you write any more Ruby, you need to learn what
#{}does. It is string interpolation.