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
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'm doing a bit of code golfing, and I've gotten to this:
for i=1,100 do for j=1,i do if i%j<1 then io.write(j.." ") end end print() end
Is there any way to get the if statement as an inline? So that it's wrapped into the io.write()
The ternary operation in lua can be mimicked with and..or operators:
a and b or c
is similar to
a ? b : c
under truthy values. For your case:
for i=1,100 do
for j=1,i do
io.write( ((i % j) < 1 and j.." " or '')
-- (i % j) < 1 and io.write(.." ")
end
print() -- why print here?
end
golfed, it is
io.write(i%j<1 and j.." "or'')
which saves you 4 characters
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.
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.
Is there a Ruby shortcut for the following?
if (x > 2) and (x < 10)
do_something_here
end
I thought I saw something to that effect, but cannot find a reference to it. Of course it's hard to lookup when you don't know what operator you're looking for.
if (3..9).include? x
# whatever
end
As a sidenote, you can also use the triple equals operator for ranges:
if (3..9) === x
# whatever
end
This lets you use them in case statements as well:
case x
when 3..9
# Do something
when 10..17
# Do something else
end
do_something if (3..9).include?( x ) # inclusive
do_something if (3...10).include?( x ) # inclusive start, exclusive end
See the Range class; you can read an introduction to them hosted on my website.
Comparable#between?
do_something if x.between?(2, 10)
Something like this?
do_something if (3..9) === x
or
r = 3..9
if r === x
. . .