Home
About
moon indicating dark mode
sun indicating light mode

Ruby - if statement?

January 15, 2024


In Ruby, the if statement looks like this

val = 1
if val == 1
p "Equality Checked!"
end

And for if else

val = 2
if val == 1
p "Equality Checked!"
else
p "Equality Unchecked!"
end

And for if, else if, else

val = 2
if val == 1
p "Equality Checked!"
elsif val == 2
p "Equality Middle Checked!"
else
p "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 statement
ret_val = if val == 1
p "Equality Checked!"
else
p "Equality Unchecked!"
end
p ret_val # => "Equality Unchecked!"

Dhanyavaad! 🙇

Edit on githubTweet

With 💗 by Aleem Isiaka.
Software Engineer >>> Computer && Machines | Learner && Writer