print("Hello, world!")
puts 'Hello, world!'
var myVariable = 42
myVariable = 50
let myConstant = 42
my_variable = 42
my_variable = 50
MY_CONSTANT = 42
let explicitDouble: Double = 70
explicit_double = 70.0
let label = "The width is "
let width = 94
let widthLabel = label + String(width)
label = "The width is "
width = 94
widthLabel = label + width.to_s
let apples = 3
let oranges = 5
let fruitSummary = "I have \(apples + oranges) " +
"pieces of fruit."
apples = 3
oranges = 5
fruit_summary = "I have #{apples + oranges} " <<
"pieces of fruit."
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..count {
print("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
names = %w(Anna Alex Brian Jack)
count = names.size
(0...count).each do |i|
puts "Person #{i + 1} is called #{names[i]}"
end
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
(1..5).each do |index|
puts "#{index} times 5 is #{index * 5}"
end
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
var shoppingList = ["catfish", "water",
"tulips", "blue paint"]
shoppingList[1] = "bottle of water"
shopping_list = ["catfish", "water",
"tulips", "blue paint"]
shopping_list[1] = "bottle of water"
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
for (key, value) in occupations {
print(key)
}
occupations = {
"Malcolm" => "Captain",
"Kaylee" => "Mechanic"
}
occupations["Jayne"] = "Public Relations"
occupations.each do |key, value|
print(key)
end
var emptyArray: [String] = []
var emptyDictionary: [String: Float] = [:]
empty_array = []
empty_map = {}
func greet(name: String, day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
def greet(name, day)
"Hello #{name}, today is #{day}."
end
greet("Bob", "Tuesday")
func getGasPrices() -> (Double, Double, Double) {
return (3.59, 3.69, 3.79)
}
def gas_prices
[3.59, 3.69, 3.79]
end
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf(42, 597, 12)
def sum_of(*numbers)
sum = 0
for number in numbers
sum += number
end
sum
end
sum_of(42, 597, 12)
// the body of the function can be written just as:
numbers.reduce &:+
func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)
def make_incrementer
def add_one number
1 + number
end
end
increment = make_incrementer
increment(7)
// makeIncrementer() can also be written in a shorter way:
make_incrementer = lambda { |number| 1 + number }
var numbers = [20, 19, 7, 12]
numbers.map({ number in 3 * number })
numbers = [20, 19, 7, 12]
numbers.map { |number| 3 * number }
sort([1, 5, 3, 12, 2]) { $0 > $1 }
[1, 5, 3, 12, 2].sort
def area(width: Int, height: Int)
return width * height
}
area(width: 10, height: 10)
def area(width:, height:)
width * height
end
area(height: 10, width: 10)
class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
class Shape
attr_writer :number_of_sides
def initialize
@number_of_sides = 0
end
def simple_description
"A shape with #{@number_of_sides} sides."
end
end
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
shape = Shape.new
shape.number_of_sides = 7
shape_description = shape.simple_description
class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
override func simpleDescription() -> String {
return "A square with sides of length \(sideLength)."
}
}
let test = Square(sideLength: 5.2)
test.area()
test.simpleDescription()
class NamedShape
attr_writer :number_of_sides
def initialize(name)
@name = name
@number_of_sides = 0
end
def simpleDescription
"A shape with #{@number_of_sides} sides."
end
end
class Square < NamedShape
def initialize(side_length, name = '')
@side_length = side_length
@number_of_sides = 4
super name
end
def area
@side_length ** 2
end
def simple_description
"A square with sides of length #{@side_length}."
end
end
test = Square.new 5.2
test.area
test.simple_description
var movieCount = 0
var songCount = 0
for item in library {
if item is Movie {
++movieCount
} else if item is Song {
++songCount
}
}
movie_count = 0
song_count = 0
library.each do |item|
if item.is_a? Movie
movie_count += 1
elsif item.is_a? Song
song_count += 1
end
end
extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
("One inch is \(oneInch) meters")
// prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// prints "Three feet is 0.914399970739201 meters"
class Float
def km
self * 1000
end
def m
self
end
def cm
self / 100
end
def mm
self / 1000
end
def ft
self / 3.28084
end
end
one_inch = 25.4.mm
puts "One inch is #{one_inch} meters"
# prints "One inch is 0.0254 meters"
three_feet = 3.0.ft
puts "Three feet is #{three_feet} meters"
# prints "Three feet is 0.914399970739201 meters"