Sintaxe de comando Ruby If, Else If

Um Yeedi Cube no carpete mostrando a docking station

A linguagem Ruby possui uma estrutura de controle muito simples, fácil de ler e seguir.

Se sintaxe

se var == 10

imprima “A variável é 10”

fim

If Else Sintaxe

se var == 10

imprima “A variável é 10”

outro

print “Variável é outra coisa”

fim

If Else If Sintaxe

Aqui está a principal diferença entre Ruby e a maioria das outras linguagens. Observe que “else if” é na verdade escrito “elsif” sem o e.

se var == 10

imprima “A variável é 10”

elsif var == “20”

imprima “A variável é 20”

outro

print “Variável é outra coisa”

fim

Sintaxe ternária (instrução if abreviada)

A sintaxe ternária é a mesma em Ruby e na maioria das linguagens. O exemplo a seguir imprimirá “A variável é 10” se var for igual a 10. Caso contrário, imprimirá “A variável não é 10”.

print “A variável é ” + (var == 10 ? “10” : “Não é 10”)

Recomendado:  Samsung Galaxy S20: Como desativar completamente o Bixby