'and' keyword?

L

list. rb

[Note: parts of this message were removed to make it a legal post.]

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!!
 
E

Einar Magnús Boson

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


How about

puts "hi", "bye" if true

einarmagnus
 
P

Peña, Botp

From: list. rb [mailto:[email protected]]=20
# Because puts("hi") yields nil, the second item never
# gets called:

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

Stefan Rusterholz

List said:
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
 
B

Brian Candler

List said:
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
 
R

Robert Dober

What about?

puts %w{ hi bye }

R.
--=20
Ne baisse jamais la t=EAte, tu ne verrais plus les =E9toiles.

Robert Dober ;)
 
L

List.rb

What about?

puts %w{ hi bye }

R.
--=20
Ne baisse jamais la t=C3=AAte, tu ne verrais plus les =C3=A9toiles.

Robert Dober ;)


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=20=
 
R

Robert Dober

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top