Hi all, I have to check for a boolean condition, and see whether its true or false to continue running a method. Not sure how to implement this in Ruby... Dummycode-- boolean islogged_in = false; # set default boolean value false <CODE> for i in 0..2 RUN FUNCTION A boolean islogged_in = true # Setting boolean to True after successful FUNCTION A RUN FUNCTION B end <CODE> For First Iteration, FUNCTION A should run, but for next Iteration onwards, only if System is not logged in FUNCTION A should run else directly FUNCTION B should Run. How do i set a check in Ruby or should i use some other logic other than boolean to do this.
on 2011-10-10 06:23
on 2011-10-10 06:52
On Sun, Oct 9, 2011 at 11:23 PM, ideal one <email@example.com> wrote: > for i in 0..2 > > For First Iteration, FUNCTION A should run, but for next Iteration > onwards, only if System is not logged in FUNCTION A should run else > directly FUNCTION B should Run. > How do i set a check in Ruby or should i use some other logic other than > boolean to do this. > > -- > Posted via http://www.ruby-forum.com/. > > This is pretty straightforward procedural code, imo. Perhaps I'm misunderstanding what you're wanting? def function_a puts 'a was run' end def function_b puts 'b was run' end logged_in = false # using 3 because 0..2 iterates over [0, 1, 2] # but we don't actually care about the values 3.times do function_a unless logged_in logged_in = true function_b end # >> a was run # >> b was run # >> b was run # >> b was run
on 2011-10-10 09:17
if islogged_in run_function_a else run_function_b end And of course inside the function you must set the islogged_in flag to true or false, depending on your conditions. This is the same in almost every programming language, are you absolutely new to programming?