#!/usr/bin/ruby

func hi (i) {

    func user (user) {
        return ("Username is '%s'".sprintf(user));      # child function return
        "Child function error! in #{__FILE__} at line #{__LINE__}\n" -> die;
    }

    user('sidef');

    if (i == 3) {

        if (i == 1+2) {
            return "IF(3) RETURN";
        }

        die "Error! (1)"
    }
    elsif (i == 3) {
        die "ERROR: re-true elsif!"
    }
    elsif (i == 1) {
        return "ELSIF(1) RETURN";
        die "Error! (2)"
    }

    i == 3 && (
        return "&& RETURN";
        die "Error! (3)"
    );

    if (i == 2) {
        return "IF(2) RETURN";
        die "Error! (4)"
    }

    return "FUNC RETURN";
    "ERROR RETURN";
}

hi(1) == "ELSIF(1) RETURN" || die "Error hi(1)!"
hi(2) == "IF(2) RETURN"    || die "Error hi(2)!"
hi(3) == "IF(3) RETURN"    || die "Error hi(3)!"
hi(4) == "FUNC RETURN"     || die "Error hi(4)!"

#
## Return from 'for' loop
#

func for_loop {

    for (["a", "z", "f"]) { |c|

        if (c == "z") {
            return "LOOP RETURN";
        }

        if (c == "f") {
            die "LOOP ERROR!";
        }
    }

    die "Error hello(name)!";
}

for_loop() == "LOOP RETURN" || die "Error hello(Sidef)!";

say "** Test passed!";
