Possible to make compound if statements

Thanks Hal. Question: What do you mean by use ||? Could you give an example? (I’m new to Ruby and programming.)

-Kurt

References: 80945 </cgi-bin/scat.rb/ruby/ruby-talk/80945>
In-reply-to: 80945 </cgi-bin/scat.rb/ruby/ruby-talk/80945>

Is it possible to make compound if statements in Ruby, something like:

if field[0] == “AIX” or if field[2] =~ /ldap/
puts “BLAH”
end

(but different, of course, since the above doesn’t work.)

Don’t repeat the if. It’s like most languages.

if field[0] == “AIX” or field[2] =~ /ldap/
puts “BLAH”
end

You can also use || as in C. There are some differences, but
in this case they’d work identically.

Hal

···

Date: Wed, 3 Sep 2003 13:05:56 +0900

Kurt Euler wrote:

Thanks Hal. Question: What do you mean by use ||? Could you
give an example? (I’m new to Ruby and programming.)

is essentially the same as or
&& is essentially the same as and

So:
if this == that || this =~ /another/
is the same as
if this == that or this =~ /another/

History lesson: ‘and’ and ‘or’ and ‘not’ are used in
languages like BASIC, Algol, and Pascal. && and || and !
are used in languages like C, C++, and Java. Ruby happens
to know both forms.

And by the way, don’t confuse the &&/|| operators with
the &/| operators. The latter are “bitwise” – they act
on individual bits in a number. For example. 5 | 6 is 7,
since you’re doing a bitwise OR of binary 101 and 110,
giving binary 111.

Whirlwind tour of truth and falsehood in Ruby:
Two rules -

  1. false and nil test as false
  2. everything else tests as true
    Example:
    if this # prints “no” if this is nil or false;
    puts “yes” # prints “yes” if it’s 0 or “” or
    else # or 237 or “foobar” or [1,2,3] or
    puts “no” # any other value imaginable.
    end

Also remember that these expressions are “short-circuited” –
as soon as we know the result, we stop executing. Especially
remember this if you’re calling a method with side effects
(in other words – don’t).
Example:
x = true || foo() # foo doesn’t get called – we already
# know the result is true
x = true && foo() # foo gets called – it’s necessary in
# order to get the correct answer
x = false || foo() # foo is called
x = false && foo() # foo is not called

This makes an interesting idiom possible. Suppose we
want to assign 5 to x, but ONLY if it doesn’t already
have a value.

An unassigned variable effectively has a value of nil.
So we can say
x = x || 5
to do this. Or using the shorthand that Ruby borrowed from
C, we can say:
x ||= 5

It’s worth mentioning that this is a little strange on
second thought, but makes sense on third thought.

Variables in Ruby aren’t declared. Ruby knows something is
a variable when it sees an assignment to it.

Now in the statement x = x || 5 you would take it for
granted that the right hand side is evaluated first so that
we can assign to the variable on the left hand side. But
how can x || 5 make sense when x is undefined??? How does it
even know x is a variable?

The answer is that the interpreter “sees” the assignment to
x at parse time and realizes that x is a variable. But since
it has never been assigned, it will have the value nil. So
the right hand side evaluates to nil || 5 (which is just 5).
So then x gets its first “real” value.

Confused yet??

Ask all the questions you want, we’ll take turns on them.

Cheers,
Hal

Also ‘and’ and ‘or’ have “extremely low precedence” (according to Ruby in a
Nutshell) so constructs using them may behave differently than those
using && and || and my need parentheses to clear up ambiguities. I don’t
have any good examples, but I’m sure someone does.

Joey

···

On Wed, 3 Sep 2003 13:54:45 +0900, Hal Fulton hal9000@hypermetrics.com wrote:

History lesson: ‘and’ and ‘or’ and ‘not’ are used in
languages like BASIC, Algol, and Pascal. && and || and !
are used in languages like C, C++, and Java. Ruby happens
to know both forms.


Dean saor, dean saor an spiorad. Is seinn d’orain beo.

Hi,

History lesson: ‘and’ and ‘or’ and ‘not’ are used in
languages like BASIC, Algol, and Pascal. && and || and !
are used in languages like C, C++, and Java. Ruby happens
to know both forms.

Also ‘and’ and ‘or’ have “extremely low precedence” (according to Ruby in a
Nutshell) so constructs using them may behave differently than those
using && and || and my need parentheses to clear up ambiguities. I don’t
have any good examples, but I’m sure someone does.

foo = false || bar = true # foo = true, bar = true
foo = false or bar = true # foo = false, bar = true

The former is equivalent to

foo = (false or bar = true)

and the latter is

(foo = false) || (bar = true)

Also note that ‘and’ and ‘or’ have same precedence.

(foo or bar and bas) means ((foo || bar) && bas)
but
(foo || bar && bas) is (foo || (bar && bas))

···

At Wed, 3 Sep 2003 21:06:54 +0900, Joey Gibson wrote:


Nobu Nakada

Also ‘and’ and ‘or’ have “extremely low precedence” (according to Ruby in a
Nutshell) so constructs using them may behave differently than those
using && and || and my need parentheses to clear up ambiguities. I don’t
have any good examples, but I’m sure someone does.

foo = false || bar = true # foo = true, bar = true
foo = false or bar = true # foo = false, bar = true

The former is equivalent to

foo = (false or bar = true)

and the latter is

(foo = false) || (bar = true)

In case a newbie gets confused: He does mean = here, not ==.
An assignment returns the value assigned.

Also note that ‘and’ and ‘or’ have same precedence.

(foo or bar and bas) means ((foo || bar) && bas)
but
(foo || bar && bas) is (foo || (bar && bas))

This confuses me. So && is higher precedence than ||
but ‘and’ is not higher than ‘or’.

In short:
true or true and false # false
true || true && false # true

For more reasons than readability, I think these
should certainly not be mixed:
if a and b || c or d && e || f and g…

:slight_smile:

Hal

···

nobu.nokada@softhome.net wrote:

Hi,

In case a newbie gets confused: He does mean = here, not ==.
An assignment returns the value assigned.

Maybe.

Also note that ‘and’ and ‘or’ have same precedence.

(foo or bar and bas) means ((foo || bar) && bas)
but
(foo || bar && bas) is (foo || (bar && bas))

This confuses me. So && is higher precedence than ||
but ‘and’ is not higher than ‘or’.

In short:
true or true and false # false
true || true && false # true

For more reasons than readability, I think these
should certainly not be mixed:
if a and b || c or d && e || f and g…

:slight_smile:

Agreed. One possibility is that it is same as Bourne-shell’s
&& and ||. Perhaps it comes via Perl, but I don’t know the
certain reason.

···

At Thu, 4 Sep 2003 00:12:46 +0900, Hal Fulton wrote:


Nobu Nakada

Well, && and || are traceable back to C, no matter what route they took to
Ruby, and their Ruby behavior, including their precedence, matches their
behavior in C.

The spelled-out ‘and’ and ‘or’ have appeared in many languages, but I
suspect their precise behavior with regard to precedence (of each other
and of the punctuational versions) was borrowed from Perl.

-Mark

···

On Thu, Sep 04, 2003 at 12:27:05AM +0900, nobu.nokada@softhome.net wrote:

One possibility is that it is same as Bourne-shell’s
&& and ||. Perhaps it comes via Perl, but I don’t know the
certain reason.

I don’t know if it is this way in Perl too, but I believe matz said once
that ‘and’ and ‘or’ had the same precedence in Ruby because they have
the same precedence in English.

···

On Thu, Sep 04, 2003 at 12:42:48AM +0900, Mark J. Reed wrote:

The spelled-out ‘and’ and ‘or’ have appeared in many languages, but I
suspect their precise behavior with regard to precedence (of each other
and of the punctuational versions) was borrowed from Perl.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Checking host system type…
i586-unknown-linux
configure: error: sorry, this is the gnu os, not linux
– Topic on #Linux