This is a much simpler example of what I'm trying to do in my program but is a similar idea. In an, if statement how do I say not equal to?
Is != correct?
def test
vara = 1
varb = 2
if vara == 1 && varb != 3
puts "correct"
else
puts "false"
end
end
Yes. In Ruby the not equal to operator is:
!=
You can get a full list of ruby operators here:
https://www.tutorialspoint.com/ruby/ruby_operators.htm.
Related
I have this loop:
puts "Welcome to the Loop Practice Problems"
puts " Write a number between 1 and 10, but not 5 or else...."
ans = gets.chomp!
if ans < 1
puts "Tf bruh bruh"
elsif ans > 10
puts "Now you just playin"
elsif x == 5
print "You wildin B"
else
puts "Fosho that's all I require"
end
It doesn't run properly, and I'm trying to understand why. If you can help me with this, I'd appreciate it.
If you know a good site for practice problems, I'd love to try it. I checked out Coderbyte and Code Kata, but the way they're set up doesn't look right, and they don't have questions to solve for fundamentals.
The issue here is that you're not converting ans to a number, but you're comparing it to one. ans is going to be a string.
In Ruby, when you compare a number to a string, Ruby says that the two aren't equal:
"1" == 1
=> false
You can reproduce the problem with this code:
puts "Welcome to the Loop Practice Problems"
puts " Write a number between 1 and 10, but not 5 or else...."
ans=gets.chomp!
p ans
The p method will output an "inspected" version of that object, (it's the same as doing puts ans.inspect). This will show it wrapped in quotes, which indicates that it's a string.
You probably want to do this:
ans = gets.chomp!.to_i
The to_i method here will convert the number to an integer, and then your comparisons will work correctly.
You have to convert input string type object into integer type
ans = gets.chomp!.to_i #input string convert into integer.
if ans < 1
puts "Tf bruh bruh"
elsif ans > 10
puts "Now you just playin"
elsif x == 5
print "You wildin B"
else
puts "Fosho that's all I require"
end
I am starting to learn programming and I have chosen to learn Ruby using Codecademy. However, I was trying to consolidate my learning but I just can't get this to work!
print "What is 2 + 2 ="
sum_var = gets.chomp
sum_var.downcase!
if sum_var == "four" || 4
puts "Correct!"
else sum_var != "four" || 4
puts "Wrong! #{sum_var} is not the answer!"
end
It just returns 'Correct!' even if it is wrong.
You need write the code as below :
print "What is 2 + 2 ="
sum_var = gets.chomp
# don't need to apply the bang method like you did - sum_var.downcase!
if sum_var.downcase == "four" || sum_var == '4'
puts "Correct!"
else # else don't need condition checking, so I removed.
puts "Wrong! #{sum_var} is not the answer!"
end
sum_var = gets.chomp gives you a string, no where you are converting it to a number. So, evenif you are passing number from the console it became "4" or "7" etc.
Let me explain you also why always you got "Correct!" as a output
sum_var == "four" || 4 - In this expression, whenever sum_var is not equal to "four", your first expression was evaluated to false, but when control went to test the second expression, it found there 4. You know in Ruby all objects are true, except nil and false. So 4 is considered as true. Thus in your code always if block was getting executed, and you were keep getting as the output "Correct!".
Now in your code, some other mess you did, that I corrected in my above code.
The problem with your code lies in the line
if sum_var == "four" || 4
The == will usually return false, so the second part will be evaluated because the precedence of == is higher than the precedence of ||. Since all objects except false and nil are "truthy" in Ruby the expression will end up being true regardless of the users input. Correction as proposed by #ArupRakshit, just wanted to add some more reason to it.
I am writing a code that is supposed to read from STDIN exactly n number of times.So lets say 3 times.What is the best way to do that ?
I tried this
counter = 0
while sentence = gets.chomp && counter < 3 do
...
counter += 1
end
but for some strange reason, sentence variable inside loop is Boolean ?
You can do as below:
n.times { sentence = gets.chomp }
or
n.times do
sentence = gets.chomp
# your code here
end
Operator precedence. The line:
while sentence = gets.chomp && counter < 3 do
Is being interpretted as
while sentence = ( gets.chomp && counter < 3 ) do
So, you could do this:
while ( sentence = gets.chomp ) && counter < 3 do
That explains why you got true or false values into sentence, and the third option should fix this, so your code is very close to working. However, it is probably more usual in Ruby to see solutions like Babai's
Can anyone please tell me what I should be using and why this method isn't correct? The code doesn't work, I know, I just want to know how I can interface with the array for this type of thing.
Thank you.
Code:
$i = 1
$f = 0
$c = 0
answer = ""
loop do
puts "Welcome\n"
puts "If you'd like to fly in first class, Press 1. For coach, Push 2."
answer = STDIN.gets
answer.chop!
break if answer =~ /1|2/
if answer == 1 then
$i += 1.each
$available.at(0..4)
end
if answer == 2 then
$i += 1.each
$available.at(5..19)
else
puts "Invalid number, enter 1 or 2."
Console_Screen.pause
end
end
puts "Here is your boarding pass. You are in seat " + $i.to_s
For first class, you can do something like this:
next_first = (0..4).find { |i| $available[i] != 1 }
If that gives you a next_available that is nil then first class is full, otherwise you have the index of the next available first class seat.
Similarly for coach:
next_coach = (5..19).find { |i| $available[i] != 1 }
Then once you have the index, have checked that it isn't nil, and stored it in $i, just $available[$i] = 1 to reserve the seat.
And an extra hint for free, this:
answer = STDIN.gets
answer.chop!
leaves a string in answer so answer == 1 will always be false.
References:
Enumerable#find
The problem seems to be here:
break if answer =~ /1|2/
This means "stop and leave the loop entirely if if we get a 1 or 2"
ie it means that none of your code that does seat-allocation gets executed if the person actually enters something valid (ie a 1 or 2).
After that comes the errors pointed out by #mu_is_too_short above.
Why is this not working?
case ARGV.length
when 0
abort "Error 1"
when > 2
abort "Error 2"
end
It's not valid ruby syntax.
What you need is
case
when ARGV.length == 0
abort "Error 1"
when ARGV.length > 2
abort "Error 2"
end
When you write case x, the important part you need to understand is that ruby takes the x and then applies a comparison to the argument or expressions you insert in the when clause.
The line where you say when x >2 reads to ruby like:
if ARGV.length == > 2
When you remove a specific object from the case statements, you can apply conditionals within the when statements .
Use 1.0 / 0.0 to get infinity, which fixes #mosch's code:
case ARGV.length
when 0
raise "Too few"
when 3..(1.0/0.0)
raise "Too many"
end
You don't have to be Chuck Norris to divide by a floating point zero.
Well, it doesn't work because it's not valid ruby syntax. However, you can do this:
x = 15
case x
when 0..9 then puts "good"
when 10..12 then puts "better"
when 13..200 then puts "best"
else
puts "either great or poor"
end
An if statement would probably be more fitting for your code, since you don't have a definitive range/value, but rather just a greater-than:
if ARGV.length == 0
abort "Error 1"
elsif ARGV.length > 2
abort "Error 2"
end
You can use either if elsif or lambda/proc (then notation is just for shortness):
case x
when 1 then '1'
when ->(a) { a > 2 } then '>2'
end
You can also create a named variable for clarity:
greater_than_2 = ->(a){ a > 2 }
case x
when 1 then '1'
when greater_than_2 then '>2'
end
You can also create your own comparison predicate:
greater_than = ->(n) { ->(a) {a > n} }
case x
when 1 then '1'
when greater_than[2] then '>2'
when greater_than[5] then '>5'
end
Answering your initial question when > 1 is not a valid syntax, since > is binary operator in Ruby, which is to say its arity 2, meaning it takes 2 arguments. Don't be confused of term binary vs bitwise.