I had this code:
if @locale
HighVoltage.content_path = "#{@template_to_use}/pages/#{@locale}/"
else
HighVoltage.content_path = 'pages/'
end
Now I have this version:
HighVoltage.content_path = @locale ?
"#{@template_to_use}/pages/#{@locale}/" :
'pages/'
What would be the suggested way to write this piece of code, first or second version or may be other
In my opinion here is the best option:
HighVoltage.content_path =
if @locale
"#{@template_to_use}/pages/#{@locale}/"
else
'pages/'
end
You don't repeat yourself with HighVoltage.content_path =
as in the first example that you have provided. BUT some people do find my approach to look a little bit ugly compared to this one. So you could use it if you believe the same.
You better not to use A ? B : C
statement if it will take multiple lines.
See https://github.com/bbatsov/ruby-style-guide#use-if-case-returns for further reference.
According to the ruby style guide this is prefered
HighVoltage.content_path =
if @locale
"#{@template_to_use}/pages/#{@locale}/"
else
'pages/'
end
https://github.com/bbatsov/ruby-style-guide#use-if-case-returns
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With