I started learning ruby today and I'm having a hard time understanding symbols and how to use them properly.
Right now I'm working on a simple server and website, and when I'm testing on the remote server I use the :TLSv1
certificate, and on my local machine the :SSLv23
certificate just because it works without having anything else.
So to not have to change the certificate when testing on those different environments -- because I can forget to change -- I tried to set up a flag for them like this:
if ENV['environment'] == 'development'
ssl_method = ":SSLv23"
else
ssl_method = ":TLSv1"
end
response = HTTParty.get('mywebsite.com/', :ssl_version => ssl_methos.to_sym)
It's not working. It returns this error when I'm trying to connect to localhost:
undefined local variable or method `ssl_method' for #<service> Did you mean? method
What alternative can I use?
UPDATE
Well, I dont know if there is a better alternative so I changed to this and worked.
if ENV['environment'] == 'development'
$ssl_method = "SSLv23"
else
$ssl_method = "TLSv1"
end
response = HTTParty.get('mywebsite.com/', :ssl_version => $ssl_method.to_sym)
I forgot to remove the :
from the string and I had to use the $
to define as a global variable. I am a python guy so I never thought that a global variable here would be different.
Something like this should work:
ssl_method =
if ENV['environment'] == 'development'
:SSLv23
else
:TLSv1
end
response = HTTParty.get('mywebsite.com/', :ssl_version => ssl_method)
The 2 ssl methods are just symbols in my example. In your get
call you were just casting them back to symbols with to_sym
, but you don't need to convert them if you initialize them as symbols.
Your code sample has a typo (methos
), but I think I know the root cause of the error (or at least have a reasonable guess because I did the same thing when I was testing the behavior).
In your code snippet you used ssl_method
(singular, for the 2 assignments), but in your get
call you tried to call/look for the ssl_methods
variable (plural, in the HTTParty call).
Since ssl_methods
wasn't ever assigned as a variable or defined as a method Ruby thought you might have meant to call the method
method (which is actually a method in ruby)