Beginner: if statement in one line?

C

Chris Chris

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
 
T

trader9

[Note: parts of this message were removed to make it a legal post.]

if 5== 5 then puts '5' end
 
C

Carl Harroch

puts '5' if 5==5

you could also write something of the form:

puts '5' unless 5!=5

/Carl
 
S

Sandro Paganotti

[Note: parts of this message were removed to make it a legal post.]

for if / else oneliners you can use also

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

es: puts ( 5 == 5 ? "equal!" : "not equal!" )
 
M

matt neuburg

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.
 
T

Trys

Chris said:
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.

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
 
D

Dave Bass

Chris said:
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
 
N

Nobuyoshi Nakada

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'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top