Grouping statements with postfix conditionals

Hi,

In perl I can do this:

print ‘1’, print ‘2’ if condition;

In other words, both of the print statements will be executed only if
the condition is true.

What’s the equivalent construct in Ruby?

Thanks!

I don’t know if there is an equivalent construct in ruby. How’s this
work in perl? Somehow you have an array of functions, which is being
executed depending on a condition. It’s very tricky looking code and
there doesn’t seem to be much of a benefit over just using a regular if
statement with a block?

if true
print “1”
print “2”
end

There are ways to get around this, you could create a proc:

msg = Proc.new do
print “1”
print “2”
end

msg.call if true #=> 12
msg.call if false #=> nil

Do this all on one line:
Proc.new { print “1”; print “2” }.call if potato.tasty?

Replace the proc with a lambda:
-> { do_stuff; more_stuff; }.call if work.stuff?

But this isn’t really what you mean, because you want to execute
straight away. Semantically it’s something like:

[ eval(“print ‘1’”, eval(“print ‘2’”) ].each { |msg| msg } if true

The other thing you could look at is sending your receiving object
messages, but it all seems to not quite be what you want.

Sergei Gerasenko wrote in post #1183816:

Hi,

In perl I can do this:

print ‘1’, print ‘2’ if condition;

you can group instructions with parentheses :

(print ‘1’ ; print ‘2’) if condition

Regis d’Aubarede wrote in post #1183830:

you can group instructions with parentheses :

(print ‘1’ ; print ‘2’) if condition

Hi Regis,

Thanks! I didn’t know that was possible. How does this work
syntactically?

Joe

Joe Gain wrote in post #1183818:

I don’t know if there is an equivalent construct in ruby. How’s this
work in perl? Somehow you have an array of functions, which is being
executed depending on a condition. It’s very tricky looking code and
there doesn’t seem to be much of a benefit over just using a regular if
statement with a block?

Hi Joe,

In Perl, the comma just groups statements in situations like that. So,
it’s not an array, but just a series of statements that are to be
executed one after another if the condition is true.

Still your solutions are interesting. Thank you for taking the time.

Sergei

Regis d’Aubarede wrote in post #1183830:

Sergei Gerasenko wrote in post #1183816:

Hi,

In perl I can do this:

print ‘1’, print ‘2’ if condition;

you can group instructions with parentheses :

(print ‘1’ ; print ‘2’) if condition

Thanks for the solution, regis.

I tried it and it works with the semicolon replaced with a comma. So
like so:

(print ‘1’, print ‘2’) if condition

If it’s a semicolon, then only the 1st print is invoked because
apparently in that case Ruby sees it like this:

print 1; print 2 if condition.

But anyway, thank you so much for the great tip!

Regis d’Aubarede wrote in post #1183854:

Sergei Gerasenko wrote in post #1183838:

Thanks! I didn’t know that was possible.

See ‘101 things you didn-t know ruby could do’ :

http://confreaks.tv/videos/aloharuby2012-ten-things-you-didn-t-know-ruby-could-do

Sergei Gerasenko wrote in post #1183838:

(print ‘1’ ; print ‘2’) if condition

Thanks for the solution, regis.

I tried it and it works with the semicolon replaced with a comma. So
like so:

(print ‘1’, print ‘2’) if condition

If it’s a semicolon, then only the 1st print is invoked because
apparently in that case Ruby sees it like this:

irb

RUBY_VERSION
=> “2.2.4”

(p 1; p 2) if true
1
2
=> 2
(p 1; p 2) if false
=> nil

a=0; ( a+=1 ; a+= 1 ; a+=1 ; a+= 1) if true ; p a
4
=> 4
a=0; ( a+=1 ; a+= 1 ; a+=1 ; a+= 1) if false ; p a
0
=> 0

Thanks! I didn’t know that was possible. How does this work
syntactically?

From ruby BNF Syntaxe:
http://ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/yacc.html

COMPSTMT : STMT (TERM EXPR)* [TERM]

TERM : ;' | \n’

STMT : CALL do [|' [BLOCK_VAR] |'] COMPSTMT end
| undef FNAME
| alias FNAME FNAME
| STMT if EXPR

| EXPR

EXPR : MLHS `=’ MRHS
| return CALL_ARGS
| yield CALL_ARGS
| EXPR and EXPR

| ARG

ARG : LHS =' ARG | LHS OP_ASGN ARG | ARG …’ ARG

| ARG `||’ ARG
| defined? ARG
| PRIMARY

PRIMARY : (' COMPSTMT )’ !!HERE: primary => ( compstmt )

| LITERAL

So
compstmt = expr ; … ==> expr ==> arg ==> primary==> ‘(’ comstmt ‘)’

You don’t find this syntax described in any ruby book :slight_smile:
but it is valid !

Regis d’Aubarede wrote in post #1183856:

See ‘101 things you didn-t know ruby could do’ :

http://confreaks.tv/videos/aloharuby2012-ten-things-you-didn-t-know-ruby-could-do

:o

Cool link.