[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Beginner: if statement in one line?

Chris Chris

7/11/2008 10:56:00 AM

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
--
Posted via http://www.ruby-....

9 Answers

trader9

7/11/2008 11:02:00 AM

0

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

if 5== 5 then puts '5' end


On Fri, Jul 11, 2008 at 4:25 PM, Chris Chris <kylejc@gmx.net> 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
> --
> Posted via http://www.ruby-....
>
>

Carl Harroch

7/11/2008 11:05:00 AM

0

puts '5' if 5==5

you could also write something of the form:

puts '5' unless 5!=5

/Carl

Chris Chris 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

--
Posted via http://www.ruby-....

akl

7/11/2008 11:10:00 AM

0

Chris Chris schrieb:

> How can I put a statement like
>
> if 5 == 5
> puts '5'
> end
>
> in one line?

puts '5' if 5 == 5

;)

Greetings

Sandro Paganotti

7/11/2008 12:13:00 PM

0

[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!" )




On Fri, Jul 11, 2008 at 11:11 AM, akl <twinandi@web.de> wrote:

> Chris Chris schrieb:
>
> How can I put a statement like
>>
>> if 5 == 5
>> puts '5'
>> end
>>
>> in one line?
>>
>
> puts '5' if 5 == 5
>
> ;)
>
> Greetings
>
>


--
Emo Philips - "I got some new underwear the other day. Well, new to me."

matt

7/11/2008 3:02:00 PM

0

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.

Chris Chris <kylejc@gmx.net> 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


--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Trys

7/11/2008 4:25:00 PM

0

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

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

Darryl L. Pierce

7/11/2008 5:54:00 PM

0

Chris Chris <kylejc@gmx.net> 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. :)


--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.mul...
"What do you care what people think, Mr. Feynman?"
** Posted from http://www.te... **

Dave Bass

7/12/2008 12:10:00 PM

0

Chris Chris 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
--
Posted via http://www.ruby-....

Nobuyoshi Nakada

7/13/2008 12:34:00 AM

0

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'

--
Nobu Nakada