Forum: Ruby Check for boolean status in ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
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?
This topic is locked and can not be replied to.