Skip to content

Instantly share code, notes, and snippets.

View alebaffa's full-sized avatar
🗼
Focusing

Alessandro alebaffa

🗼
Focusing
View GitHub Profile
class Dog
def initialize(name)
@name = name
end
def bark
puts "#{@name} says woof!"
end
def eat
class Dog
def initialize(name)
@name = name
end
def bark
'woof!'
end
def eat
class Dog
def initialize(name)
@name = name
end
def bark
'woof!'
end
def eat
module UnpredictableString
def scramble
self.split(//).shuffle.join
end
end
class String
include UnpredictableString
end
@alebaffa
alebaffa / w5_exe1_2.rb
Created March 10, 2014 14:03
This is a second version, but with the method scramble this time as a class method (instead of instance method).
class String
def self.scramble
"this is an example".split(//).shuffle.join
end
end
puts String.scramble
@alebaffa
alebaffa / unpredictable_string.rb
Created March 10, 2014 22:01
String class does not call to_s when it is representing its own objects. So Kernel#puts which generally calls an objects to_s method does not do so when the object inherits from String.
class UnpredictableString
def initialize string
@self_badname = string
end
def scramble
@self_badname.split(//).shuffle.join
end
def to_s
@alebaffa
alebaffa / shapes.rb
Created March 11, 2014 20:34
Week 5 exercise 2.
class Shape
def initialize name
@name = name
end
def rotate(rotation_type)
"#{@name} is rotating around #{rotation_type}"
end
def play_sound(format)
"#{@name} is playing file #{@name}.#{format}"
end
class Shape
def rotate
'Rotating around the center of ' + self.class.name
end
def play_sound
"I am playing the sound #{self.class.name}.aif"
end
end
@alebaffa
alebaffa / gameboard.rb
Created March 12, 2014 21:31
Week 5 exe 3
class GameBoard
def initialize
@range = [0,1,2,3,4,5,6]
@no_of_hits = 0
end
def set_locations_cells(locations)
@numbers_to_guess = []
locations.each{|index| @numbers_to_guess.push(@range[index])}
end
@alebaffa
alebaffa / simple_repl.rb
Created March 13, 2014 20:55
Week 5 - Challange 1. Implement a very simple REPL for Ruby.
# simple_repl.rb
# This program replicate a very simple REPL for Ruby.
def prompt
print '>>'
user_input = gets.chomp!
puts("=> #{eval(user_input)}")
end
loop do
prompt