Paul Jonathan Thompson
2006-Jul-03 23:47 UTC
[Rails] 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
James Ludlow
2006-Jul-03 23:55 UTC
[Rails] How do I code this conditional statement in Ruby
On 7/3/06, Paul Jonathan Thompson <rails001@gmail.com> 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.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.
Jason Roelofs
2006-Jul-04 00:07 UTC
[Rails] How do I code this conditional statement in Ruby
James Ludlow wrote:> On 7/3/06, Paul Jonathan Thompson <rails001@gmail.com> 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. > > 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. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > 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
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.
Chris Carter
2006-Jul-04 04:08 UTC
[Rails] Re: How do I code this conditional statement in Ruby
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 Thompson 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-- Posted via http://www.ruby-forum.com/.
Paul Jonathan Thompson
2006-Jul-04 23:47 UTC
[Rails] Re: How do I code this conditional statement in Ruby
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 On 04/07/06, Chris Carter <cdcarter@gmail.com> wrote:> 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 Thompson 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 > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >