Conditional statements with multiple arguments

Good afternoon everyone,

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!

Should be
If action == “M” or action == “m”

I think you can replace the word or with || if you want.

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

If action =~ /M|m/

On Sat, Sep 24, 2011 at 4:53 AM, Thomas B. [email protected] wrote:

menu()
i.e:
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 believe a nice way to achieve it could be the “case” statement.
That does allow you to give a list of strings to match against.

E.g.

[email protected]:~$ cat case.rb
action = gets.chomp()

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:

if (action is “M”) or true

which will be always true.

HTH,

Peter

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.

There are only 2 objects that are “false”:

  • false
  • nil

All the rest is “true”, e.g.

  • 0 # zero
  • [] # emtpy array
  • “” # empty string
    are all “true”

Peter

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

end

On Sat, Sep 24, 2011 at 1:19 PM, Thomas B. [email protected] wrote:

Peter,

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.

[email protected]:~$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]
[email protected]:~$ irb
001:0> false.class
=> FalseClass
002:0> nil.class
=> NilClass

I guess I just don’t understand the

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.

On Array and String, you could use empty? to that purpose:

006:0> a = Array.new
=> []
007:0> a.empty?
=> true
008:0> a << “test”
=> [“test”]
009:0> a.empty?
=> false

012:0> s = String.new
=> “”
013:0> s.empty?
=> true
014:0> s << “test”
=> “test”
015:0> s.empty?
=> false

But then again, this is an interesting case:

016:0> s = " "
=> " "
017:0> s.empty?
=> false
018:0> s.strip.empty?
=> true

All of this can be found in basic Ruby tutorials…

HTH,

Peter

There is a reason to evaluate, say, 0 to true.

For example, consider a function returning an index in an array where
you can find an object.

arr = [‘a’, ‘b’, ‘c’, ‘d’]
puts arr.index(‘c’) #=> 2
puts arr.index(‘a’) #=> 0

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.

– Matma R.

Peter,

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.

On Sat, Sep 24, 2011 at 7:10 PM, Intransition [email protected]
wrote:

if x.either == [1, 2]

You could do it with inlcude:
[1,2].include? x

cheers

I was thinking about this just yesterday. Sure would be nice if their
were a way to make dry’er considtions

if x == 1 or x == 2

vs.

if x == 1 or 2

Is there a way to do it?

if x.either == [1, 2]

On Sun, Sep 25, 2011 at 7:10 AM, Intransition [email protected]
wrote:

if x.either == [1, 2]

if x.in? 1, 2

On 09/24/2011 06:32 PM, botp wrote:

On Sun, Sep 25, 2011 at 7:10 AM, Intransition[email protected] wrote:

if x.either == [1, 2]
if x.in? 1, 2

Object#in? is an activesupport extension.

-Justin

On Sep 24, 7:25pm, Chris H. [email protected] wrote:

On Sat, Sep 24, 2011 at 7:10 PM, Intransition [email protected] wrote:

if x.either == [1, 2]

You could do it with inlcude:
[1,2].include? x

It’s more limited though. Consider:

if a.either.include? [1, 2]

if a.either.is_a? [String, Symbol]

etc.

On Sun, Sep 25, 2011 at 10:20 AM, Justin C.
[email protected] wrote:

Object#in? is an activesupport extension.

anyone can create it :wink:

On Sun, Sep 25, 2011 at 2:51 PM, Intransition [email protected]
wrote:

It’s more limited though. Consider:

if a.either.include? [1, 2]

if a.either.is_a? [String, Symbol]

etc.

I like it, but either implies 2 options. Seems weird to say

if a.either == [1, 2, 99, 501]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs