Hi
How can i compress
p a, (b == 5 ? 4 : 3 ), c
if i dont need the last part : 3 ?
Thanks
Berg
You can write (4 if b == 5).
If the condition evaluates to false, the expression under parenthesis will
evaluate to nil. If this is a call to puts, it will print a blank line. If
that is not OK you can do:
p *[a, (4 if b == 5), c].compact
So, this builds an array with the arguments you want to pass to p. The
compact call removes eventual Nils. The * operator (splash op) expands the
array elements as actual arguments of the method call. Pretty ugly, though,
and more verbose than your original version.
Samuel B
p a
p b if b==5
p c
···
On Sat, Feb 13, 2016 at 9:41 PM, A Berger <aberger7890@gmail.com> wrote:
p a, (b == 5 ? 4 : 3 ), c
Thanks
( @botp: qu not "who do I expand"!
···
Am 13.02.2016 15:08 schrieb "Samuel Brandão" <gb.samuel@gmail.com>:
You can write (4 if b == 5).
If the condition evaluates to false, the expression under parenthesis will
evaluate to nil. If this is a call to puts, it will print a blank line. If
that is not OK you can do:p *[a, (4 if b == 5), c].compact
So, this builds an array with the arguments you want to pass to p. The
compact call removes eventual Nils. The * operator (splash op) expands the
array elements as actual arguments of the method call. Pretty ugly, though,
and more verbose than your original version.Samuel B
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
To skip the `compact` call, use:
p a, *(4 if b == 5), c
the unary `*` operator turns non-array values (`4`) into one argument,
`nil` into zero arguments.
···
On Sat, 2016-02-13 at 16:05 +0100, A Berger wrote:
> p *[a, (4 if b == 5), c].compact