This file contains 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
# Write a Ruby program that tells you how many minutes there are in | |
# a year (do not bother right now about leap years etc.) | |
def minutes_in_year | |
puts "There are #{365 * 24 * 60} minutes in a year, in case you were curious." | |
end | |
minutes_in_year() |
This file contains 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
# Exercise 5: The following program prints the value of the variable. Why? | |
my_string = 'Hello Ruby World' | |
def my_string | |
'Hello World' | |
end | |
This file contains 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
# Write a method called convert that takes one argument which is a temperature in degrees Fahrenheit. | |
# This method should return the temperature in degrees Celsius. | |
# 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 the string 45.57. | |
def convert_c_f(fahrenheit_degrees) | |
celsius = (fahrenheit_degrees.to_f - 32) / 1.8 | |
printf "#{fahrenheit_degrees} degrees Fahrenheit is %.2f degrees Celsius. ", celsius | |
end |
This file contains 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
# Exercise 1 | |
# Write a program that processes the string s = "Welcome to the forum.\nHere you can learn Ruby.\nAlong with other members.\n" | |
# a line at a time, using all that we have learned so far. The expected output is: | |
# >ruby tmp.rb | |
# Line 1: Welcome to the forum. | |
# Line 2: Here you can learn Ruby. | |
# Line 3: Along with other members. | |
# >Exit code: 0 | |
string_to_slice = "Welcome to the forum.\nHere you can learn Ruby.\nAlong with other members.\n" |
This file contains 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
# Chapter Two - Class Hierarchies, Attributes and Class Variables | |
# Inheritance. How to create a descendent class in Ruby. | |
# In this example the @name and @description variables are asssigned values in the initialize | |
# method when a new Thign object is created. | |
# INSTANCE VARIABLES SHOULD NOT BE ACCESSED DIRECTLY FROM THE "OUTSIDE WORLD" (OOP: encapsulation) | |
# SO WHAT DO WE DO? | |
# To obtain the value of each variable, we need a 'get' accessor method such as get_name AND... | |
# To asign a new value you need a 'set' accessor method such as set_name | |
class Thing |
This file contains 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
x = 10 | |
5.times do |x| | |
puts "x inside the block #{x}" | |
end | |
puts "x outside the block: #{x}" | |
This file contains 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
def mtdarry | |
10.times do |num| | |
puts num | |
end | |
end | |
mtdarry | |
# output: | |
# 0 |
This file contains 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
# Exercise3. Write a method leap_year?. It should accept a | |
# year value from the user, check whether it's a leap year, and then | |
# return true or false. With the help of this leap_year?() method | |
# calculate and display the number of minutes in a leap year | |
# (2000 and 2004) and the number of minutes in a non-leap year | |
# (1900 and 2005). Note: Every year whose number is divisible by four | |
# without a remainder is a leap year, excepting the full centuries, which, | |
# to be leap years, must be divisible by 400 without a remainder. If not | |
# so divisible they are common years. 1900, therefore, is not a leap year. |
This file contains 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
def mtdarry | |
10.times do |num| | |
square = num * num | |
return num, square if num > 5 | |
end | |
end | |
num, square = mtdarry | |
puts num | |
puts square |
This file contains 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
# Here is an interesting example of a method that returns an array: | |
# if you give return multiple parameters, the method returns them in an array | |
# The times method of the Integer class iterates block num times, | |
# passing in values from zero to num -1 | |
def mtdarry | |
10.times do |num| | |
square = num * num | |
return num, square if num > 5 | |
end |