I have this method it's very simple and almost all of the time the isTrue param should be false and return "2".
def test(isTrue = false)
isTrue ? 1 : 2
end
this works fine in my dev env but when I push it to heroku suddenly it starts returning as if it is true, and Im absolutly positive that its false. I think it possibly be checking if the var is nil ( or something like that)
I changed the ternary to:
isTrue == true ? 1 : 2
And it corrects the problem, I don't understand why this happens.
Can someone explain it?
thanks!
This is definitely wrong:
isTrue = true ? 1 : 2
It sets the variable isTrue to true and uses the result of that statement (true) as the input of the ternary operator, so this will always return true.
Change it to:
isTrue == true ? 1 : 2
Regarding the differences between development and production mode: check that you really feed booleans into the method and not integers (0 or 1), strings ('0', '1', 't', 'f', 'y', 'n', etc) or nil.
isTrue = true ? 1 : 2
this will always return 1 as Mark Meeus commented.
= is the assignment operator in ruby, used to make assign a variable a given value.
==, however is a comparison operator.
so with your code as it is currently, you're assigning "isTrue = true" and then telling the code to return 1 if isTrue is true.
Related
sorry for asking this question but I couldn't understand it
-- but i don't understand this code
ballDX = math.random(2) == 1 and 100 or -100
--here ballDY will give value between -50 to 50
ballDY = math.random(-50, 50)
I don't understand the structure what is (2) and why it's == 1
Thank you a lot
math.random(x) will randomly return an integer between 1 and x.
So math.random(2) will randomly return 1 or 2.
If it returns 1 (== 1), ballDX will be set to 100.
If it returns 2 (~= 1), ballDX will be set to -100.
A simple way to make a 50-50 chance.
That is a very common way of assigning variables in Lua based on conditionals. It’s the same you’d do, for example, in Python with “foo = a if x else b”:
The first function, math.random(2), returns either 1 or 2. So, if it returns 1 the part math.random(2) == 1 is true and so you assign 100 to the variable ballDX. Otherwise, assign -100 to it.
In lua
result = condition and first or second
basically means the same as
if condition and first ~= nil and first ~= false then
result = first
else
result = second
end
So in your case
if math.random(2) == 1 then
ballDX = 100
else
ballDX = -100
end
in other words, there is a 50/50 chance for ballDX to become 100 or -100
For a better understanding, a look at lua documentation helps a lot :
https://www.lua.org/pil/3.3.html
You can read:
The operator or returns its first argument if it is not false; otherwise, it returns its second argument:
So if the random number is 1 it will return the first argument (100) of the "or" otherwise it will return the second argument (-100).
Hello guys i please need your help.
I have values, most of them are numbers but some of them are strings.
How can i check if value is string or number?
I already tried this code but when it reach string value i get error " attempt to perform arithmetic on local 'numberValue' (a nil value)"
function Config:IsNumber(value)
if value ~=nill or value ~=nil then
local numberValue = tonumber(value)
if numberValue/numberValue ==1 then
return true
else
return false
end
end
end
end
end
First of all let's start with errors in your code.
You have 2 ends to many.
it is nil, not nill. I'm not sure where you're going with that check.
Other issues / things to improve:
numberValue / numberValue == 1 does not make any sense. A number dividided by itself always results in 1. So checking that is pointless.
Also instead of
if something == 1 then
return true
else
return false
end
Can simply be replaced by return something == 1. There is no need for a conditional statement here.
To your question:
To check wether a value is a string use type(value) == "string".
To check wether a value can be convertet do a number use tonumber(value). It will either return a number or nil.
So according to your error the conversion to a number failed.
If you don't know for sure that your value can be converted to a number, you should ensure that tonumber succeded befor you do any operations on its return value.
local numberValue = tonumber(value)
if not numberValue then
print("converting value to a number failed)
return
end
-- at this point you know numberValue is a number
So if you wanted to write a function that ensures a string represents a number you could do something like this:
function IsNumber(value)
return tonumber(value) and true or false
end
From the Lua 5.1 documentation for load():
Loads a chunk using function func to get its pieces. Each call to func must return a string that concatenates with previous results. A return of an empty string, nil, or no value signals the end of the chunk.
From my testing, this is not actually true. Or, rather, the documentation is at a minimum misleading.
Consider this example script:
function make_loader(return_at)
local x = 0
return function()
x = x + 1
if x == return_at then return 'return true' end
return nil
end
end
x = 0
repeat
x = x + 1
until not load(make_loader(x))()
print(x)
The output is the number of successive calls to the function returned by make_loader() that returned nil before load() gives up and returns a function returning nothing.
One would expect the output here to be "1" if the documentation is to be taken at face value. However, the output is "3". This implies that the argument to load() is called until it returns nil three times before load() gives up.
On the other hand, if the chunk function returns a string immediately and then nil on subsequent calls, it only takes one nil to stop loading:
function make_loader()
local x = 0
return {
fn=function()
x = x + 1
if x == 1 then return 'return true' end
return nil
end,
get_x=function() return x end
}
end
loader = make_loader()
load(loader.fn)
print(loader.get_x())
This prints "2" as I would expect.
So my question is: is the documentation wrong? Is this behavior desirable for some reason? Is this simply a bug in load()? (It seems to appear intentional, but I cannot find any documentation explaining why.)
This is a bug in 5.1. It has been corrected in 5.2, but we failed to incorporate the correction in 5.1.
I get slightly different results from yours, but they are still not quite what the documentation implies:
function make_loader(return_at)
local x = 0
return function()
x = x + 1
print("make_loader", return_at, x)
if x == return_at then return 'return true' end
return nil
end
end
for i = 1, 4 do
load(make_loader(i))
end
This returns the following results:
make_loader 1 1
make_loader 1 2
make_loader 2 1
make_loader 2 2
make_loader 2 3
make_loader 3 1
make_loader 3 2
make_loader 4 1
make_loader 4 2
For 1 it's called two times because the first one was return true and the second one nil. For 2 it's called three times because the first one was nil, then return true, and then nil again. For all other values it's called two times: it seems like the very first nil is ignored and the function is called at least once more.
If that's indeed the case, the documentation needs to reflect that. I looked at the source code, but didn't see anything that could explain why the first returned nil is ignored (I also tested with empty string and no value with the same result).
I have the following function in Lua:
function iffunc(k,str,str1)
if k ~= 0 then
return str .. k .. (str1 or "")
end
end
This function allows me to check if value k is populated or not. I'm actually using it to determine if I want to display something that has zero value. My problem is this: I'm trying to concatenate a string of iffunc(), but since some of the values are 0, it returns an error of trying to concatenate a nil value. For instance:
levellbon = iffunc(levellrep["BonusStr"],"#wStr#r{#x111","#r}") .. iffunc(levellrep["BonusInt"],"#wInt#r{#x111","#r}") .. iffunc(levellrep["BonusWis"],"#wWis#r{#x111","#r}")
If any of the table values are 0, it'll return the error. I could easily put 'return 0' in the iffunc itself; however, I don't want a string of 000, either. So how can I work it where no matter which values are nil, I won't get that error? Ultimately, I'm going to do an iffunc on the levellbon variable to see if that's populated or not, but I've got that part figured out. I just need to get past this little hurdle right now. Thanks!
I'd do this:
function iffunc(k,str,str1)
if k == 0 then return "" end
return str .. k .. (str1 or "")
end
You should add an else statement in the function, where you return an empty string ("").
I have seen code in lua like this
if (a==b or false) then
what is the purpose of the "or false"?
This is the original code:
Function = function(self, aura, auraID)
-- Store the version.
-- aura.Version = 50000;
-- Fix texmode/glow.
aura.texmode = (aura.texmode == 1 or aura.texmode == true or false);
-- Texture source.
if(aura.owntex or aura.SourceType == PowaAuras.SourceTypes.Icon) then
aura.SourceType = PowaAuras.SourceTypes.Icon;
elseif(aura.wowtex or aura.SourceType == PowaAuras.SourceTypes.WoW) then
aura.SourceType = PowaAuras.SourceTypes.WoW;
elseif(aura.customtex or aura.SourceType == PowaAuras.SourceTypes.Custom) then
aura.SourceType = PowaAuras.SourceTypes.Custom;
elseif(aura.textaura or aura.SourceType == PowaAuras.SourceTypes.Text) then
aura.SourceType = PowaAuras.SourceTypes.Text;
else
aura.SourceType = PowaAuras.SourceTypes.Default;
end
end
And here is another example
-- Iterate over element children.
visible, hidden = self:UpdateScrollList(key, level+1, visible, hidden,
(parentShowing == true and item:GetExpanded() or false));
This looks like a behavior that comes from the syntax:
a=b or false
to initialize a to the value of b or false if b isn't defined.
Within the if statement as you wrote it, I don't see any purpose since the == is evaluated before the or. If they used parenthesis to change the order of operations, then it could be used to validate that b has been defined, e.g.:
> a=nil
> b=nil
> if (a == (b or false)) then print("yikes") else print("aok") end
aok
> if (a == b or false) then print("yikes") else print("aok") end
yikes
It is used to temporarily disable a block of Code.
It's semantics are technically the same as:
#if 0
dead code goes here ...
#endif
It's to explicitly state the value assigned to the expression when none of its prior conditions are true.
In Lua, the and and or expressions return the values that determine their truth values, rather than the flat truth value itself, like this:
function And(A,B)
if A then return B --if A is true, then the agreement of both depends on B
else return A end --otherwise, they're not, because A wasn't
end
function Or(a,b)
if A then return A --if A is true, then it doesn't matter if B was true
else return B end --otherwise, the truth hinges on B's value
end
In Lua, all values other than false and nil evaluate to true in conditional constructions, so this behavior is commonly used as an idiom to describe default values:
local color = color or 'blue' -- if `color` is defined to any value other than
-- `false` (such as any string), it will evaluate
-- as true, and the value will be returned from
-- the `or` statement (so `color` will be assigned
-- its own value and no change will result) -
-- if `color` is not defined, the `or` statement
-- will evaluate its `nil` value as false and
-- return the second value, 'blue'
This idiom can be combined and extended in various ways:
local color = color or self.color
or (favorites and favorites.color)
or (type(favourites)=='table' and favourites.colour)
or get_defaults('color')
or {'red','blue','green','yellow'}[math.random(1,4)]
Traditionally, though, they stay fairly simple, with maybe a few chained or cases before the default value.
That's what we're seeing here: the cases where or false is being used are for the last-case value for the expression.
So, while this:
aura.texmode = (aura.texmode == 1 or aura.texmode == true or false);
could have been written as this with exactly the same effect (since the == comparisons will return either true or false):
aura.texmode = (aura.texmode == 1 or aura.texmode == true);
adding the final or false makes it more obvious when looking at that line that the value can be false.
The same goes for the second example, in which the value is being assigned to an argument.
There is no use, because if a~=b then it will try false, which is ... false and return false anyway ...