I came across an error: attempt to compare boolean with number with the following code:
local x = get_x_from_db() -- x maybe -2, -1 or integer like 12345
if 0 < x < 128 then
-- do something
end
What causes this error? Thanks.
writing 0 < x < 128 is okay in Python, but not in Lua.
So, when your code is executed, Lua will first calculate if 0 < x is true. If it is true, then the comparison becomes true < 128, which is obviously the reason of the error message.
To make it work, you have to write:
if x < 128 and x > 0 then
--do something
end
0 < x < 128 is equivalent to (0 < x) < 128), hence the error message.
Write the test as 0 < x and x < 128.
Related
Let's say I have a method
def check_discrepancy(a, b)
if a < b-5 || a > b+5
return 'discrepancy between values!'
end
end
The if statement will be true if the difference between A and B is greater than 5, the code works as intended, but I want to know if there is a different/cleaner way to do it in Ruby
I am working on a Ruby challenge for work, and I am unable to create a working method. Every method I try keeps returning "nil".
Here is the question:
Create a method that passes an integer argument to a single parameter. If the integer is greater than 0 print the numbers from the integer to 0. If the number is less than 0 simply print the integer. Use a for loop, while loop, or unless loop to print the range of numbers from the integer to 0.
For example:
sample(4)
output = 3, 2, 1
sample(-1)
output = -1
Here is the code I tried to use
def countdown(n)
loop do
n -= 1
print "#{n}"
break if n <= 0
end
countdown(4)
A method returns the results of the last statement executed. Your loop is returning nil:
def countdown(n)
x = loop do
n -= 1
puts "#{n}"
break if n <= 0
end
x
end
countdown(4)
3
2
1
0
=> nil
Now let's return something:
def countdown(n)
loop do
puts "#{n}"
break if n <= 0
n -= 1
end
"okay we're done"
end
countdown(4)
4
3
2
1
0
=> "okay we're done"
It's not necessary to print inside the function and also outside it - this will cause duplicate printing. Also you are calling print on the positive numbers but not calling print if they are negative or zero. Additionally, you are using print "#{n}" which is the same as print n.
As far as the title of your question goes - "keeps returning nil" - you can change your approach a bit to do the print calls outside the function.
def countdown(n)
n <= 1 ? [n] : (n-1).downto(1).to_a
end
print countdown(n).join(", ")
Try this:
def countdown(n)
n.downto(n > 0 ? 0 : n) { |i| puts i }
end
countdown(4)
# 4
# 3
# 2
# 1
# 0
countdown(-4)
# -4
countdown(0)
# 0
You didn't mention what is to be done if the argument is zero. I've assumed it's treated as a positive or negative number.
Admittedly, this is cheating, as it does not "Use a for loop, while loop, or unless loop...", but Ruby is designed mainly to use iterators and blocks. This, or something like it, is the way to go. I just had a thought: treat that as a suggestion, not a requirement.
By the way, among loops, Kernel#loop was not mentioned, which is strange, as it is quite useful. As for "for loops", who uses them? I never have, not even once.
If you must use a loop, you could do the following.
def countdown(n)
while n > 0
puts n
n-= 1
end
puts n
end
countdown(4)
# 4
# 3
# 2
# 1
# 0
countdown(-4)
# -4
countdown(0)
# 0
You may try this...
def sample (a)
if a > 0
(1..a).to_a.reverse
else
a
end
end
Hope this will work for you
In comparison, instead of using 1 < i && i < 10 can we use 1 < i < 10? As you can see the latter saves space and the readability is increased.
Is this possible in any programming language?
EDIT:
In Javascript, 1 < i < 10 will always return true regardless of what i equals. For example,1 < 44 < 10 returns true.
JavaScript does not support it. For example, -3 < -2 < -1 results in false. For positive values, it sometimes "looks" like it is working, but there is implicit type conversion going on. For example, 3 < 4 < 5 gives true, but this value really comes from (3<4)<5, which is equal to true < 5, which is equal to 1 < 5 === true.
This is supported in Python:
Comparisons can be chained arbitrarily; for example, x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
Example:
1 < 2 < 3 # true (correct) - equivalent to 1 < 2 and 2 < 3
1 < 4 < 3 # false (correct) - equivalent to 1 < 4 and 4 < 3
(1 < 4) < 3 # true ("incorrect")
3 > 2 > 1 # true (correct) - equivalent to 3 < 2 and 2 < 1
3 > 4 > 1 # false (correct) - equivalent to 3 > 4 and 4 > 1
3 > (4 > 1) # true ("incorrect")
This feature is merely an artifact of a particular programming language (e.g Python) and most languages (e.g. JavaScript) only treat/parse comparison operators (i.e. <) as a leftward-associative binary operators.
As such, where such syntax is even well-typed, a < b < c is generally not a "range" comparisons, but with few exceptions (e.g. Python), is parsed as (a < b) < c.
I created Float class in lib folder:
class Float
def precision(p = 2)
# Make sure the precision level is actually an integer and > 0
raise ArgumentError, "#{p} is an invalid precision level. Valid ranges are integers > 0." unless p.class == Fixnum or p < 0
# Special case for 0 precision so it returns a Fixnum and thus doesn't have a trailing .0
return self.round if p == 0
# Standard case
(self * 10**p).round.to_f / 10**p
end
end
In rspec tests, works. But when the application is running, this error is raised:
undefined method `precision' for 5128.5:Float
How to make this override work?
Ruby already implements a round method for Float. There is no need for your implementation.
0.12345.round(2) # => 0.12
0.12345.round(3) # => 0.123
I think this should do it.
module MyFloatMod
def precision(p = 2)
# Make sure the precision level is actually an integer and > 0
raise ArgumentError, "#{p} is an invalid precision level. Valid ranges are integers > 0." unless p.class == Fixnum or p < 0
# Special case for 0 precision so it returns a Fixnum and thus doesn't have a trailing .0
return self.round if p == 0
# Standard case
(self * 10**p).round.to_f / 10**p
end
end
Float.send(:include, MyFloatMod)
EDIT: Almost forgot you also need to make sure this all gets included somewhere during your app's start up.
I'm using ar-extensions to extend the ability of doing conditions in ruby on rails. I am trying to use _ne or _not but it won't work. I can use _lt or _gt. Anyone have ideas?
LHS: normal comparison
RHS: equivalent comparison using only < operator
A < B :: A < B
A > B :: B < A
A != B :: A < B || B < A