Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > 'and' keyword?

Reply
Thread Tools

'and' keyword?

 
 
list. rb
Guest
Posts: n/a
 
      12-04-2008
[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!!

 
Reply With Quote
 
 
 
 
Einar Magnús Boson
Guest
Posts: n/a
 
      12-04-2008

On 04.12.2008, at 05:38 , 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!!



How about

puts "hi", "bye" if true

einarmagnus




 
Reply With Quote
 
 
 
 
Peña, Botp
Guest
Posts: n/a
 
      12-04-2008
From: list. rb [private.php?do=newpm&u=]=20
# Because puts("hi") yields nil, the second item never
# gets called:

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

 
Reply With Quote
 
Stefan Rusterholz
Guest
Posts: n/a
 
      12-04-2008
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      12-04-2008
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      12-04-2008
What about?

puts %w{ hi bye }

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

Robert Dober

 
Reply With Quote
 
List.rb
Guest
Posts: n/a
 
      12-04-2008
On Dec 4, 2008, at 3:51 PM, "Robert Dober" <> =20
wrote:

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

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      12-05-2008
On Thu, Dec 4, 2008 at 11:28 PM, David A. Black <> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57