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.
Related
for i, v in pairs(Buttons:GetChildren()) do
local NewItem = BoughtItems:FindFirstChild(v.Item.value)
if NewItem ~= nil then
Items[NewItem.Name] = NewItem:Clone()
NewItem:Destroy()
else
v.ButtonPart.Transparency = 1
v.ButtonPart.CanCollide = false
v.ButtonPart.BillBoardGui.Frame.Visible = false
end
if v:FindFirstChild("Dependency") then
coroutine.resume(coroutine.create(function(
v.ButtonPart.Transparency = 1
v.ButtonPart.CanCollide = false
v.ButtonPart.BillBoardGui.Frame.Visible = false
if BoughtItems:WaitForChild(v.Dependency.Value, 100000)then
v.ButtonPart.Transparency = 0
v.ButtonPart.CanCollide = true
v.ButtonPart.BillBoardGui.Frame.Visible = true
end
end))
end
v.ButtonPart.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid")then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Values.OwnerValue.Value == Player then
if v.ButtonPart.CanCollide == true and v.ButtonPart.Transparency == 0 then
if Player:WaitForChild("leaderstats").Cash.Value >= v.Price.Value then
Player.leaderstats.Cash.Value -= v.Price.Value
Items[v.Item.Value].Parent = BoughtItems
v:Destroy()
end
end
end
end
end)
end
how do i fix this the header is the problem i dont knw what it means so is there a fix to it for my tycoon if anyone knows pls comment (heres some random stuff since my post is mostly code) A brief Introduction to Copypasta. Copypasta means to copy a text or a part of the text from an existing manuscript and inclusion of that very text in an under-process manuscript by pasting. At present, the societies are going to be expanded with an enormous pattern.
Parenthesis always come in pairs. The error message already strongly suggests that you are missing a closing parenthesis for an opening one in line 38.
1 2 3
coroutine.resume(coroutine.create(function(
v.ButtonPart.Transparency = 1
v.ButtonPart.CanCollide = false
v.ButtonPart.BillBoardGui.Frame.Visible = false
if BoughtItems:WaitForChild(v.Dependency.Value, 100000)then
v.ButtonPart.Transparency = 0
v.ButtonPart.CanCollide = true
v.ButtonPart.BillBoardGui.Frame.Visible = true
end
end)) <--- one is missing
1 2
Use a text editor that autocompletes pairs or even better one that hightlights pairs in matching colors to avoid errors like this.
Is there a way to get 1 with true and 0 with false in Lua?
There is the tobool which gives true or false with 1 or 0, but tonumber gives a nil value with true or false.
You can combine and and or clauses like ternary operators.
function bool_to_number(value)
return value and 1 or 0
end
You can also do this:
bool_to_number={ [true]=1, [false]=0 }
print(bool_to_number[value])
Or this:
debug.setmetatable(true, {__len = function (value) return value and 1 or 0 end})
print(#true)
print(#false)
The answer from hjpotter92 takes any value different to nil as a true value (returning 1). This instead takes the value true or false.
local value = true
print(value == true and 1 or value == false and 0)
-- we add the false check because it would drop 0 in case it was nil
If you want to use a function instead, this would be it
local value = true
local function bool_to_number(value)
return value == true and 1 or value == false and 0
end
print(bool_to_number(value))
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.
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
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