# 'and' keyword?

**URL:** <https://www.ruby-forum.com/t/and-keyword/154819>\
**Category:** Ruby\
**Created:** [December 4, 2008, 6:45am UTC](https://www.ruby-forum.com/t/and-keyword/154819 "2008-12-04T06:45:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 4, 2008, 6:45am UTC](https://www.ruby-forum.com/t/and-keyword/154819/1 "2008-12-04T06:45:10Z")

</div>

Because puts(“hi”) yields nil, the second item never gets called:  
puts “hi” and puts “bye” if true

I can work around it with these:  
!puts “hi” and puts “bye” if true  
puts “hi”;puts “bye” if true  
[puts(“hi”), puts(“bye”)] if true

–But was hoping there was a more aesthetic one liner out there that can  
use  
‘and’…

Thanks in advance!!

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 4, 2008, 7:12am UTC](https://www.ruby-forum.com/t/and-keyword/154819/2 "2008-12-04T07:12:08Z")

</div>

On 04.12.2008, at 05:38 , list. rb wrote:

> ‘and’…
> 
> Thanks in advance!!

How about

puts “hi”, “bye” if true

einarmagnus

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 4, 2008, 7:47am UTC](https://www.ruby-forum.com/t/and-keyword/154819/3 "2008-12-04T07:47:53Z")

</div>

From: list. rb [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Because puts(“hi”) yields nil, the second item never

# gets called:

you can create your own puts/print that returns true or whatever

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 4, 2008, 9:04am UTC](https://www.ruby-forum.com/t/and-keyword/154819/4 "2008-12-04T09:04:43Z")

</div>

List Rb wrote:

> Because puts(“hi”) yields nil, the second item never gets called:  
> puts “hi” and puts “bye” if true
> 
> I can work around it with these:  
> !puts “hi” and puts “bye” if true  
> puts “hi”;puts “bye” if true  
> [puts(“hi”), puts(“bye”)] if true
> 
> –But was hoping there was a more aesthetic one liner out there that can  
> use  
> ‘and’…
> 
> Thanks in advance!!

In my opinion, using logical operators with side-effects is bad style.  
I’d try to avoid it.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 4, 2008, 9:57am UTC](https://www.ruby-forum.com/t/and-keyword/154819/5 "2008-12-04T09:57:40Z")

</div>

List Rb wrote:

> Because puts(“hi”) yields nil, the second item never gets called:  
> puts “hi” and puts “bye” if true

I guess what you’re trying to compress is this:

if true  
puts “hi”  
puts “bye”  
end

If you really want a one-liner, then your best option is probably

(puts “hi”; puts “bye”) if true

(The parentheses ARE needed here. Otherwise puts “hi” is run always)

If these are actually both calls to puts then you only need a single  
one:

puts “hi”,“bye” if true

Everything else gets yucky. For example, you can rely on the nil return  
value of puts:

puts “hi” or puts “bye” if true

puts(“hi”) || puts(“bye”) if true

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 4, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/and-keyword/154819/6 "2008-12-04T23:01:31Z")

</div>

On Dec 4, 2008, at 3:51 PM, “Robert D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> What about?
> 
> puts %w{ hi bye }
> 
> ## R.
> 
> Ne baisse jamais la tÃªte, tu ne verrais plus les Ã©toiles.
> 
> Robert D. 😉

In English, I woud say…

jump and shout unless someone is sleeping.

Ruby is so elegant that I fidng myself wanting to write similarly,.

jump and shout unless someone.sleeping?

☹

Just wish it’s was interpreted as such

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 4, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/and-keyword/154819/7 "2008-12-04T21:58:29Z")

</div>

What about?

puts %w{ hi bye }

## R.

Ne baisse jamais la tête, tu ne verrais plus les étoiles.

Robert D. 😉

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 4, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/and-keyword/154819/8 "2008-12-04T23:36:09Z")

</div>

Hi –

On Fri, 5 Dec 2008, List.rb wrote:

> > Robert D. 😉
> 
> In English, I woud say…
> 
> jump and shout unless someone is sleeping.
> 
> Ruby is so elegant that I fidng myself wanting to write similarly,.
> 
> jump and shout unless someone.sleeping?

Ruby isn’t English, though.

> ☹
> 
> Just wish it’s was interpreted as such

It is; it’s just that puts returns nil, so it doesn’t pass through the  
and gate. I don’t think the word “and” just to mean “and then execute  
this” would be very useful.

David

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 5, 2008, 9:50am UTC](https://www.ruby-forum.com/t/and-keyword/154819/9 "2008-12-05T09:50:53Z")

</div>

On Thu, Dec 4, 2008 at 11:28 PM, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:  
I do not think highly of the following code in general, but if this is  
about some literal programming idea it might come in handy. Children  
just do not use it when home alone 😉

irb(main):010:0\> class Object  
irb(main):011:1\> def and &blk  
irb(main):012:2\> blk.call  
irb(main):013:2\> end  
irb(main):014:1\> end  
=\> nil  
irb(main):015:0\> puts( 42 ).and { puts 42 }.and { puts 42 }  
42  
42  
42

Cheers  
R.
