Forum: Ruby Check for boolean status in ruby

A2a007b60856e56aa497faa11f8116d6?d=identicon&s=25 ideal one (shekarls)
on 2011-10-10 06:23
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.
B31e7abd14f1ceb4c4957da08933c630?d=identicon&s=25 Josh Cheek (josh-cheek)
on 2011-10-10 06:52
(Received via mailing list)
On Sun, Oct 9, 2011 at 11:23 PM, ideal one <idealone5@hotmail.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
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
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?
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.