I have been working my way around a game in swift and just want to create this statement:
if (player1!=null)||(player2!=null)
println("You are out of the game")
I've already declared a variable for player1 and player2 but how do I go around this?
Write it in Swift instead of C:
if player1 != nil || player2 != nil {
println("You are out of the game")
}
Related
I am a programming neophyte so I apologize if this is more simple than it seems. I've searched around and only found individual answers for each_with_index and conditions, never together.
My goal is to iterate through an array and perform a single action based on the index. My first draft looked a little something like this:
#word_array.each_with_index do |word, index|
if index % 2 == 0
word = "EVEN"
end
end
Which works, but then I wanted to refactor it into a single line. My first attempt invoked an error due to it anticipating the else : part of the condition:
#word_array.each_with_index { |word, index| index % 2 == 0 ? word = "EVEN" }
I had tried it this way first thinking I may have some luck being as the block did not require an else condition, but obviously to no avail. I did discover I could use nil as a placeholder and have the code work:
#word_array.each_with_index { |word, index| index % 2 == 0 ? word = "EVEN" : nil }
However even after further reading up on each piece I'm uncertain whether or not having the nil there could potentially cause troubles in more complex situations even though it doesn't hamper my basic example here.
So to break it down into two questions:
1.) Is the nil going to bite me in the butt later?
2.) If so, is there a way to go about this using one line, or is my best bet to stick with the conditional block?
If you want this one-liner style:
word = 'EVEN' if index % 2 == 0
Using a ternary with a missing condition leaves people wondering why you didn't do this in the first place. A ternary is a great way to branch, like:
(index % 2 == 0) ? 'EVEN' : word
#word_array.each_with_index { |word, index| (index % 2 == 0) ? word = "EVEN" : nil }
should work. The ternary operator is easily confused.
1.) Is the nil going to bite me in the butt later?
Yes. Why not:
#word_array.each_with_index { |word, index| word = (index % 2 == 0) ? "EVEN" : "ODD" }
Rspec question
having
puts x.y
x.y = nil
I want to test if this line has been executed
so something like
allow(x).to receive(:y) { 'abc' }
and then
expect(x).to have_received(:y).with nil
but with(nil) doesn't work
any suggestions?
I want to make sure y is set to nil, but I cannot just check the value since I am stubbing x.y beforehand
You are attempting to assert that
x.y(nil)
is called.
To assert that the method y= is called with nil parameter, you'd want to use the :y= symbol instead.
allow(x).to receive(:y=){ "xyz" }
expect(x).to have_received(:y=).with(nil)
I have created a tic-tac-toe program in Ruby. I am having trouble with the part of the code which allows the game to end in a tie.
I wrote an if-statement to check for when a player wins the game. This is my else condition. When I try and run the program I get an error. What's wrong?
else
while #turn == "x" or "o"
#square_count -= 1 # I set empty_count to 9 in the initialize of the class
# of this program. This would minus 1 from empty) count_each every turn
end
if #square_count == 0 #when all the slots are taken, its a tie game
puts "Tie game!"
return true #this makes the program end
end
The error I am getting is:
tac.rb:89: warning: string literal in condition
tac.rb:88:in `block in check_win': undefined method `-' for nil:NilClass (NoMethodError)
from tac.rb:77:in `each'
from tac.rb:77:in `check_win'
from tac.rb:108:in `<class:Game>'
from tac.rb:1:in `<main>'
It looks like #square_count might not be assigned a value, so you may be getting the error you indicate, as without an assignment, #square_count will hold a nil value.
Also, your while statement may not be what you want, the while statement will currently always evaluate as 'true' because of the value "o".
Replace
while #turn == "x" or "o"
with
while #turn == "x" or #turn == "o"
I just tried to run some code that looked like this
def get_proj4(srid, type=nil)
type.downcase! if type
case type
when nil || "epsg"
open("http://spatialreference.org/ref/epsg/#{srid}/proj4/").read
when "esri"
open("http://spatialreference.org/ref/esri/#{srid}/proj4/").read
end
end
and it didn't run properly, returning nil every time. wrapping the nil || "epsg" in parentheses didn't work either
It turns out ruby wouldn't allow me to use the || operator in this
Now I assume ruby takes the case/when method and ultimately breaks it down into a group of conditionals looking something like
x = type
if x == (nil || "epsg")
y = ...runs code...
elsif x == "esri"
y = ...
end
x = nil
y
but obviously it does not. What's going on here?
Thanks
The expression is evaluated first so when nil || "espg" is equivalent to when "espg"1 - it will never match nil.
To match either-or, separate the options with a comma:
case type
when nil, "espg" ..
when "esri" ..
Or, alternatively, perhaps normalize the value:
case (type || "espg")
when "espg" ..
when "esri" ..
Or use the other form that resembles an if-else:
case
when type.nil? || type == "espg" ..
when type == "esri" ..
Or some combination of everything :)
1 This is also the same reason why the example if is suspect. It should probably be written like:
if type.nil? || type == "espg"
I always meet this Ruby problem, I want to write it more cleanly.
var a can be nil
a.value can also be nil
a.value has possible true or false value
if (not a.nil?) && (not a.value.nil?) && a.value == false
puts "a value is not available"
else
puts "a value is true"
end
The problem is that the conditional statement is too clumsy and hard to read.
How can I improve the checking nil and false conditional statement?
Thanks, I am a Ruby newbie
Ruby on rails has an extension called try which allows you to write:
if a.try(:value) == false
which is very clean. Without try, you can just write
if a && a.value == false
If a.value is nil, it is not false, so that is ok :)
If it is possible that a.value is not defined (which would raise an exception), I would write that as follows:
if a && a.respond_to?(:value) && a.value == false
[UPDATE: after ruby 2.3]
Since ruby 2.3 there is an even shorter version:
if a&.value == false
which is almost equivalent to a.try(:value) (but is pure ruby). Differences:
if value does not exist, the &. operator will throw, try will just return nil (preferable or not?)(note: try! would also throw).
when cascading try or &. they also handle false differently. This follows logically from previous difference, try will return nil, while &. will throw because false knows no methods :P
You can achieve it in more compacted way using Safe Navigation Operator (&.):
if a&.value == false
Source : http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/
if a && a.value!=false
puts "a value is true"
else
puts "a value is not available"
end
or just
puts a && a.value!=false ? "a value is true" : "a value is not available"
The simplest and cleanest way is to flip it and reverse it. Check for the truthy value rather than the falsey values
if a && a.value
puts "a value is true"
else
puts "a value is not available"
end
Of course in Rails you could do it either way by using blank? or present?
Your condition is redundant. If a.value is to be false, then it would not be nil.
if a.nil?.! && a.value == false
puts "a value is not available"
else
puts "a value is true"
end
This will always return a boolean, if nil it will return false; if false it returns false; if true returns true. Try it out, it's nice and short:
!!a.try(:value) == false