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
book('Charlie and the chocolate factory', 'Roald Dahl'). | |
book('Boy', 'Roald Dahl'). | |
book('Oliver Twist', 'Charles Dickens'). | |
book('A Christmas Carol', 'Charles Dickens'). | |
book('1984', 'George Orwell'). |
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
Builder := Object clone | |
Builder indent := 0 | |
Builder writeIndent := method(for(i, 1, indent, " " print)) | |
Builder forward := method( | |
self writeIndent | |
writeln("<", call message name, ">") | |
indent = indent +1 |
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
loop_fib := method(limit, | |
fib_sequence := list(1, 1) | |
i := 2 | |
while (i <= limit, i = i + 1; fib_sequence append(fib_sequence last + fib_sequence at((fib_sequence size) - 2))) | |
fib_sequence at(limit-1) | |
) | |
loop_fib(1) println | |
loop_fib(4) println |
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
1 + 1 println | |
(1 + "one") println |
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
class CsvRow | |
def initialize(headers, row) | |
@headers = headers | |
@row = row | |
end | |
def method_missing name | |
unless @headers.index(name.to_s).nil? | |
@row[@headers.index(name.to_s)] | |
end |
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
# With block - closes file automatically | |
File.open("test.txt") do |file| | |
puts file.readlines | |
end | |
# Without block | |
file = File.open("test.txt") | |
puts file.readlines | |
file.close |
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
#Print the string Hello World | |
puts "Hello, World" | |
#Find the index of Ruby in "Hello, Ruby" | |
"Hello, Ruby".index("Ruby") | |
#Print your name 10 times | |
(1..10).each do | |
puts "Jo Cranford" | |
end |
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
puts "What's the maximum number?" | |
max_num = gets().to_i | |
number_to_guess = rand(max_num + 1) | |
puts "Thinking of a number between 0 and #{max_num} ... OK." | |
guess = nil | |
while guess != number_to_guess do | |
puts "Please make a guess ..." | |
guess = gets().to_i |
NewerOlder