diff --git a/.gem/credentials b/.gem/credentials new file mode 100644 index 0000000..e69de29 diff --git a/.gem/lib/test_gem.rb b/.gem/lib/test_gem.rb new file mode 100644 index 0000000..e69de29 diff --git a/.gem/test_gem-0.0.0.gem b/.gem/test_gem-0.0.0.gem new file mode 100644 index 0000000..2a7cbe2 Binary files /dev/null and b/.gem/test_gem-0.0.0.gem differ diff --git a/.gem/test_gem.gemspec b/.gem/test_gem.gemspec new file mode 100644 index 0000000..d155ada --- /dev/null +++ b/.gem/test_gem.gemspec @@ -0,0 +1,11 @@ +Gem::Specification.new do |s| +s.name = 'test_gem_Thomas' +s.version = '0.0.0' +s.date = '2013-11-19' +s.summary = "Make a Test Gem" +s.description = "A gem to get a grade" +s.authors = ["Thomas Osborn"] +s.email = 'trosborn@gmail.com' +s.homepage = 'http://rubygems.org/gems/test_gem' +s.files = ["lib/test_gem.rb"] +end \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4a48538 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_store \ No newline at end of file diff --git a/lib/test_gem.rb b/lib/test_gem.rb new file mode 100644 index 0000000..e69de29 diff --git a/midterm/animal.rb b/midterm/animal.rb new file mode 100644 index 0000000..ebfe41a --- /dev/null +++ b/midterm/animal.rb @@ -0,0 +1,2 @@ +class Animal +end \ No newline at end of file diff --git a/midterm/dinner.rb b/midterm/dinner.rb new file mode 100644 index 0000000..918f10b --- /dev/null +++ b/midterm/dinner.rb @@ -0,0 +1,2 @@ +class Dinner +end \ No newline at end of file diff --git a/midterm/mid_term_spec.rb b/midterm/mid_term_spec.rb index 0556a97..096dfcb 100644 --- a/midterm/mid_term_spec.rb +++ b/midterm/mid_term_spec.rb @@ -58,7 +58,7 @@ # Dinners don't always have dessert, but ThanksgivingDinners always do! it "should have desserts" do - @t_dinner.menu[:desserts].should eq({:pies => [:pumkin_pie], :other => ["Chocolate Moose"], :molds => [:cranberry, :mango, :cherry]}) + @t_dinner.menu[:desserts].should eq({:pies => [:pumpkin_pie], :other => ["Chocolate Moose"], :molds => [:cranberry, :mango, :cherry]}) end end @@ -69,6 +69,6 @@ end it "should return what is on the dessert menu" do - @t_dinner.whats_for_dessert.should eq "Tonight we have 5 delicious desserts: Pumkin Pie, Chocolate Moose, and 3 molds: Cranberry and Mango and Cherry." + @t_dinner.whats_for_dessert.should eq "Tonight we have 5 delicious desserts: Pumpkin Pie, Chocolate Moose, and 3 molds: Cranberry and Mango and Cherry." end end diff --git a/midterm/thanksgiving_dinner.rb b/midterm/thanksgiving_dinner.rb new file mode 100644 index 0000000..6ac54bb --- /dev/null +++ b/midterm/thanksgiving_dinner.rb @@ -0,0 +1,24 @@ +require './dinner.rb' + +class ThanksgivingDinner < Dinner + + def initialize(diet) + @diet = diet + end + + def guests=(guests) + @guests = guests + end + + def seating_chart_size + @guests.inject(0) {|n, i| n += i.length } + end + + def menu + { diet: @diet, proteins: ["Tofurkey", "Hummus"], veggies: [:ginger_carrots , :potatoes , :yams], desserts: ({:pies => [:pumpkin_pie], :other => ["Chocolate Moose"], :molds => [:cranberry, :mango, :cherry]}) } + end + + def whats_for_dinner + @t_dinner.menu + end +end \ No newline at end of file diff --git a/midterm/turkey.rb b/midterm/turkey.rb new file mode 100644 index 0000000..49b347f --- /dev/null +++ b/midterm/turkey.rb @@ -0,0 +1,17 @@ +require './animal.rb' + +class Turkey < Animal + + def initialize(weight) + end + + def weight + weight = 10 + end + + def gobble_speak(turkey_words) + turkeys_words = "Gobble Gobble Gobble gobble Gobble. Gobble Gobb'le Gobble Gobble." + end + +end + diff --git a/test_gem.rb b/test_gem.rb new file mode 100644 index 0000000..e69de29 diff --git a/test_gem_Thomas-0.0.0.gem b/test_gem_Thomas-0.0.0.gem new file mode 100644 index 0000000..7f663d7 Binary files /dev/null and b/test_gem_Thomas-0.0.0.gem differ diff --git a/test_gem_Thomas.gemspec b/test_gem_Thomas.gemspec new file mode 100644 index 0000000..d155ada --- /dev/null +++ b/test_gem_Thomas.gemspec @@ -0,0 +1,11 @@ +Gem::Specification.new do |s| +s.name = 'test_gem_Thomas' +s.version = '0.0.0' +s.date = '2013-11-19' +s.summary = "Make a Test Gem" +s.description = "A gem to get a grade" +s.authors = ["Thomas Osborn"] +s.email = 'trosborn@gmail.com' +s.homepage = 'http://rubygems.org/gems/test_gem' +s.files = ["lib/test_gem.rb"] +end \ No newline at end of file diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..33298e8 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -4,12 +4,24 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +An object is anything "we manipulate in Ruby." This would not, for example, include variables, because a variable is merely a reference to an object. + 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 an object, but an object is not necessarily a class. Any class is an object of the class Class, and like any object it will inherit relevant class properties. However unlike a class a non-class object will not contain methods or reference a superclass. + 4. What is a String? +Strings are sequences of characters. They are objects of class String. + 5. What are three messages that I can send to a string object? Hint: think methods +chomp, split, count + 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? + +You can define a string literal by wrapped text in either single quotes or double quotes. The difference between the two is that a double quoted string can support more escape sequences. \ No newline at end of file diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..725d309 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 characters" 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 (Encoding.find("UTF-8")) end end end diff --git a/week2/.DS_Store b/week2/.DS_Store deleted file mode 100644 index 6f2aa37..0000000 Binary files a/week2/.DS_Store and /dev/null differ diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..e43fcdd 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -4,10 +4,20 @@ Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +An array is an indexed collection of object references. A hash is also an indexed collection of object references, but with an array you index integers and in a hash you index any object type but the object is indexed with a key value. + 2. When would you use an Array over a Hash and vice versa? +I would use a hash over an array when I wanted the indices for the objects to be more memorable. I would use an array when the ordering of the objects matter because hashes don't have an order and arrays do. + 3. What is a module? Enumerable is a built in Ruby module, what is it? +A module is a collection of methods and constants. Enumerable is a module that allows your + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +Ruby is a single inheritance language. However, Ruby can use a mixin to provide "multiple-inheritence-like" capability. + 5. What is the difference between a Module and a Class? + +Modules are collections of variables and/or methods, but are not instances of the class Class. They can however behave like a superclass by being added to a class. \ No newline at end of file diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..eb040ea --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,21 @@ +module SimonSays + def echo(string) + string + end + + def shout(string) + string.upcase + end + + def repeat(string, n=2) + [string] * n * ' ' #So may variable string is now an array... I am terrible at naming >< + end + + def start_of_word(string, n) + string[0..n-1] + end + + def first_word(string, n=1) + string.split(/ /)[0] + end +end \ No newline at end of file diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..c6ce7de --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,18 @@ +class Calculator + + def sum input + input.inject(0, :+) + end + + def multiply *input + input.flatten.inject(:*) + end + + def pow input1, input2 + input1**input2 + end + + def fac input + (1..input).inject(:*) || 1 + end +end \ No newline at end of file diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..c66d9e1 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -6,10 +6,20 @@ Please Read: 1. What is a symbol? +A symbol is an immutable representation of a string. + 2. What is the difference between a symbol and a string? +A string is mutable, a symbol is not. + 3. What is a block and how do I call a block? +A block is code that is called with curly braces or do ... end. + 4. How do I pass a block to a method? What is the method signature? +A block can be passed to a method with yield or it can be instantiated with an ampersand. + 5. Where would you use regular expressions? + +A regular express is useful for matching text against the contents of a string, such as when verifying the contents of a string or substituting pieces of the string. \ No newline at end of file diff --git a/week4/Rakefile.rb b/week4/Rakefile.rb new file mode 100644 index 0000000..ca1554d --- /dev/null +++ b/week4/Rakefile.rb @@ -0,0 +1,10 @@ +task :default => [:hello_world] + +desc "this outputs hello world" +task :hello_world do + puts "hello world!" +end + +def test + "tequila" +end \ No newline at end of file diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..847ae25 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 reads files through the File class, with the command File.open("file", "r") {|n| n.gets} + 2. How would you output "Hello World!" to a file called my_output.txt? + +File.open("output", "w") {|n| n.puts "Hello World!"} + 3. What is the Directory class and what is it used for? + +Objects in the directory class represent file directories on the computer, and it is used to list those directories and see thecontents. 4. What is an IO object? + +Your mom... is not an IO object. An IO object is an object of the class IO and is a representation of a source file, which allows for external communication with computer resources. + 5. What is rake and what is it used for? What is a rake task? + +Rake is Ruby make. It is for writing automation scripts and making dependecies, as well as grouping together like-tasks, kind of like you would with a module. A rank task is merely the set of commands Ruby uses to do those things I just mentioned. \ No newline at end of file diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..a338182 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,5 @@ +class Worker + def self.work i = 1 + i.times.inject(0){yield} + end +end \ No newline at end of file diff --git a/week4/homework/worker_spec.rb b/week4/homework/worker_spec.rb new file mode 100644 index 0000000..8fd0f70 --- /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 \ No newline at end of file diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb new file mode 100644 index 0000000..83391d9 --- /dev/null +++ b/week5/exercises/Rakefile.rb @@ -0,0 +1,5 @@ +task :outputs_name do + File.open("names").readlines.each do |line| + puts line + end +end \ No newline at end of file diff --git a/week7/MagicNineBall/Bin/magic_nine_ball.rb b/week7/MagicNineBall/Bin/magic_nine_ball.rb new file mode 100644 index 0000000..8360d76 --- /dev/null +++ b/week7/MagicNineBall/Bin/magic_nine_ball.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby + +require 'magic_nine_ball' + +class MagicNineBall + + print "What is your question " + question = gets.chomp + array = ["Yes, in due time.", + "My sources say no.", + "Definitely not.", + "Yes.", + "You will have to wait.", + "I have my doubts.", + "Outlook so so.", + "Looks good to me!", + "Who knows?", + "Looking good!", + "Probably.", + "Are you kidding?", + "Go for it!", + "Don't bet on it.", + "Forget about it."] + @array = array + puts @array.sample +end \ No newline at end of file diff --git a/week7/MagicNineBall/Lib/magicnineball.rb b/week7/MagicNineBall/Lib/magicnineball.rb new file mode 100644 index 0000000..5c72cfc --- /dev/null +++ b/week7/MagicNineBall/Lib/magicnineball.rb @@ -0,0 +1,24 @@ +#!/usr/bin/env ruby + +class MagicNineBall + + print "What is your question " + question = gets.chomp + array = ["Yes, in due time.", + "My sources say no.", + "Definitely not.", + "Yes.", + "You will have to wait.", + "I have my doubts.", + "Outlook so so.", + "Looks good to me!", + "Who knows?", + "Looking good!", + "Probably.", + "Are you kidding?", + "Go for it!", + "Don't bet on it.", + "Forget about it."] + @array = array + puts @array.sample +end \ No newline at end of file diff --git a/week7/MagicNineBall/magic_nine_ball-0.0.0.gem b/week7/MagicNineBall/magic_nine_ball-0.0.0.gem new file mode 100644 index 0000000..8ff8af7 Binary files /dev/null and b/week7/MagicNineBall/magic_nine_ball-0.0.0.gem differ diff --git a/week7/MagicNineBall/magic_nine_ball-0.0.1.gem b/week7/MagicNineBall/magic_nine_ball-0.0.1.gem new file mode 100644 index 0000000..13ee9bb Binary files /dev/null and b/week7/MagicNineBall/magic_nine_ball-0.0.1.gem differ diff --git a/week7/MagicNineBall/magic_nine_ball-0.0.3.gem b/week7/MagicNineBall/magic_nine_ball-0.0.3.gem new file mode 100644 index 0000000..346b768 Binary files /dev/null and b/week7/MagicNineBall/magic_nine_ball-0.0.3.gem differ diff --git a/week7/MagicNineBall/magic_nine_ball.gemspec b/week7/MagicNineBall/magic_nine_ball.gemspec new file mode 100644 index 0000000..dd69f4d --- /dev/null +++ b/week7/MagicNineBall/magic_nine_ball.gemspec @@ -0,0 +1,13 @@ + Gem::Specification.new do |s| + s.name = "magic_nine_ball" + s.version = "0.0.3" + s.date = '2013-11-10' + s.summary = "This gem predicts the future" + s.description = "Type in your question to get your fortune!" + s.authors = ["Thomas Osborn"] + s.email = 'trosborn@gmail.com' + s.homepage = 'http://rubygems.org/gems/magic_eight_ball' + s.files = ["lib/MagicNineBall.rb"] + s.licenses = ['MIT'] + s.executables << 'magic_nine_ball.rb' +end \ No newline at end of file diff --git a/week7/MagicNineBall/magic_nine_ball_spec.rb b/week7/MagicNineBall/magic_nine_ball_spec.rb new file mode 100644 index 0000000..2f467cb --- /dev/null +++ b/week7/MagicNineBall/magic_nine_ball_spec.rb @@ -0,0 +1,9 @@ +# magic_nine_ball_spec.rb +require 'magicnineball' + +describe MagicNineBall do + it "makes sure magicnineball is not nil" do + @magicnineball = MagicNineBall.new + @magicnineball.nil? + end +end \ No newline at end of file diff --git a/week7/class_materials/cucumber/features/step_definitions/converter.rb b/week7/class_materials/cucumber/features/step_definitions/converter.rb index e37c629..bc824cf 100644 --- a/week7/class_materials/cucumber/features/step_definitions/converter.rb +++ b/week7/class_materials/cucumber/features/step_definitions/converter.rb @@ -4,15 +4,15 @@ def initialize(unit) @unit = unit.to_f end - def type=(type) - @type = type + def type=(scale) + @scale = scale end def convert - self.send("#{@type}_convertion") + self.send("#{@scale}_conversion") end - def Celsius_convertion - (@unit * (9.0/5.0) + 32.0).round(1) + def Celsius_conversion + @unit * 9/5 + 32 end -end +end \ No newline at end of file diff --git a/week7/homework/features/piratetranslator.rb b/week7/homework/features/piratetranslator.rb new file mode 100644 index 0000000..0bf7f69 --- /dev/null +++ b/week7/homework/features/piratetranslator.rb @@ -0,0 +1,13 @@ +class PirateTranslator + + + @@pirate_words = {"Hello Friend" =>"Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!"} + + def say(p_string) + @p_string = p_string + end + + def translate + @@pirate_words[@p_string] + end +end \ No newline at end of file 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..3036816 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,135 @@ +class TicTacToe + + attr_accessor :board + attr_accessor :player_move + attr_accessor :player_symbol + attr_accessor :computer_symbol + attr_accessor :player + attr_accessor :current_player + attr_accessor :player_won + attr_accessor :computer_won + + SYMBOLS = [:X, :O] + + def initialize(current_player = :computer, player_symbol= :X, player = "") + @player_symbol = player_symbol + @current_player = current_player + @player = player + @board = { + "a1"=>" ","a2"=>" ","a3"=>" ", + "b1"=>" ","b2"=>" ","b3"=>" ", + "c1"=>" ","c2"=>" ","c3"=>" " + } + end + + def welcome_player + "Welcome #{@player}" + end + + def current_player + if (@current_player == :computer) + return "Computer" + else + @player + end + end + + def computer_symbol + if (@player_symbol == :O) + @player_symbol = :X + else + @computer_symbol = :O + end + end + + def indicate_palyer_turn + puts "#{@player}'s Move:" + end + + def computer_move + computer_symbol + move = open_spots.sample + @board[move] = @computer_symbol + move + @current_player = player + end + + def current_state + @board.values + end + + def open_spots + spots = [] + @board.each{|k, v| spots << k if v == " " } + spots + end + + def get_player_move + player_move = gets.chomp + end + + + def player_move + board_square = get_player_move + if @board[board_square] != " " + player_move + else + @board[board_square] = @player_symbol + end + @current_player = :computer + end + + def computer_won? + @computer_won + end + + def player_won? + @player_won + end + + def spots_open? + @board.has_value?(' ') + end + + def over? + if (@board["a1"]==@board["a2"] && @board["a2"]==@board["a3"] && @board["a1"]==:X ) || (@board["b1"]==@board["b2"] && @board["b2"]==@board["b3"] && @board["b1"]==:X ) || (@board["c1"]==@board["c2"] && @board["c2"]==@board["c3"] && @board["c1"]==:X ) || (@board["a1"]==@board["b1"] && @board["b1"]==@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["a2"]==@board["a3"] && @board["a1"]==:O ) || (@board["b1"]==@board["b2"] && @board["b2"]==@board["b3"] && @board["b1"]==:O ) || (@board["c1"]==@board["c2"] && @board["c2"]==@board["c3"] && @board["c1"]==:O ) || (@board["a1"]==@board["b1"] && @board["b1"]==@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 @board.empty? + @draw = true + @over = true + end + @over + end + + def draw? + draw = false + if over? && player_won? == false && computer_won? == false + draw = true + end + draw + end + + def current_state + <<-board + "#{@board["a1"]}""#{@board["a2"]}""#{@board["a3"]}" + "#{@board["b1"]}""#{@board["b2"]}""#{@board["b3"]}" + "#{@board["c1"]}""#{@board["c2"]}""#{@board["c3"]}" + board + end + + def determine_winner + over? + end +end \ No newline at end of file diff --git a/week7/homework/features/symbols.rb b/week7/homework/features/symbols.rb new file mode 100644 index 0000000..4d84be2 --- /dev/null +++ b/week7/homework/features/symbols.rb @@ -0,0 +1,4 @@ +module Symbols + def initialize + end +end \ No newline at end of file diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..9cc0152 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,18 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + +Method_missing is an error handler that allows for a more graceful way to handle a no method error. 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +Eigenclass is this weird invisible class that Singleton methods live in. 3. When would you use DuckTypeing? How would you use it to improve your code? + +Ducktyping helps to reduce the amount of code that you have to maintain when using code on large collections of objects. 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + +Class and instance methods can only be called on classes and instances, respectively. The evals are used to define those methods for their respective classes. 5. What is the difference between a singleton class and a singleton method? + +The singleton class is the eigenclass created when a singleton method is created. The singleton method is a method particular to a specific object. +