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!

···

--
Posted via http://www.ruby-forum.com/.

Should be
If action == "M" or action == "m"

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

···

On Sep 24, 2011 10:54 AM, "Thomas B." <sinixlol@gmail.com> wrote:

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!

--
Posted via http://www.ruby-forum.com/\.

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!

···

--
Posted via http://www.ruby-forum.com/.

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 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.

peterv@ASUS:~$ cat case.rb
action = gets.chomp()

case action
when "M", "m"
  puts "Found 'M' or 'm'"
else
  puts "Enter a valid selection"
end
peterv@ASUS:~$ ruby case.rb
A
Enter a valid selection
peterv@ASUS:~$ ruby case.rb
M
Found 'M' or 'm'
peterv@ASUS:~$ ruby case.rb
m
Found 'M' or 'm'
peterv@ASUS:~$

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 4:53 AM, Thomas B. <sinixlol@gmail.com> 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?

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.

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

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]

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 11:06 AM, Thomas B. <sinixlol@gmail.com> 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!

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"

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.

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

···

On Sat, Sep 24, 2011 at 11:19 AM, Thomas B. <sinixlol@gmail.com> 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.

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

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.

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

···

On Sat, Sep 24, 2011 at 1:19 PM, Thomas B. <sinixlol@gmail.com> wrote:

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

cheers

···

On Sat, Sep 24, 2011 at 7:10 PM, Intransition <transfire@gmail.com> wrote:

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]

if x.in? 1, 2

···

On Sun, Sep 25, 2011 at 7:10 AM, Intransition <transfire@gmail.com> wrote:

if x.either == [1, 2]

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 Rex

Object#in? is an activesupport extension.

-Justin

···

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

On Sun, Sep 25, 2011 at 7:10 AM, Intransition<transfire@gmail.com> wrote:

  if x.either == [1, 2]

if x.in? 1, 2

It's more limited though. Consider:

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

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

etc.

···

On Sep 24, 7:25 pm, Chris Hulan <chris.hu...@gmail.com> wrote:

On Sat, Sep 24, 2011 at 7:10 PM, Intransition <transf...@gmail.com> wrote:

> if x.either == [1, 2]

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

anyone can create it :wink:

···

On Sun, Sep 25, 2011 at 10:20 AM, Justin Collins <justincollins@ucla.edu> wrote:

Object#in? is an activesupport extension.

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

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

···

On Sun, Sep 25, 2011 at 2:51 PM, Intransition <transfire@gmail.com> wrote:

On Sep 24, 7:25 pm, Chris Hulan <chris.hu...@gmail.com> wrote:
> On Sat, Sep 24, 2011 at 7:10 PM, Intransition <transf...@gmail.com> > 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.