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-forum.com/ .
trader9
(trader9)
11 July 2008 11:02
2
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-forum.com/\ .
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-forum.com/\ .
Matt9
11 July 2008 15:00
6
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, Matt Neuburg’s Home Page
Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
Trys
(Trys)
11 July 2008 16:30
7
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
Sure, and make it a little more readable too:
puts '5' if 5 == 5 # The condition can be at the end of the line.
···
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?
--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
** Posted from http://www.teranews.com **
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-forum.com/\ .
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."
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