comparing two strings in ruby
From what you printed, it seems var2
is an array containing one string. Or actually, it appears to hold the result of running .inspect
on an array containing one string. It would be helpful to show how you are initializing them.
irb(main):005:0* v1 = "test"
=> "test"
irb(main):006:0> v2 = ["test"]
=> ["test"]
irb(main):007:0> v3 = v2.inspect
=> "[\"test\"]"
irb(main):008:0> puts v1,v2,v3
test
test
["test"]
Comparing two strings using (greater than sign) in Ruby?
String
includes the Comparable
module, which defines <
, >
, >=
, etc, based on the base class's compare (<=>
) method. So if string a comes alphabetically prior to string b, a <=> b
returns -1
, and <
returns true
. The same <=>
method is used for sorting strings, so you can imagine that in a sorted array of strings, each string is 'less than' its neighbor to the right.
Finding the difference between strings in Ruby
All of the solutions so far ignore the fact that the second array can also have elements that the first array doesn't have. Chuck has pointed out a fix (see comments on other posts), but there is a more elegant solution if you work with sets:
require 'set'
teamOne = "Billy, Frankie, Stevie, John"
teamTwo = "Billy, Frankie, Stevie, Zach"
teamOneSet = teamOne.split(', ').to_set
teamTwoSet = teamTwo.split(', ').to_set
teamOneSet ^ teamTwoSet # => #<Set: {"John", "Zach"}>
This set can then be converted back to an array if need be.
ruby comparing two identical strings as a condition for if else
You probably meant to use and
not &&
:
puts 'User directory header pass' and $log.info("User directory header pass")
which evaluates puts
and $log.info()
separately.
The form you use evaluates like:
puts ('User directory header pass' && $log.info("User directory header pass"))
And is same as
puts (true)
I'm not sure though why you have to join two statements like that when you can just do
puts 'User directory header pass'
$log.info("User directory header pass")
String || comparison operator in Ruby
'X' || 'O'
just says X or O. And since any string is truthy it always returns X. So any spot where you’ve said [('X' || 'O')]
, you’ve really just said ['X']
.
Because of this, you’re only ever checking if a whole line of 3 is all X.
I don’t really understand how you’re trying to test this, but I feel like you’d be better off running the function twice first handing in X and then handing in O if it didn’t find X as a winner, as opposed to trying to check both as once.
Alternatively you could instead have the function return 'X', 'O', or nil and then you could have it only run the function once. The string returned would be who won and if it’s nil then no one won. I would also recommend making a loop for this. I find it easier to read.
Here's how I would solve the problem.
ROWS = [
[1,2,3],
[4,5,6],
[7,8,9],
[1,4,7],
[2,5,8],
[3,6,9],
[1,5,9],
[7,5,3],
]
def find_winner(board)
ROWS.each do |row|
# subtract 1 because the ROWS is 1-indexed (like a phone dial) but arrays are 0-indexed
row_values = row.map { |v| board[v - 1] }
return('X') if row_values.all?('X')
return('O') if row_values.all?('O')
end
return(nil)
end
test1 = [
'X', 'X', 'X',
'O', 'X', 'O',
'O', 'O', '',
]
puts "Winner of test1: #{find_winner(test1).inspect}"
"X"
test2 = [
'X', '', 'X',
'X', 'O', 'O',
'X', 'O', 'X',
]
puts "Winner of test2: #{find_winner(test2).inspect}"
"X"
test3 = [
'O', 'X', 'X',
'O', 'X', 'O',
'O', 'O', '',
]
puts "Winner of test3: #{find_winner(test3).inspect}"
"O"
test4 = [
'O', 'X', 'O',
'X', 'O', 'X',
'O', 'O', 'X',
]
puts "Winner of test4: #{find_winner(test4).inspect}"
"O"
test5 = [
'O', 'X', 'O',
'O', 'X', 'O',
'X', 'O', 'X',
]
puts "Winner of test5: #{find_winner(test5).inspect}"
nil
Related Topics
Why Are Symbols Not Frozen Strings
Accessing Ruby Class Variables with Class_Eval and Instance_Eval
Best Place to Store Model Specific Constants in Rails 3.1
Rails, Ruby, How to Sort an Array
How to Force Activerecord to Reload a Class
Building a Hash in a Conditional Way
Rails Collection_Select VS. Select
How to Use Capybara in Pure Ruby (Without Rails)
Renaming Uploaded Files with Carrierwave
Compressing Large String in Ruby