How do I code this conditional statement in Ruby

Hi, I am a COBOL programmer and I am busy teaching myself Rails and
Ruby.

In COBOL I can code this conditional

If x = 1
next sentence
else

The “next sentence” statement enables me to get out of the
conditional. How would I code the same thing in Ruby? In C you could
use break but I understand that Ruby has no break statement.

Regards,

Paul

On 7/3/06, Paul Jonathan T. [email protected] wrote:

conditional. How would I code the same thing in Ruby? In C you could
use break but I understand that Ruby has no break statement.

Can you give a better example of what you’re really trying to do? Why
would you ever want to break out of a conditional? Just don’t put
code in there that you don’t want executed.

James L. wrote:

http://lists.rubyonrails.org/mailman/listinfo/rails

Ruby does too have ‘break’. I’m assuming your in a loop here, as you
mentioned doing the ‘same thing’ in C. It works just as well in Ruby:

loop.construct do
if x == 1
break
end
end

will kick you out of the loop. If you really mean just jumping out of
the conditional, you should then rewrite the conditional to be the
opposite, aka:

if x != 1
do the other stuff
end

Jason

Hello, It looks like what you want is something that executes unless
x==1. You could do if x != 1 or the more popular (and more positive)
unless x==1.

Chris
Paul Jonathan T. wrote:

Hi, I am a COBOL programmer and I am busy teaching myself Rails and
Ruby.

In COBOL I can code this conditional

If x = 1
next sentence
else

The “next sentence” statement enables me to get out of the
conditional. How would I code the same thing in Ruby? In C you could
use break but I understand that Ruby has no break statement.

Regards,

Paul

If I remember my COBOL right, can’t you execute a number of instructions
up
to a period and call that a sentence? You’ll find most languages
designed
after the '70s (exception: Basic) had more clearly delineated blocks
associated with conditionals instead of the concept of sentences.

The paradigm is:

if a
do something
else
do something else
elseif x== 1
do something with x
end

I think the elseif may be what you are looking for. Consider using the
case
statement instead:

case x
when 1 then puts “a partridge in a pear tree”
when 2 then puts “two turteldoves…”
else puts “that’s all, folks”
end

These constructs take you away from the “goto” flow of control that
earlier
procedural languages relied so heavily on.

View this message in context:
http://www.nabble.com/How-do-I-code-this-conditional-statement-in-Ruby-tf1887308.html#a5160746
Sent from the RubyOnRails Users forum at Nabble.com.

Hi,

Thank you to all who replied, problem sorted out now. When you are
used to a procedural language, OOP takes a bit of getting used too and
a fundamentally different approach.

Regards,

Paul

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs