Ruby - if statement?
January 15, 2024
In Ruby, the if
statement looks like this
val = 1if val == 1p "Equality Checked!"end
And for if else
val = 2if val == 1p "Equality Checked!"elsep "Equality Unchecked!"end
And for if, else if, else
val = 2if val == 1p "Equality Checked!"elsif val == 2p "Equality Middle Checked!"elsep "Equality Unchecked!"end
Also, remember that everything in ruby returns a value, so your if statement can return a value that could be stored in another variable.
val = 2# store the returned value from the if statementret_val = if val == 1p "Equality Checked!"elsep "Equality Unchecked!"endp ret_val # => "Equality Unchecked!"
Dhanyavaad! 🙇
Edit on githubTweet