Forum: Ruby 'and' keyword?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
List R. (Guest)
on 2008-12-04 07:45
(Received via mailing list)
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!!
Einar Magnús Boson (Guest)
on 2008-12-04 08:12
(Received via mailing list)
On 04.12.2008, at 05:38 , list. rb wrote:

> 'and'..
>
> Thanks in advance!!


How about

puts "hi", "bye" if true

einarmagnus
Peña, Botp (Guest)
on 2008-12-04 08:47
(Received via mailing list)
From: list. rb [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
Stefan R. (Guest)
on 2008-12-04 10:04
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
Brian C. (Guest)
on 2008-12-04 10:57
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
Robert D. (Guest)
on 2008-12-04 22:58
(Received via mailing list)
What about?

puts %w{ hi bye }

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

Robert D. ;)
List R. (Guest)
on 2008-12-05 00:01
(Received via mailing list)
On Dec 4, 2008, at 3:51 PM, "Robert D." <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
David A. Black (Guest)
on 2008-12-05 00:36
(Received via mailing list)
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
Robert D. (Guest)
on 2008-12-05 10:50
(Received via mailing list)
On Thu, Dec 4, 2008 at 11:28 PM, David A. Black 
<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.
This topic is locked and can not be replied to.