Beginner: if statement in one line?


#1

Hi,

a simple question, however I did not find an answer for this:

How can I put a statement like

if 5 == 5
puts ‘5’
end

in one line?

Just writing them all statements together in one line didn’t work,
and a syntax like if 5 == 5 {puts ‘5’} end didn’t either.

Thanks for your help.

CHeers, CHris


#2

if 5== 5 then puts ‘5’ end


#3

puts ‘5’ if 5==5

you could also write something of the form:

puts ‘5’ unless 5!=5

./Carl

Chris C. wrote:

Hi,

a simple question, however I did not find an answer for this:

How can I put a statement like

if 5 == 5
puts ‘5’
end

in one line?

Just writing them all statements together in one line didn’t work,
and a syntax like if 5 == 5 {puts ‘5’} end didn’t either.

Thanks for your help.

CHeers, CHris


#4

Chris C. schrieb:

How can I put a statement like

if 5 == 5
puts ‘5’
end

in one line?

puts ‘5’ if 5 == 5

:wink:

Greetings


#5

for if / else oneliners you can use also

( [condition] ? [true] : [false] )

es: puts ( 5 == 5 ? “equal!” : “not equal!” )


#6

Chris C. wrote:

in one line?
puts ‘5’ if 5 == 5

Just writing them all statements together in one line didn’t work,
and a syntax like if 5 == 5 {puts ‘5’} end didn’t either.

Thanks for your help.

CHeers, CHris


M. Edward (Ed) Borasky
http://ruby-perspectives.blogspot.com/

“A mathematician is a machine for turning coffee into theorems.” –
Alfréd Rényi via Paul Erdős


#7

Chris C. wrote:

in one line?

Just writing them all statements together in one line didn’t work,
and a syntax like if 5 == 5 {puts ‘5’} end didn’t either.

AFAIK, you can’t use {} brackets to mark block for “if” statement, so
anything with them won’t work.

“If” uses “then” to mark beginning of its block and ends it with usual
“end”.

if 5 == 5 then
puts ‘5’
end

But you can omit “then” if you have multiple lines:

if 5 == 5
puts ‘5’
end

but for one-liners it is a must. So what you need is either:

if 5 == 5 then puts ‘5’ end

or even shorter switching “then” for “:”

if 5 == 5 : puts ‘5’ end


#8

Chris C. removed_email_address@domain.invalid wrote:

a simple question, however I did not find an answer for this:

How can I put a statement like

if 5 == 5
puts ‘5’
end

in one line?

Sure, and make it a little more readable too:

puts ‘5’ if 5 == 5 # The condition can be at the end of the line. :slight_smile:


#9

In addition to others answers you’ve received, keep in mind that you can
always disambigue when multiple lines go on a single line through the
use of semicolons:

if 5 == 5; puts 5; end

It happens that in this simple case, that isn’t necessary, since Ruby
lets you disambiguate with “then”:

if 5 == 5 then puts 5 end

Or, as has been pointed out, you can use the end-of-line “if” operator:

puts 5 if 5 == 5

m.


#10

Hi,

At Sat, 12 Jul 2008 01:30:57 +0900,
Trys wrote in [ruby-talk:307868]:

or even shorter switching “then” for “:”

if 5 == 5 : puts ‘5’ end

That usage of “:” had never been an official feature, and no
longer possible. Do not use it.

And another one:

5 == 5 and puts ‘5’


#11

Chris C. wrote:

and a syntax like if 5 == 5 {puts ‘5’} end didn’t either.

Be aware that { } blocks in Ruby don’t have the same meaning as { }
blocks in C-like languages. They’re not used for grouping statements;
rather, they’re used as code blocks (cf. anonymous subroutines, closures
etc).

Also, { } is an empty hash, which can sometimes be confusing.

x = lambda { } # an empty code block
x = { } # an empty hash

Dave


#13

Like with shellscript, you can use && for ‘then’ and || for ‘else’ :

v=5
( v==5 )  && ( puts "5 is the value" ; puts "that's it." )
( v==6 )  ||    ( puts "6 is note the value" ; puts "that's it." )