One-line assignment: If (condition) variable = value1 else value2 - ruby-on-rails

How do I do something like this?
if params[:property] == nil
#item.property = true
else
#item.property = false
Always forget the proper syntax to write it in one line.
In PHP it would be like this:
#item.property=(params[:property]==nil)true:false
Is it the same in rails?

use the ternary operator:
#item.property = params[:property] ? true : false
or force a boolean conversion ("not not" operation) :
#item.property = !!params[:property]
note : in ruby, it is a common idiom not to use true booleans, as any object other than false or nil evaluates to true.

m_x answer is perfect but you may be interested to know other ways to do it, which may look better in other circumstances:
if params[:property] == nil then #item.property = true else #item.property = false end
or
#item.property = if params[:property] == nil then true else false end

Related

im having troubles getting my toggle script working in roblox

i need help
this is the script:
script.Parent.Parent.Activated:connect(function()
local a = game.Workspace.LogRideToggled.Value
if a == true then
a = false
script.Parent.Click:Play()
end
if a == false then
a = true
script.Parent.Click:Play()
end
end)
this is the hirachy:
https://imgur.com/a/4FXHY
but NOTHING happens, no errors either, except for the click sound playing
i seriously need help
The problem is is that after you do a == true, you set a to false, which a == false then matches after.
You can solve this with an if then else end statement, like so:
script.Parent.Parent.Activated:connect(function()
local a = game.Workspace.LogRideToggled.Value
if a == true then
a = false
script.Parent.Click:Play()
else
a = true
script.Parent.Click:Play()
end
end)
However, this will only change the local value of a, which means that it will not save the change.
To fix this, we need to assign the game.Workspace.LogRideToggleds value of Value directly, which we can do like:
script.Parent.Parent.Activated:connect(function()
if game.Workspace.LogRideToggled.Value == true then
game.Workspace.LogRideToggled.Value = false
script.Parent.Click:Play()
else
game.Workspace.LogRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Although it's bad practice to repeatedly index this like this, so we can store game.Workspace.LogRideToggled in a local variable. You can read up on why this works but storing value doesn't here
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value == true then
logRideToggled.Value = false
script.Parent.Click:Play()
else
logRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Also, the == true is redundant, as Lua expects a truthy or falsey value as the condition, all == true does in this case is give true or false if it's true of false.
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value then
logRideToggled.Value = false
script.Parent.Click:Play()
else
logRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Yet we can clean this up a bit more, as we use script.Parent.Click:Play() in both cases, and we can replace logRideToggled.Value = with a logical not, like so.
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value then
-- Todo if truthy
else
-- Todo if falsey
end
logRideToggled.Value = not logRideToggled.Value
script.Parent.Click:Play()
end)
But if you only want to toggle this value, not do anything special for either case, we can remove that entire conditional, leaving:
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
logRideToggled.Value = not logRideToggled.Value
script.Parent.Click:Play()
end)
Hope this has helped!

How can find the missing bracket in this code?

When I run the code it tells me there's an error which is ')' expected near '=':
function restartLvl()
for i = 1, #balloonTexts do
display.remove(balloonTexts[i])
print ("restart level")
end
score.text = '0'
ballRemain.text = '3'
balloonText = {}
createBalloons(1, 3)
if (askUser.isVisible = true) then --this is the line where the error occured
askUser.isVisible = false
end
if (yesBtn.isVisible = true) then
yesBtn.isVisible = false
end
if (noBtn.isVisible = true) then
noBtn.isVisible = false
end
end
I don't know how it is still missing a ')', because I closed all the brackets.
= is the assignment operator, == is the operator to test equality. Change it to:
if (askUser.isVisible == true) then
askUser.isVisible = false
end
And all the others as well. The brackets () can be ommited for simplicity:
if askUser.isVisible == true then
askUser.isVisible = false
end
If the value is a boolean, you can also do this because all values that are not nil or false are treated as true.
if askUser.isVisible then
askUser.isVisible = false
end
This is not related to your answer but
I recommend you to use lua glider IDE because this type error can be detect well by using this IDE.

Ruby lazy if statement with no operator

Is it possible to do this in ruby?
variablename = true
if variablename
puts "yes!"
end
Instead of this
variablename = true
if variablename == true
puts "yes!"
end
Edit:
also considering having:
variablename = 0 #which caused my problem
I can't get that to work. Is such a style of saying if possible? I'm learning ruby now, and it is possible in PHP but im not sure how to do it right in ruby
sure, it's possible
everything except nil and false is treated as true in ruby. Meaning:
var = 0
if var
# true!
end
var = ''
if var
# true!
end
var = nil
if var
# false
end
xdazz and Vlad are correct with their answers, so you would need to catch 0 separately:
variable = false if variable.zero? # if you need 0 to be false
puts "yes!" if variable # now nil, false & 0 will be considered false
It's possible at all. In ruby, only nil and false is considered as false, any other value is true.

Could you explain what's !! do? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does !! mean in ruby?
I found !! in Paypal gem here: https://github.com/tc/paypal_adaptive/blob/master/lib/paypal_adaptive/config.rb
like 59
but I don't understand what it does.
I know that ! means NOT, but !! doesn't make sense.
here's the screen: http://tinyurl.com/7acklhr
It forces any value to true or false depending on its "truthy" nature.
This is simply because, as you've noted, ! is the Boolean-not operator. For instance:
t = 1
puts !t # => false
puts !!t # => true
f = nil
puts !f # => true
puts !!f # => false
The !! is used to return either true or false on something that returns anything :
In Ruby, everything other than nil and false is interpreted as true. But it will not return true, it will return the value.
So if you use !, you get true or false but the opposite value of what is really is.
If you use !!, you get the true or false corresponding value.
It is used to make sure its the boolean type.
Explanation more detailed
Eg:
!!active
=> true
active = false
=> false
!!active
=> false
active = nil
=> nil
!!active
=> false
This forces a result to be true or false. As in ruby nil is not exactly false this can be useful. For instance:
def try x
if x == 1
return nil
else
return "non-nil"
end
end
p "try1" if try(1) # here you get a string printed
p "try2" if !!try(1) # here you don't

Return false statement

I'm new to RoR; I want create the following statement. I've an array; I want that controller return false if all array elements are not equal to a variable value.This is the code
def check_warehouse(asset,serial)
wh = Warehouse.where(["(asset = ? and serial = ?) OR (asset = ?) OR (serial= ?)",asset,serial,asset,serial])
return false if wh.count > 1
return false if
wh.each do |wh|
wh.position_id != session[:position_id]
end
end
but it doesn't works!why?
Moreover, can you suggest me a plugin or gem running on Rails 3.1 for generate pdf from RoR datas?
Thank you all
You have this code:
return false if wh.each do |wh|
wh.position_id != session[:position_id]
end
This will not execute the way you want. In ruby, .each will execute the "block" (code between do/end) and return to you the original array.
So if wh is an array, empty or not, and you say:
return false if []
ruby will not return false. instead, you'd likely rather:
return false if wh.any? {|wh| wh.position_id != session[:position_id] }
You probably would want it to return true if the position is the session position, so you can switch to:
return wh.any?{|wh| wh.position_id == session[:position_id] }
Try smth like this:
def check_warehouse(asset,serial)
wh = Warehouse.where(["(asset = ?) OR (serial= ?)",asset,serial]) # first condition was just extra
return false if wh.detect {|wh| wh.position_id != session[:position_id] }
end
I removed return false if wh.count > 1 because there's no sense to check the array if you return if it has more than 1 element. Please tell me if I misunderstood you
UPD
Actually you can do that in the db:
def check_warehouse(asset,serial)
Warehouse.where(
["(asset = ? OR serial= ?) AND NOT position_id = ?", asset, serial, session[:position_id]]
).count.zero?
end

Resources