+
+
+
+
diff --git a/RubyProject/learn_couleurs_game-0.0.0.gem b/RubyProject/learn_couleurs_game-0.0.0.gem
new file mode 100644
index 0000000..6b9315d
Binary files /dev/null and b/RubyProject/learn_couleurs_game-0.0.0.gem differ
diff --git a/RubyProject/learn_couleurs_game-0.0.1.gem b/RubyProject/learn_couleurs_game-0.0.1.gem
new file mode 100644
index 0000000..26470eb
Binary files /dev/null and b/RubyProject/learn_couleurs_game-0.0.1.gem differ
diff --git a/RubyProject/learn_couleurs_game-0.0.2.gem b/RubyProject/learn_couleurs_game-0.0.2.gem
new file mode 100644
index 0000000..26b5de1
Binary files /dev/null and b/RubyProject/learn_couleurs_game-0.0.2.gem differ
diff --git a/RubyProject/learn_couleurs_game.gemspec b/RubyProject/learn_couleurs_game.gemspec
new file mode 100644
index 0000000..8ca4b8f
--- /dev/null
+++ b/RubyProject/learn_couleurs_game.gemspec
@@ -0,0 +1,13 @@
+Gem::Specification.new do |s|
+s.name = 'learn_couleurs_game'
+s.version = '0.0.2'
+s.date = '2013-12-10'
+s.summary = "Project for Ruby Class"
+s.description = "A gem to learn the names of colors in French"
+s.authors = ["Alicia French"]
+s.email = 'alienor18@yahoo.com'
+s.homepage = 'http://rubygems.org/gems/learn_couleurs_game'
+s.files = ['lib/learn_couleurs_game.rb','lib/learn_couleurs_game/learn_couleurs.rb']
+s.executables << 'learn_couleurs_game'
+end
+
diff --git a/RubyProject/lib/learn_couleurs_game.rb b/RubyProject/lib/learn_couleurs_game.rb
new file mode 100644
index 0000000..32af7c5
--- /dev/null
+++ b/RubyProject/lib/learn_couleurs_game.rb
@@ -0,0 +1,15 @@
+require 'learn_couleurs_game/learn_couleurs.rb'
+
+@game = LearnCouleurs.new
+puts "What is your name?"
+@game.player = gets.chomp
+puts @game.welcome_player
+@game.selection
+
+until @game.over?
+ @game.display_new_color
+ @game.player_answer
+ @game.display_score
+end
+ @game.comment_final_score
+
diff --git a/RubyProject/lib/learn_couleurs_game/learn_couleurs.rb b/RubyProject/lib/learn_couleurs_game/learn_couleurs.rb
new file mode 100644
index 0000000..5bec281
--- /dev/null
+++ b/RubyProject/lib/learn_couleurs_game/learn_couleurs.rb
@@ -0,0 +1,84 @@
+class LearnCouleurs
+
+ attr_accessor :player,:score,:over,:current_couleur
+
+ def initialize
+ @player = ""
+ @couleurs = { 40 => "noir", 41 => "rouge", 42 => "vert", 43 => "jaune", 44 => "bleu", 47 => "gris"
+ }
+ @current_couleur = 0
+ @over = false
+ @rounds = -1
+ @score = 0
+ @max_rounds = 5
+ end
+
+ def welcome_player
+ welcome = "Bienvenue "+ @player + "\n\n"
+ end
+
+ def selection
+ puts "Reviser(r) ou Commencer(c)?"
+ selection = gets.chomp
+ if selection == "r"
+ display_couleurs
+ elsif selection == "c"
+ @rounds +=1
+ else
+ abort("Invalide")
+ end
+ end
+
+ def display_couleurs
+ puts "Liste couleurs:"
+ @couleurs.each { |key, value| print " \033["+key.to_s+"m \033[0m" + ' = ' + value + "\n\n" }
+ @over = true
+ end
+
+ def over?
+ if @rounds == @max_rounds
+ @over = true
+ end
+ @over
+ end
+
+ def display_new_color
+ @current_couleur = @couleurs.keys.sample
+ puts "Quelle couleur?"
+ couleur_code = " \033["+@current_couleur.to_s+"m \033[0m\n"
+ puts couleur_code
+ end
+
+ def player_answer
+ answer = gets.chomp
+ until answer == @couleurs[@current_couleur]
+ puts "Oh non! Essaie encore:\n\n"
+ @score-=1
+ answer = gets.chomp
+ end
+ puts "Bravo!\n\n"
+ @score+=1
+ @rounds+=1
+ end
+
+ def display_score
+ puts "Score: "+@score.to_s+"/"+@max_rounds.to_s
+ end
+
+ def comment_final_score
+ if @rounds == -1
+ puts "Essaie de jouer maintenant!"
+ elsif @score < 0
+ puts "CATASTROPHE!!!"
+ elsif @score < @max_rounds/2
+ puts "MOUAIS..."
+ elsif @score < @max_rounds*3/4
+ puts "BIEN!"
+ elsif @score < @max_rounds
+ puts "TRES BIEN!"
+ else
+ puts "PARFAIT!!!"
+ end
+ end
+
+end
diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt
index bd581a6..073fb47 100644
--- a/week1/homework/questions.txt
+++ b/week1/homework/questions.txt
@@ -3,13 +3,21 @@ Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)
1. What is an object?
+An object is a class instance.
2. What is a variable?
+A variable is a reference to an object.
3. What is the difference between an object and a class?
+A class is like a model for things used in the application, and an object is what is created according to this model.
4. What is a String?
+A String is a sequence of characters. It is an object of class String.
5. What are three messages that I can send to a string object? Hint: think methods
+downcase, reverse, empty?.
6. What are two ways of defining a String literal? Bonus: What is the difference between the two?
+A String literal can be defined with single quotes or double quotes.
+Strings defined with single quotes can only use a limited number of escape sequences (backslash and single quote) but require less work from the interpreter, whereas strings defined with double quotes can handle a lot more escape sequences (/n, /t...).
+
diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb
index ea79e4c..57dc943 100644
--- a/week1/homework/strings_and_rspec_spec.rb
+++ b/week1/homework/strings_and_rspec_spec.rb
@@ -12,14 +12,15 @@
before(:all) do
@my_string = "Renée is a fun teacher. Ruby is a really cool programming language"
end
- it "should be able to count the charaters"
- it "should be able to split on the . charater" do
- pending
- result = #do something with @my_string here
+ it "should be able to count the charaters" do
+ @my_string.should have(66).characters
+ end
+ it "should be able to split on the . character" do
+ result = @my_string.split(".")
result.should have(2).items
end
it "should be able to give the encoding of the string" do
- pending 'helpful hint: should eq (Encoding.find("UTF-8"))'
+ "#{@my_string.encoding}".should eq "UTF-8"
end
end
end
diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt
index 939e42d..f6621a6 100644
--- a/week2/homework/questions.txt
+++ b/week2/homework/questions.txt
@@ -3,11 +3,19 @@ Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins
1. What is the difference between a Hash and an Array?
+Both are indexed collections of object references, but Arrays are indexed with integers, whereas Hashes are indexed with objects of any type (symbols, strings, etc.). Also Arrays are ordered collections, whereas Hashes are not necessarily ordered.
2. When would you use an Array over a Hash and vice versa?
+I would use an Array over a Hash if I need to have an ordered collection, and I would use a Hash over an array if I need to map information to words or other objects.
3. What is a module? Enumerable is a built in Ruby module, what is it?
+Modules are a way of grouping together methods, classes, and constants.
+Enumerable is a standard mixin, implementing a bunch of methods (to iterate, map, search, etc.) in terms of the host class's each method.
4. Can you inherit more than one thing in Ruby? How could you get around this problem?
+No, Ruby is a single-inheritance language. But we could get around this problem by using mixins, since Ruby classes can include the functionality of any number of mixins.
5. What is the difference between a Module and a Class?
+A Module provides methods that can be used across multiple Classes. It cannot generate objects or inherit from a parent like Classes do. It can be mixed into other Modules or Classes whereas a Class cannot.
+
+
diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb
new file mode 100644
index 0000000..6c9eec5
--- /dev/null
+++ b/week2/homework/simon_says.rb
@@ -0,0 +1,23 @@
+module SimonSays
+
+ def echo(input)
+ input
+ end
+
+ def shout(input)
+ input.upcase
+ end
+
+ def repeat(input, number=2)
+ ([input] * number).join ' '
+ end
+
+ def start_of_word(input, number)
+ input[0...number]
+ end
+
+ def first_word(input)
+ input.split(" ")[0]
+ end
+
+end
diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb
new file mode 100644
index 0000000..88f076a
--- /dev/null
+++ b/week3/homework/calculator.rb
@@ -0,0 +1,28 @@
+class Calculator
+
+ def sum(input=0)
+ input.inject(0) {|sum, i| sum + i}
+ end
+
+ def multiply(input1=0,input2=0 )
+
+ if !input1.kind_of?(Array)
+ input1 * input2
+ else
+ input1.inject(1) {|sum, i| sum * i}
+ end
+ end
+
+ def pow(input1=0, input2=0)
+ input1**input2
+ end
+
+ def fac(input=0)
+ if input == 0
+ 1
+ else
+ input * fac(input-1)
+ end
+ end
+
+end
diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt
index dfb158d..a49b082 100644
--- a/week3/homework/questions.txt
+++ b/week3/homework/questions.txt
@@ -5,11 +5,19 @@ Please Read:
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants
1. What is a symbol?
+A symbol is an object used to represent names and strings in the Ruby interpreter.
2. What is the difference between a symbol and a string?
+Unlike strings, symbols are immutable.
3. What is a block and how do I call a block?
+A block is a chunk of code that can be associated with method invocations.
+I call a block using curly brackets ({}) or by writing between "do" and "end".
4. How do I pass a block to a method? What is the method signature?
+You can pass a block to a method using the "yield" statement in the method definition, and with the block appended to the method call.
+The method signature is its name and the names and default values of its arguments.
5. Where would you use regular expressions?
+I would use regular expressions for processing text or search patterns for instance.
+
diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt
index bc1ab7c..1eac29f 100644
--- a/week4/homework/questions.txt
+++ b/week4/homework/questions.txt
@@ -3,7 +3,20 @@ Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/
1. How does Ruby read files?
+Ruby can read files via the file.gets method used with a loop, or with iterators such as IO#each_line or IO.foreach.
+
2. How would you output "Hello World!" to a file called my_output.txt?
+I would use the following code:
+File.open("output.txt", "w") do |file|
+ file.puts "Hello World!"
+end
+
3. What is the Directory class and what is it used for?
+The Dir class creates directory streams representing directories in the underlying file system. It is used to create, query, filter or remove directories.
+
4. What is an IO object?
+An IO object is a bidirectional channel between a Ruby program and some external resource.
+
5. What is rake and what is it used for? What is a rake task?
+Rake is a make-like build tool written in Ruby. It is used for job automation: to specify tasks, describe dependencies and group tasks in a namespace. A rake task can be one of the built-in types or a block of my own Ruby code.
+
diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb
new file mode 100644
index 0000000..6c41d28
--- /dev/null
+++ b/week4/homework/worker.rb
@@ -0,0 +1,6 @@
+class Worker
+ def self.work(input = 1)
+ (input-1).times {yield}
+ yield
+ end
+end
diff --git a/week4/homework/worker_spec.rb b/week4/homework/worker_spec.rb
new file mode 100644
index 0000000..dfc6f02
--- /dev/null
+++ b/week4/homework/worker_spec.rb
@@ -0,0 +1,36 @@
+require "#{File.dirname(__FILE__)}/worker"
+
+describe Worker do
+
+ it "executes a block and returns a string" do
+ result = Worker.work do
+ "hello"
+ end
+ result.should == "hello"
+ end
+
+ it "executes a block and returns a number" do
+ result = Worker.work do
+ 3 + 4
+ end
+ result.should == 7
+ end
+
+ it "executes a block in the context of the calling method" do
+ n = 1
+ result = Worker.work do
+ n + 4
+ end
+ result.should == 5
+ end
+
+
+ it "executes a block 3 times and returns the result" do
+ n = 5
+ result = Worker.work(3) do
+ n += 1
+ end
+ result.should == 8
+ end
+
+end
diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb
new file mode 100644
index 0000000..013f933
--- /dev/null
+++ b/week7/homework/features/step_definitions/tic-tac-toe.rb
@@ -0,0 +1,108 @@
+class TicTacToe
+ attr_accessor :player,:current_player,:board
+
+ def initialize
+ @player = ""
+ @SYMBOLS = ["X", "O"]
+ @player_symbol = ""
+ @computer_symbol = ""
+ @over = false
+ @current_player = ""
+ @board = {
+ :A1 => " ", :A2 => " ", :A3 => " ",
+ :B1 => " ", :B2 => " ", :B3 => " ",
+ :C1 => " ", :C2 => " ", :C3 => " "
+ }
+ @open_spots = [:A1,:A2,:A3,:B1,:B2,:B3,:C1,:C2,:C3]
+ @player_won = false
+ @computer_won = false
+ @draw = false
+ end
+
+ def welcome_player
+ welcome = "Welcome "+ @player
+ players = ["Computer", @player]
+ @current_player = players.sample
+ @SYMBOLS.shuffle
+ @player_symbol = @SYMBOLS[0]
+ @computer_symbol = @SYMBOLS[1]
+ welcome
+ end
+
+ def computer_move
+ puts "Computer moves"
+ cMove = @open_spots.sample
+ @board[cMove]=@computer_symbol
+ @open_spots.delete(cMove)
+ @current_player=@player
+ end
+
+ def indicate_player_turn
+ puts @player + "'s Move:"
+ end
+
+ def player_move
+ pMove = gets.chomp
+ valid = false
+ until valid == true
+ if @board.keys.include?(pMove.to_sym)==false
+ puts "Invalid input!"
+ pMove = gets.chomp
+ elsif @open_spots.include?(pMove.to_sym)==false
+ puts "Spot already taken!"
+ pMove = gets.chomp
+ else
+ valid = true
+ end
+ end
+ @board[pMove.to_sym]=@player_symbol
+ @open_spots.delete(pMove.to_sym)
+ @current_player="Computer"
+ end
+
+ def current_state
+ current_state = " 1 2 3\n -----\n "+"A|#{@board[:A1]} "+"#{@board[:A2]} "+"#{@board[:A3]}|"+"\n "+"B|#{@board[:B1]} "+"#{@board[:B2]} "+"#{@board[:B3]}|"+"\n "+"C|#{@board[:C1]} "+"#{@board[:C2]} "+"#{@board[:C3]}|"+"\n -----"
+ end
+
+ def over?
+ #detect 3 consecutives symbols
+ if (@board[:A1]==@board[:A2] && @board[:A1]==@board[:A3] && @board[:A1]=="X" ) || (@board[:B1]==@board[:B2] && @board[:B1]==@board[:B3] && @board[:B1]=="X" ) || (@board[:C1]==@board[:C2] && @board[:C1]==@board[:C3] && @board[:C1]=="X" ) || (@board[:A1]==@board[:B1] && @board[:A1]==@board[:C1] && @board[:A1]=="X" ) || (@board[:A2]==@board[:B2] && @board[:A2]==@board[:C2] && @board[:A2]=="X" ) || (@board[:A3]==@board[:B3] && @board[:A3]==@board[:C3] && @board[:A3]=="X" ) || (@board[:A1]==@board[:B2] && @board[:A1]==@board[:C3] && @board[:A1]=="X" ) || (@board[:A3]==@board[:B2] && @board[:A3]==@board[:C1] && @board[:A3]=="X" )
+ if @player_symbol == "X"
+ @player_won = true
+ elsif @computer_symbol == "X"
+ @computer_won = true
+ end
+ @over = true
+ elsif (@board[:A1]==@board[:A2] && @board[:A1]==@board[:A3] && @board[:A1]=="O" ) || (@board[:B1]==@board[:B2] && @board[:B1]==@board[:B3] && @board[:B1]=="O" ) || (@board[:C1]==@board[:C2] && @board[:C1]==@board[:C3] && @board[:C1]=="O" ) || (@board[:A1]==@board[:B1] && @board[:A1]==@board[:C1] && @board[:A1]=="O" ) || (@board[:A2]==@board[:B2] && @board[:A2]==@board[:C2] && @board[:A2]=="O" ) || (@board[:A3]==@board[:B3] && @board[:A3]==@board[:C3] && @board[:A3]=="O" ) || (@board[:A1]==@board[:B2] && @board[:A1]==@board[:C3] && @board[:A1]=="O" ) ||
+ (@board[:A3]==@board[:B2] && @board[:A3]==@board[:C1] && @board[:A3]=="O" )
+ if @player_symbol == "O"
+ @player_won = true
+ elsif @computer_symbol == "O"
+ @computer_won = true
+ end
+ @over = true
+ elsif @open_spots.empty?
+ @draw = true
+ @over = true
+ end
+ @over
+ end
+
+ def player_won?
+ @player_won
+ end
+
+ def computer_won?
+ @computer_won
+ end
+
+ def draw?
+ @draw
+ end
+
+ def determine_winner
+
+ end
+
+end
+
diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb
index 0535830..7b99f10 100644
--- a/week7/homework/play_game.rb
+++ b/week7/homework/play_game.rb
@@ -10,7 +10,7 @@
when "Computer"
@game.computer_move
when @game.player
- @game.indicate_palyer_turn
+ @game.indicate_player_turn
@game.player_move
end
puts @game.current_state
diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt
index d55387d..f35323a 100644
--- a/week7/homework/questions.txt
+++ b/week7/homework/questions.txt
@@ -3,7 +3,22 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming
Questions:
1. What is method_missing and how can it be used?
+When sending a message to an object if the object has no method corresponding to the message a NoMethodError exception is created. This exception can be avoided though by adding a method called method_missing to the object. This method allows to handle nicely unaswerable messages. It can be used by defining method_missing in the object's class, and we can pass the name of the wrongly called method, the array of arguments and the block that was originally passed (if any).
+ def method_missing(method, *args, &block)
+ #add here desired behavior in case of non-existing method
+ end
+
2. What is and Eigenclass and what is it used for? Where Do Singleton methods live?
+An Eigenclass is an anonymous class created dynamically by Ruby when methods are created for a specific object and not shared by other instances of this object's class.
+Such methods are called Singleton methods and live in the Eigenclass.
+
3. When would you use DuckTypeing? How would you use it to improve your code?
+I would use Ducktyping to keep my code concise (DRY).
+
4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval?
+A class method is a method that is called on a class and an instance method is a method that is called on an instance of a class. Class_eval and instance_eval differ in the way they set up the environment for method definition. Class_eval sets the environment as if in the body of a class definition, so method definitions will define instance methods. On the contrary calling instance_eval on a class acts as if working inside the singleton class of self. Any methods defined will become class methods.
+
5. What is the difference between a singleton class and a singleton method?
+A singleton class is a class which defines a single object.
+A singleton method is a method that belongs to a single object rather than to a whole class and other objects.
+