I’m a beginner in Ruby with a little bit of experience programming in
Java and PHP. Right now I’m having a difficult time grasping conditional
(if/elsif) statements in Ruby. I’m trying to get an if statement to
evaluate two different strings…i.e.
action = gets.chomp()
if action == “M” || “m”
menu()
else
puts “Please enter in a valid selection.”
end
So, from what I’m used to in programming, the if action == “M” || “m”"
should evaluate to a conditional statement as to whether or not action
is equal to the strings “M” or “m”. However, this does not seem to be
the case. It’s not giving any kind of an error, however, it is calling
the menu() method regardless of what is entered. I can easily make this
work by separating the “M” and the “m” into an if and an elsif argument
i.e:
action = gets.chomp()
if action == “M”
menu()
elsif action == “m”
menu()
else
puts “Please enter in a valid selection.”
end
The above process does seem to work, however, isn’t there a simpler way
of accomplishing this so I’m not adding unnecessary conditional
statements? I’ve been trying to find more resources online about
conditional statements with multiple arguments, but none of them have
seemed to help out. I’m guessing that my understanding of the || and
“or” statements is misguided.
Any help would be really appreciated on this issue. Thanks!
Thank you very much for the quick response Allen. You are completely
right, that works perfectly. I guess in my head it didn’t make sense to
have to add the variable twice, but now it makes perfect sense. Thanks
again!
On Sat, Sep 24, 2011 at 11:06 AM, Thomas B. [email protected] wrote:
Thank you very much for the quick response Allen. You are completely
right, that works perfectly. I guess in my head it didn’t make sense to
have to add the variable twice, but now it makes perfect sense. Thanks
again!
if you want something similar to your original idea, you may need to
express it in regex form, like
case action
when “M”, “m”
puts “Found ‘M’ or ‘m’”
else
puts “Enter a valid selection”
end [email protected]:~$ ruby case.rb
A
Enter a valid selection [email protected]:~$ ruby case.rb
M
Found ‘M’ or ‘m’ [email protected]:~$ ruby case.rb
m
Found ‘M’ or ‘m’ [email protected]:~$
Of course, you could also have said:
if action.downcase == ‘m’
I’ve been trying to find more resources online about
conditional statements with multiple arguments, but none of them have
seemed to help out. I’m guessing that my understanding of the || and
“or” statements is misguided.
The specific error in your original code
your code => if action == “M” || “m”
is that this will be interpreted as
if (action == “M”) || “m”
and that a string “m” evaluates to true. So what you where saying is:
On Sat, Sep 24, 2011 at 11:19 AM, Thomas B. [email protected] wrote:
Ok, that makes a lot more sense. So basically, what you’re saying is if
I had a single argument in a conditional statement (even a variable?) it
would evaluate to true?
Unless it is “nil” or “false”
Sorry if this all sounds a bit stupid, but I’m just trying to fully
grasp the concept of how arguments are evaluated in conditional
statements.
Ok, that makes a lot more sense. So basically, what you’re saying is if
I had a single argument in a conditional statement (even a variable?) it
would evaluate to true?
I.e.:
x = 1
if x
puts x
end
If I’m understanding this correctly, the above statement will just
evaluate to true? Or is this only true with a string?
Sorry if this all sounds a bit stupid, but I’m just trying to fully
grasp the concept of how arguments are evaluated in conditional
statements.
edit:
Peter,
You are exactly correct that action.downcase == “m” or vice versa
action.upcase == “M” is the easiest way to accomplish it. After sitting
around on it for a couple of hours, I finally figured that out. Thank
you again to everyone for all of your help. Just so you can see what I
ended up going with, here’s the code!:
def lobby()
puts “Welcome to the lobby. This is the central starting point of”
puts “the game. Type in the letter ‘M’ at any time to bring up the”
puts “menu. If you would like to exit, please type ‘exit’.”
prompt()
action = gets.chomp()
if action.upcase == "M"
menu()
elsif action.downcase == "exit"
puts "Thank you for playing!"
exit()
else
puts "Please enter a valid selection"
end
Is there a reason that something like an empty array or an empty string
would evaluate to true as opposed to nil? Or is it just one of those
things that just is the way it is?
That’s it indeed … in more detail, only the singleton objects of
NilClass
and FalseClass are false, all the rest is true.
I know we’re getting a little bit into the bushes with this stuff, and I
should probably be a little bit more resourceful. It’s just interesting
to me that an empty array or string would evaluate to a true statement.
On Array and String, you could use empty? to that purpose:
What should it return if there is no such object in array? A
reasonable expectation is that it should return false or nothing (nil)
and that’s what it does on Ruby. However, for example in JavaScript,
false is that same as 0, and thus you wouldn’t know if it found the
value of not.
JavaScript solves this by returning -1 in such case, which is IMO a
terrible hack (function is arr.indexOf). PHP returns false/null and
expects you to compare the results using strict comparison operator,
=== (function called array_search), meaning you can’t just stuf it in
an if().
Of course, Ruby way can be awkward at times (when you need to, say,
make sure you have a string that that it’s not empty), but IMO it’s
still much cleaner.
Is there a reason that something like an empty array or an empty string
would evaluate to true as opposed to nil? Or is it just one of those
things that just is the way it is? I guess I just don’t understand the
purpose of Ruby handling an empty array the same way as it would handle
an array with a given value. Wouldn’t it just be easier to have the
empty array evaluate to nil like this:
array = []
puts array.nil? # returns => true (it actually returns false, because as
you said, it isn’t nil or false)
array = [1, 2]
puts array.nil? # returns => false
I know we’re getting a little bit into the bushes with this stuff, and I
should probably be a little bit more resourceful. It’s just interesting
to me that an empty array or string would evaluate to a true statement.
edit:
I’ll leave the above question just in case anyone has a similar
question…but it looks like I may have found my answer. So there’s both
the .nil? and .empty?.. .nil? will evaluate to true if the object it is
evaluating is nil. Where as .empty? will evaluate if the array or string
or hash contains no characters. Am I understanding this correctly?
Thank you to everyone for your responses and bearing with my very basic
Ruby questions. It has helped me a ton in understanding quite a few
different concepts that I’ve been having a hard time with. It’s
certainly nice to see that are plenty of people in the Ruby community
willing to help a beginner like myself.