Created
October 18, 2013 13:20
-
-
Save edolopez/7041448 to your computer and use it in GitHub Desktop.
A simple way to use functions (methods) in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # encoding: UTF-8 | |
| # Pide calificación mexicana y convierte a americana | |
| def pregunta_usuario(variable) | |
| puts "sigo en el bloque" | |
| begin | |
| # acciones al menos 1 vez | |
| puts "Dame un valor entero para la calificación que quieres convertir" | |
| calificacion = gets.to_i | |
| # Fíjense como toma el valor que devuelve convierte_nota(calificacion) | |
| # y lo asigna a la variable "americana" | |
| americana = convierte_nota(calificacion) | |
| puts "Su nota americana es: #{americana}!!" | |
| 5.times {print "************"} | |
| end while (calificacion < 0) | |
| # esta función no devuelve ningún valor | |
| end | |
| def convierte_nota(nota_mexicana) | |
| puts "nota_mexicana: #{nota_mexicana}" | |
| case nota_mexicana | |
| when 40 | |
| nota_americana = "F" | |
| when 50, 70 | |
| nota_americana = "D" | |
| when 80, 90 | |
| nota_americana = "C" | |
| when 100 | |
| nota_americana = "A" | |
| else | |
| nota_americana = "Indeterminada" | |
| end | |
| nota_americana # esta variable se está devolviendo a la función que la llamó | |
| end | |
| # Pregunta 10 veces por 10 calificaciones | |
| cont = 0 | |
| while cont < 10 | |
| print "\nNombre del alumno a evaluar: " | |
| alumno = gets | |
| pregunta_usuario(alumno) | |
| cont += 1 | |
| end | |
| # ejemplo de uso | |
| # for i in 0..10 | |
| # pregunta_usuario(variable) | |
| # end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment