Possible to make compound if statements

Hal: Thanks for your long response. While reading it I was thinking to myself that this guy should write a book on Ruby. Then I realized your name was familiar and found you on Amazon.

Say, do you think you’ll be updating Ruby Way with info on the later version?!

Thanks.

-Kurt

References: *
In-reply-to: *
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

···

Date: Wed, 3 Sep 2003 13:54:45 +0900

Kurt Euler wrote:

Hal: Thanks for your long response. While reading it I was thinking
to myself that this guy should write a book on Ruby. Then I realized
your name was familiar and found you on Amazon.

Say, do you think you’ll be updating Ruby Way with info on the later version?!

I’d like to think so, but the first edition has to sell
sufficiently first.

As Dave Thomas said before: Hint, hint… :slight_smile:

Hal

Amazon.com says there are only 5 left in stock. Is that a good thing?

I wish I could find your book here in .br … :frowning:

Andre

···

On Wed, 2003-09-03 at 11:27, Hal Fulton wrote:

I’d like to think so, but the first edition has to sell
sufficiently first.

Andre Nathan wrote:

I’d like to think so, but the first edition has to sell
sufficiently first.

Amazon.com says there are only 5 left in stock. Is that a good thing?

Hmm, I’m not sure what Amazon means by “in stock” – I
thought they ordered from the publisher on demand.

I wish I could find your book here in .br … :frowning:

I wish you could, too. :slight_smile: Pardon my ignorance of geography
and and top-level domains… .br is Bra[sz]il?

Hal

···

On Wed, 2003-09-03 at 11:27, Hal Fulton wrote:

Yes. The only store that has the book on their catalog says it can take
up to 10 weeks (!) for it to be delivered… They will import it on
demand, I believe.

Andre

···

On Wed, 2003-09-03 at 12:23, Hal Fulton wrote:

I wish you could, too. :slight_smile: Pardon my ignorance of geography
and and top-level domains… .br is Bra[sz]il?

Andre Nathan wrote:

···

On Wed, 2003-09-03 at 12:23, Hal Fulton wrote:

I wish you could, too. :slight_smile: Pardon my ignorance of geography
and and top-level domains… .br is Bra[sz]il?

Yes. The only store that has the book on their catalog says it can take
up to 10 weeks (!) for it to be delivered… They will import it on
demand, I believe.

I think it is available electronically from safari.net
(or is that safari.oreilly.com?). So I hear, I have
never used that service.

Hal

It’s safari.oreilly.com.

I’ve been an enthusiastic supporter of that service for several
months, until I heard that they don’t pay royalty for the books you
check out. :frowning:

···

On Thu, Sep 04, 2003 at 12:50:28AM +0900, Hal Fulton wrote:

I think it is available electronically from safari.net
(or is that safari.oreilly.com?). So I hear, I have
never used that service.