The Free Online Ruby Programming Course - Week-01 07/Jan/2008
Comecei hoje a minha aprendizagem online, graças à iniciativa FORPC101 do Satish Salim.
A primeira semana decorre sem imprevistos e a bom ritmo…
Aqui ficam os exercícios resolvidos, em comentário a este “artigo”...
Update 2008.01.18: Abri a WikiPage para registar a minha evolução e guardar notas que considero importante no âmbito deste curso de aprendizagem online.
- Posted in : doCoding
- Author : José Carlos Monteiro
Comments»
Exercício 1:
A precedência do operador “=” (assignment) é superior à do operador “or”. Por isso:
x = y or z # x = false (y = false) OR z = true
puts x
=> false
Na realidade, a instrução anterior é a “condensada” da instrução
(x = y) or z
=> false
Com os parêntesis dando maior prioridade ao operador “or” então
x = (y or z)
=> true
Exercício 2:
Parece que afinal os parêntesis serem opcionais não uma vantagem assim tão grande. Senão veja-se:
my_string = ‘Hello Ruby World’
def my_string
‘Hello World’
end
puts my_string
puts my_string()
Isto vem indicado em http://rubylearning.com/satishtalim/variables_and_assignment.html: “Here’s how Ruby decides what it’s seeing when it encounters a bareword: (a) If there’s an equal sign (=) to the right of the bareword, it’s a local variable undergoing an assignment. (b) If the bareword is a keyword, it’s a keyword (Ruby has an internal list of these and recognizes them). (c) Otherwise, the bareword is assumed to be a method call.”
Exercício 3:
Este é bastante fácil: Imprime 00123, que é o número 123 formatado com 5 casas numéricas sendo que as sem dígito assumem o valor 0.
Exercício 4:
=begin
Write a Ruby program that displays how old I am, if I am 979000000 seconds old.
Note: To format the output to say 2 decimal places, we can use the Kernel’s format method. For example, if x = 45.5678 then format(“%.2f”, x) will return a string 45.57
=end
age_in_seconds = 979000000
age_in_minutes = age_in_seconds / 60
age_in_hours = age_in_minutes / 60
age_in_days = age_in_hours / 24
age_in_years = age_in_days / 365
puts ‘I am ’ + format(“%.2f”, age_in_years) + ’ years old…’
age_in_years = age_in_days / 365.00
puts ‘I am ’ + format(“%.2f”, age_in_years) + ’ years old…’
Exercício 5:
=begin
Write a Ruby program that tells you how many minutes are there in a year (do not bother right now about leap years etc.).
=end
days_in_year = 365
hours_in_days = days_in_year * 24
minutes_in_days = hours_in_days * 60
puts ‘There are ’ + minutes_in_days.to_s + ’ in a year’
Esta informação terá que ser passada para o Wiki, por causa de SyntaxHighlight (maior legibilidade do código).
No fim, colocar um tar.bz2 com os exercícios resolvidos.