Created
March 23, 2015 15:01
-
-
Save tibastral/7af22d1b0ec7e6f28ba9 to your computer and use it in GitHub Desktop.
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
# Array + Hashes | |
a = [ 1, 'cat', 3.14 ] # array with three elements | |
puts "The first element is #{a[0]}" | |
a[2] = nil | |
puts "The array is now #{a.inspect}" | |
# -> The first element is 1 | |
# -> The array is now [1, "cat", nil] | |
a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ] | |
a[0] # => "ant" | |
a[3] # => "dog" | |
# this is the same: | |
a = %w{ ant bee cat dog elk } | |
a[0] # => "ant" | |
a[3] # => "dog" | |
inst_section = { | |
'cello' => 'string', | |
'clarinet' => 'woodwing', | |
'violin' => 'string', | |
'drum' => 'percussion' | |
} | |
:cello | |
inst_section = { | |
cello: 'string', | |
clarinet: 'woodwing', | |
violin: 'string', | |
drum: 'percussion' | |
} | |
inst_section[:cello] | |
inst_section[:drum] | |
inst_section[:violin] | |
inst_section[:tuba] | |
# Symbols | |
NORTH = 1 | |
EAST = 2 | |
SOUTH = 3 | |
WEST = 4 | |
walk(:north) | |
look(:east) | |
# If then else | |
if count > 10 | |
puts "Try again" | |
elsif tries == 3 | |
puts "You lose" | |
else | |
puts "Enter a number" | |
end | |
while weight < 100 and num_pallets <= 30 | |
pallet = next_pallet() | |
weight += pallet.weight | |
num_pallets += 1 | |
end | |
square = 2 | |
while square < 1000 | |
square = square*square | |
end | |
# Regex | |
/P(erl|ython)/ | |
/.+@.+\..+/ | |
'Perl' =~ /P(erl|ython)/ | |
'thibaut@milesrock.com' =~ /.+@.+\..+/ | |
line.gsub(/Python/, 'Ruby') | |
# Blocks | |
{ puts "Hello" } | |
def call_block | |
puts "Start of method" | |
yield | |
yield | |
puts "End of method" | |
end | |
phone_numbers = ['0679389289', '078392982'] | |
phone_numbers.each do |phone_number| | |
if phone_number.index(1) == '6' | |
call_number(phone_number) | |
end | |
end | |
call_block { puts "In the block" } | |
def who_says_what | |
yield("Dave", "hello") | |
yield("Andy", "goodbye") | |
end | |
who_says_what { |a, b| | |
puts "#{a} says #{b}" | |
} | |
animals = %w( ant bee cat dog elk ) | |
animals.each {|animal| puts animal } | |
class Furniture | |
attr_accessor :length, :height, :name, :weight | |
end | |
furniture = Furniture.new | |
furniture.length = 2 | |
# Classes | |
class BookInStock | |
end | |
class BookInStock | |
def initialize(isbn, price) | |
@isbn = isbn | |
@price = Float(price) | |
end | |
end | |
class BookInStock | |
def initialize(isbn, price) | |
@isbn = isbn | |
@price = Float(price) | |
end | |
def isbn | |
return @isbn | |
end | |
def price | |
@price | |
end | |
end | |
book = BookInStock.new("isbn1", 12.34) | |
puts "ISBN = #{book.isbn}" | |
puts "Price = #{book.price}" | |
class BookInStock | |
attr_reader :isbn, :price | |
def initialize(isbn, price) | |
@isbn = isbn | |
@price = Float(price) | |
end | |
end | |
book = BookInStock.new("isbn1", 12.34) | |
puts "ISBN = #{book.isbn}" | |
puts "Price = #{book.price}" | |
attr_reader :isbn, :price | |
attr_writer :isbn, :price | |
attr_accessor :isbn, :price | |
def price=(new_price) | |
@price = new_price | |
end | |
book.price=(2) | |
book.price = 2 | |
class BookInStock | |
attr_reader :isbn | |
attr_accessor :price | |
def initialize(isbn, price) | |
@isbn = isbn | |
@price = Float(price) | |
end | |
def price_in_cents | |
Integer(price*100 + 0.5) | |
end | |
def price_in_cents=(cents) | |
@price = cents / 100.0 | |
end | |
end | |
book = BookInStock.new("isbn1", 33.80) | |
puts "Price = #{book.price}" | |
puts "Price in cents = #{book.price_in_cents}" | |
book.price_in_cents = 1234 | |
puts "Price = #{book.price}" | |
puts "Price in cents = #{book.price_in_cents}" | |
------------------- | |
# public / private | |
# introspection | |
person = "Tim" | |
puts "The object in 'person' is a #{person.class}" | |
puts "The object has an id of #{person.object_id}" | |
puts "and a value of '#{person}'" | |
# Arrays | |
a = [ 3.14159, "pie", 99 ] a.class #=> Array a.length #=> 3 | |
a[0] #=> 3.14159 | |
a[1] #=> "pie" | |
a[2] #=> 99 | |
a[3] #=> nil | |
a=[1,3,5,7,9] | |
a[1] = 'bat' | |
a[-3] = 'cat' | |
a[3] = [ 9, 8 ] | |
a[6] = 99 | |
→ [1,3,5,7,9] | |
→ [1, "bat", 5, 7, 9] | |
→ [1, "bat", "cat", 7, 9] | |
→ [1, "bat", "cat", [9, 8], 9] | |
→ [1, "bat", "cat", [9, 8], 9, nil, 99] | |
# Files | |
f = File.open("testfile") f.each do |line| | |
puts "The line is: #{line}" end | |
f.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment