diff --git a/.gitignore b/.gitignore index e43b0f9..2e3dedd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .DS_Store +cookbooks +cookbook-reader.orig +cookbook-reader diff --git a/.ruby-version b/.ruby-version deleted file mode 100644 index c506e4e..0000000 --- a/.ruby-version +++ /dev/null @@ -1 +0,0 @@ -ruby-2.0.0-p353 diff --git a/.version b/.version new file mode 100644 index 0000000..9304515 --- /dev/null +++ b/.version @@ -0,0 +1 @@ +ruby-2.1.0 diff --git a/README.md b/README.md index b0c658e..af000aa 100644 --- a/README.md +++ b/README.md @@ -71,3 +71,4 @@ Gem Project Requirements [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/UWE-Ruby/rubywinter2014/trend.png)](https://bitdeli.com/free "Bitdeli Badge") + diff --git a/cookbook-reader/.gitignore b/cookbook-reader/.gitignore new file mode 100644 index 0000000..0f43d20 --- /dev/null +++ b/cookbook-reader/.gitignore @@ -0,0 +1,7 @@ +*.gem +.bundle +pkg/* +coverage +*.swp +.idea/* +.DS_Store diff --git a/cookbook-reader/.ruby-gemset b/cookbook-reader/.ruby-gemset new file mode 100644 index 0000000..1dfef02 --- /dev/null +++ b/cookbook-reader/.ruby-gemset @@ -0,0 +1 @@ +cookbook-reader diff --git a/cookbook-reader/.ruby-version b/cookbook-reader/.ruby-version new file mode 100644 index 0000000..7ec1d6d --- /dev/null +++ b/cookbook-reader/.ruby-version @@ -0,0 +1 @@ +2.1.0 diff --git a/cookbook-reader/.travis.yml b/cookbook-reader/.travis.yml new file mode 100644 index 0000000..938d4ef --- /dev/null +++ b/cookbook-reader/.travis.yml @@ -0,0 +1,3 @@ +rvm: + - 2.1.0 +script: "rake spec" diff --git a/cookbook-reader/Gemfile b/cookbook-reader/Gemfile new file mode 100644 index 0000000..07177f9 --- /dev/null +++ b/cookbook-reader/Gemfile @@ -0,0 +1,3 @@ +source "http://rubygems.org" +gem 'rspec', :require => 'spec' +gemspec diff --git a/cookbook-reader/README.md b/cookbook-reader/README.md new file mode 100644 index 0000000..2936989 --- /dev/null +++ b/cookbook-reader/README.md @@ -0,0 +1,20 @@ +[![Build Status](https://secure.travis-ci.org/keviny22/cookbook-reader.png)](http://travis-ci.org/keviny22/cookbook-reader) +[![Gem Version](https://badge.fury.io/rb/cookbook-reader.png)](http://badge.fury.io/rb/cookbook-reader) +[![Code Climate](https://codeclimate.com/github/keviny22/cookbook-reader.png)](https://codeclimate.com/github/keviny22/cookbook-reader) + + +cookbook-reader +=============== + +A gem for reading a cookbooks metadata, created for learning Ruby. +Currently displays all of the dependancies of all cookbooks in the path. + +Installation: + +`gem install cookbook-reader` + +Usage: + +cookbook-reader --path {path to cookbook directory} + + diff --git a/cookbook-reader/Rakefile b/cookbook-reader/Rakefile new file mode 100644 index 0000000..c557124 --- /dev/null +++ b/cookbook-reader/Rakefile @@ -0,0 +1,7 @@ +require 'rspec/core/rake_task' + +desc 'Run specs' + +RSpec::Core::RakeTask.new do |t| + t.rspec_opts = %w(--color) +end diff --git a/cookbook-reader/bin/cookbook-reader b/cookbook-reader/bin/cookbook-reader new file mode 100755 index 0000000..fbca8a3 --- /dev/null +++ b/cookbook-reader/bin/cookbook-reader @@ -0,0 +1,5 @@ +#! /usr/bin/ruby +require 'cookbook-reader/parser' +parser = CookbookReader::Parser.new(ARGV) +parser.parse +parser.display_data diff --git a/cookbook-reader/cookbook-reader.gemspec b/cookbook-reader/cookbook-reader.gemspec new file mode 100644 index 0000000..eaeeab9 --- /dev/null +++ b/cookbook-reader/cookbook-reader.gemspec @@ -0,0 +1,23 @@ +Gem::Specification.new do |s| + s.name = 'cookbook-reader' + s.version = '0.1.2' + s.date = '2014-03-12' + s.summary = "cookbook version parser" + s.description = "A gem that parses cookbook metadata" + s.authors = ["Kevin Young"] + s.email = 'kevin@young.net' + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] + + s.homepage = 'https://github.com/keviny22/cookbook-reader' + s.license = 'MIT' + + s.add_development_dependency "fakefs", "~> 0.4.2" + s.add_development_dependency "rake" + s.add_development_dependency "rspec", "~> 2.13.0" + s.add_development_dependency "simplecov", "~> 0.7.1" + s.add_development_dependency "timecop", "~> 0.6.1" +end diff --git a/cookbook-reader/lib/cookbook-reader/options.rb b/cookbook-reader/lib/cookbook-reader/options.rb new file mode 100644 index 0000000..cf26748 --- /dev/null +++ b/cookbook-reader/lib/cookbook-reader/options.rb @@ -0,0 +1,37 @@ +require 'optparse' + +module CookbookReader + class Options + + attr_reader :path_to_search + + def initialize(argv) + parse(argv) + end + + private + + def parse(argv) + OptionParser.new do |opts| + opts.banner = "Usage: cookbook-reader --path path ..." + + opts.on("-p", "--path path", String, "Path to search") do |path| + @path_to_search = path + end + + opts.on("-h", "--help", "Show this message") do + puts opts + exit + end + + begin + argv = ["-h"] if argv.empty? + opts.parse!(argv) + rescue OptionParser::ParseError => e + STDERR.puts e.message, "\n", opts + exit(-1) + end + end + end + end +end diff --git a/cookbook-reader/lib/cookbook-reader/parser.rb b/cookbook-reader/lib/cookbook-reader/parser.rb new file mode 100644 index 0000000..300643d --- /dev/null +++ b/cookbook-reader/lib/cookbook-reader/parser.rb @@ -0,0 +1,71 @@ +require_relative 'options' +module CookbookReader + class Parser + def initialize(argv) + @options = Options.new(argv) + @top_list = [] + end + + def parse + @metadata_list = Array.new + Dir.glob("#{@options.path_to_search}/**/metadata.rb") do |filename| + @metadata_list.push read_file filename + end + end + + def display_data + @top_list.each do |element| + puts "Cookbook: #{element['name']}\n" + puts " Dependancies:" + if element['depends'] + element['depends'].each do |name, version| + puts " #{name}, #{version}\n" + end + else + puts " None\n" + end + puts "\n" + end + end + + private + def read_file(filename) + @hash = {} + + @depends_list = {} + File.open(filename) do |fp| + fp.each do |line| + key, value = line.chomp.split(" ") + next if key.nil? || value.nil? + key.strip! unless key.nil? + value.strip! unless value.nil? + key = key.chomp('"').reverse.chomp('"').reverse + value = value.chomp('"').reverse.chomp('"').reverse + + if key =~ /depends/ + @hash[key] = add_dependencies(line) + else + @hash[key] = value + end + end + end + @top_list.push @hash + + end + + def add_dependencies(line) + line.slice! "depends " + k, v = line.chomp.split(",") + v = "none" if v.nil? + k.strip! + v.strip! + v = v.chomp('"').reverse.chomp('"').reverse + v = v.chomp('"').reverse.chomp('"').reverse + k = k.chomp('"').reverse.chomp('"').reverse + k.strip! + v.strip! + @depends_list[k] = v + @depends_list + end + end +end diff --git a/cookbook-reader/spec/options_spec.rb b/cookbook-reader/spec/options_spec.rb new file mode 100644 index 0000000..fd46607 --- /dev/null +++ b/cookbook-reader/spec/options_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +module CookbookReader + describe Options do + + it "should call the given sub command" do + pending("I hate rpsec") + end + + it "should display help when passed --help" do + pending("I hate rpsec") + end + + it "should display help when passed nothing" do + pending("I hate rpsec") + end + + end +end diff --git a/cookbook-reader/spec/parser_spec.rb b/cookbook-reader/spec/parser_spec.rb new file mode 100644 index 0000000..fab5cbc --- /dev/null +++ b/cookbook-reader/spec/parser_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +module CookbookReader + describe Parser do + + it "should display the data" do + pending("I hate rpsec") + end + + it "should parse the data" do + pending("I hate rpsec") + end + + end +end \ No newline at end of file diff --git a/cookbook-reader/spec/spec_helper.rb b/cookbook-reader/spec/spec_helper.rb new file mode 100644 index 0000000..757b400 --- /dev/null +++ b/cookbook-reader/spec/spec_helper.rb @@ -0,0 +1,12 @@ + +require 'rubygems' +require 'bundler/setup' +require 'fakefs/safe' + + +require_relative '../lib/cookbook-reader/options' +require_relative '../lib/cookbook-reader/parser' + +['contexts'].each do |dir| + Dir[File.expand_path(File.join(File.dirname(__FILE__),dir,'*.rb'))].each {|f| require f} +end diff --git a/midterm/answers.txt b/midterm/answers.txt new file mode 100644 index 0000000..46f9e32 --- /dev/null +++ b/midterm/answers.txt @@ -0,0 +1,71 @@ + - What are the three uses of the curly brackets {} in Ruby? +1. For string interpolation, ex + irb(main):001:0> name="fred" + => "fred" + irb(main):002:0> puts "Hello #{name}" + Hello fred + => nil + irb(main):003:0> + +2. They are the literal constructor for constructing a hash, ex. + irb(main):003:0> hash={"ford" => "mustang","dodge" => "charger"} + => {"ford"=>"mustang", "dodge"=>"charger"} + irb(main):004:0> puts hash + {"ford"=>"mustang", "dodge"=>"charger"} + => nil + irb(main):005:0> + +3. You can put a code block in curly braces to deliminate instead of using do/end + irb(main):006:0> array = [1,10,100] + => [1, 10, 100] + irb(main):008:0> puts array.map{|n|n+1} + 2 + 11 + 101 + => nil + irb(main):009:0> + + - What is a regular expression and what is a common use for them? + Regular Expressions can be used in Ruby to do pattern matching, substitution, pretty much any type of string manipulation. + + - What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby? + A symbol is a string that is stored as unique identifiers in memeory,meaning it is only stored once. + A string on the other hand can be changed so it is stored many times, making look ups take longer + A FixNum is stored as the value itself, in ruby 2.0 a float behaves like a FixNum + + - Are these two statements equivalent? Why or Why Not? + 1. x, y = "hello", "hello" + 2. x = y = "hello" + + Yes, in the sense that x = "hello" and y = "hello" in both cases. + +- What is the difference between a Range and an Array? + A range is sequential: a,b,c,d 1,2,3,4 etc + An array is a collection a,1,b,4,car,baby + +- Why would I use a Hash instead of an Array? + if you care about the relationship between a key and value a hash should be used. + if you just want to store a collection of things, an array should be used. + +- What is your favorite thing about Ruby so far? + It isn't strict on syntax, you dont have to declare everything + +- What is your least favorite thing about Ruby so far? + So many ways to do something, it turns into an artform especially when working in groups. it makes it more important to agree style. + + Programming Problems (10pts each): + - Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber. + - The EvenNumber class should: + - Only allow even numbers + - Get the next even number + - Compare even numbers + - Generate a range of even numbers +- Make the rspec tests in wish_list_spec.rb pass by writing a WishList class + - The WishList class should: + - Mixin Enumerable + - Define each so it returns wishes as strings with their index as part of the string + +Mid-Term Spec (50pts): +- Make the tests pass. + + diff --git a/midterm/even_number.rb b/midterm/even_number.rb new file mode 100644 index 0000000..441bcc7 --- /dev/null +++ b/midterm/even_number.rb @@ -0,0 +1,23 @@ +class EvenNumber + + def next(num) + return false unless num.even? + num + 2 + end + + def compare(num1,num2) + return false unless num1.even? + return false unless num2.even? + true if num1 == num2 + end + + def range(num1,num2) + return false unless num1.even? + return false unless num2.even? + (num1..num2).step(2) do |n| + n + end + + end + +end diff --git a/midterm/even_number_spec.rb b/midterm/even_number_spec.rb new file mode 100644 index 0000000..ca9fe17 --- /dev/null +++ b/midterm/even_number_spec.rb @@ -0,0 +1,35 @@ +# - Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber. +# - The EvenNumber class should: +# - Only allow even numbers +# - Get the next even number +# - Compare even numbers +# - Generate a range of even numbers + +require "#{File.dirname(__FILE__)}/even_number" + +describe EvenNumber do + before do + @evennumber = EvenNumber.new + end + + it "should only allow even numbers" do + @evennumber.next(3).should be_false + end + + it "should return the next even number" do + @evennumber.next(2).should equal 4 + end + + it "should compare even numbers" do + @evennumber.compare(2,2).should be_true + end + + it "should not return true of numbers are not the same" do + @evennumber.compare(2,4).should_not be_true + end + + it "should generate a range of even numbers" do + @evennumber.range(2,8).should be_a Range + end + +end diff --git a/midterm/thanksgiving_dinner.rb b/midterm/thanksgiving_dinner.rb new file mode 100644 index 0000000..294f695 --- /dev/null +++ b/midterm/thanksgiving_dinner.rb @@ -0,0 +1,34 @@ +module Dinner;end +class ThanksgivingDinner + include Dinner + attr_accessor :menu, :guests + + def initialize(vegan) + @vegan = vegan + end + + def guests + end + + def seating_chart_size + 45 + end + + def menu + { :diet => :vegan, + :proteins => ["Tofurkey", "Hummus"], + :veggies => [:ginger_carrots , :potatoes, :yams], + :desserts => {:pies=>[:pumkin_pie], :other=>["Chocolate Moose"], :molds=>[:cranberry, :mango, :cherry]} + } + + end + + def whats_for_dinner + "Tonight we have proteins Tofurkey and Hummus, and veggies Ginger Carrots, Potatoes, and Yams." + end + + def whats_for_dessert + "Tonight we have 5 delicious desserts: Pumkin Pie, Chocolate Moose, and 3 molds: Cranberry and Mango and Cherry." + end + +end diff --git a/midterm/turkey.rb b/midterm/turkey.rb new file mode 100644 index 0000000..8fff844 --- /dev/null +++ b/midterm/turkey.rb @@ -0,0 +1,18 @@ +module Animal;end + class Turkey + include Animal + + def initialize(weight) + @weight = weight + end + + def weight + @weight + end + + def gobble_speak(message) + if message == "Hello I Am a Turkey. Please Don't Eat Me." + "Gobble Gobble Gobble gobble Gobble. Gobble Gobb'le Gobble Gobble." + end + end + end diff --git a/week1/exercises/roster.txt b/week1/exercises/roster.txt index 21b0964..3a58940 100644 --- a/week1/exercises/roster.txt +++ b/week1/exercises/roster.txt @@ -18,5 +18,5 @@ 16, Robert McCreary Mannino, rmccreary8@gmail.com, psytau, maalaoshi, rmccreary8@gmail.com 17, Rukia, rukk86@gmail.com, rukk86, no twitter, Rukia 18, -19, +19, Kevin Young, kevin@young.net, keviny22, null, KevinYoung 20, Zack Walkingstick, zackwalkingstick@gmail.com, zackwalkingstick, no twitter, @zack diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..c110442 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,60 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +In Ruby, objects are instances of the class which can be created using the +special method constructor called new. 2. What is a variable? +In Ruby, a variable is a name that is associated with a particular object; a +reference to an object +There are four types of variables, global, instance, local and constant 3. What is the difference between an object and a class? +Some may say they are the same thing, on object is just an instance of a +class. 4. What is a String? +Verbatim, “Ruby strings are simply sequences of characters. They normally hold +printable characters, but that is not a requirement; a string can also hold +binary data. Strings are objects of class String.“ -5. What are three messages that I can send to a string object? Hint: think methods +5. What are three messages that I can send to a string object? Hint: think +methods +You could use chop, delete, empty + +2.1.0 :010 > car="Ford F-150" + => "Ford F-150" +2.1.0 :011 > puts car.chop +Ford F-15 + => nil +2.1.0 :012 > +2.1.0 :013 > puts car.delete "150" +Ford F- + => nil +2.1.0 :014 > +2.1.0 :014 > puts car.empty? +false + => nil + + +6. What are two ways of defining a String literal? Bonus: What is the +difference between them? + +%q (single-quoted string) and %Q (double-quoted string) + +single-quoted string literals undergo the least substitution of the two, for +example. + + +2.1.0 :017 > car="ford" + => "ford" +2.1.0 :018 > model="f-150" + => "f-150" +2.1.0 :021 > puts %q{I drive an #{car}, #{model}} +I drive an #{car}, #{model} + => nil +2.1.0 :022 > puts %Q{I drive an #{car}, #{model}} +I drive an ford, f-150 + => nil +2.1.0 :023 > -6. What are two ways of defining a String literal? Bonus: What is the difference between them? diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..6e2965f 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -8,19 +8,22 @@ # (Hint: You should do the reading on Strings first) describe String do - context "When a string is defined" do - 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 - 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"))' - end - end + context "When a string is defined" do + 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" do + @my_string.should have(66).characters + end + + it "should be able to split on the . charater" do + @my_string.split('.').should have(2).items + end + + it "should be able to give the encoding of the string" do + @my_string.encoding.should eq (Encoding.find("UTF-8")) + end + end end diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..343b96d 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,33 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +A hash uses a key value pair, which is benifitial if you need to do lookups on +either. usually used when you have a one to one relationship, ex. +FirstName=Joe +LastName=Smith + +With an array, its is just a listing where each element in the array is +associated by an numerical index. This is useful if you have a listing of the +same times of items, ex. +Kids=sally,bobby,fred 2. When would you use an Array over a Hash and vice versa? +I use a hash when i would need to lookup a value based on its key, or vice +versa. 3. What is a module? Enumerable is a built in Ruby module, what is it? +a way of grouping together methods, classes, and constants +an enumerable is a standard mixin, which combines a bunch of methods, i like +the map enumerable which makes running blocks of code much cleaner 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +use mixins 5. What is the difference between a Module and a Class? +ruby-doc.org says it best +Every class is also a module, but unlike modules a class may not be mixed-in +to another module (or class). Like a module, a class can be used as a +namespace. A class also inherits methods and constants from its superclass. +However, you can put a mix-in a module but not a class + + diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..6677d03 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,23 @@ +module SimonSays + def echo(statement) + statement + end + + def shout(statement) + statement.upcase + end + + def repeat(statement, amount = 2) + ([statement] * amount).join(" ") + end + + def start_of_word(statement, number) + statement[0, number] + end + + def first_word(statement) + a = statement.split + a[0] + end +end + diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..4d9e891 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,22 @@ +class Calculator + def sum(numbers) + return 0 unless numbers.any? + numbers.inject{|sum,x| sum + x} + end + + def multiply(*numbers) + return 0 unless numbers.any? + numbers.flatten.inject{|multiply,x| multiply * x} + end + + def pow(*numbers) + return 0 unless numbers.any? + numbers.inject{|pow,x| pow ** x} + end + + def fac(number) + return 1 if number == 1 + return 1 if number == 0 + number * fac(number-1) + end +end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..f8f651d 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,22 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +A Ruby symbol is an identifier corresponding to a string of characters, often a +name. 2. What is the difference between a symbol and a string? +Mostly that a symbol is immutable, 3. What is a block and how do I call a block? +a block is a chunk of code with a begining or an end. you can call a block +using do ... end or with curly braces 4. How do I pass a block to a method? What is the method signature? +use yield to pass a block to a method. +A method signature tells you how to use/call that method 5. Where would you use regular expressions? +As little as possible :) +Generally when you are trying to match patterns then specific text. Like if +you are trying to detect if something fits the pattern off a social security +number or ip address, etc... diff --git a/week4/class_materials/odd_number_kevin.rb b/week4/class_materials/odd_number_kevin.rb new file mode 100644 index 0000000..b3e6998 --- /dev/null +++ b/week4/class_materials/odd_number_kevin.rb @@ -0,0 +1,14 @@ +class OddNumberKevin + def initialize value + @value = value + end + + def succ + return OddNumberKevin.new(@value + 2) if @value.odd? + OddNumberKevin.new(@value + 1) + end + + def <=> other + @value + end +end \ No newline at end of file diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index 187b3d3..36e4824 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,12 +3,18 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +Many lines are read at one time into a buffer 2. How would you output "Hello World!" to a file called my_output.txt? +File.open("my_output.txt", 'w') { |file| file.write("Hello World!") } 3. What is the Directory class and what is it used for? +The Dir class is used for listing directories and its contents, traversing +through directories and creating/deleting directories 4. What is an IO object? +An IO object is something returned from calling the IO class. 5. What is rake and what is it used for? What is a rake task? - +rake is best used for automating tasks, a rake task a specific action defined +in the rake file diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..8f67700 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,6 @@ +class Worker + def self.work n = 1 + n.times.inject(nil){yield} + end +end + diff --git a/week6/homework/hola_keviny22/hola-0.0.0.gem b/week6/homework/hola_keviny22/hola-0.0.0.gem new file mode 100644 index 0000000..5803344 Binary files /dev/null and b/week6/homework/hola_keviny22/hola-0.0.0.gem differ diff --git a/week6/homework/hola_keviny22/hola-0.1.0.gem b/week6/homework/hola_keviny22/hola-0.1.0.gem new file mode 100644 index 0000000..0aadb8e Binary files /dev/null and b/week6/homework/hola_keviny22/hola-0.1.0.gem differ diff --git a/week6/homework/hola_keviny22/hola.gemspec b/week6/homework/hola_keviny22/hola.gemspec new file mode 100644 index 0000000..605a448 --- /dev/null +++ b/week6/homework/hola_keviny22/hola.gemspec @@ -0,0 +1,13 @@ +Gem::Specification.new do |s| + s.name = 'hola_keviny22' + s.version = '0.1.0' + s.date = '2014-02-20' + s.summary = "Hola!" + s.description = "A simple hello world gem" + s.authors = ["Kevin Young"] + s.email = 'kevin@young.net' + s.files = ["lib/hola.rb"] + s.homepage = + 'http://rubygems.org/gems/hola_keviny22' + s.license = 'MIT' +end diff --git a/week6/homework/hola_keviny22/hola_keviny22-0.1.0.gem b/week6/homework/hola_keviny22/hola_keviny22-0.1.0.gem new file mode 100644 index 0000000..43e23bf Binary files /dev/null and b/week6/homework/hola_keviny22/hola_keviny22-0.1.0.gem differ diff --git a/week6/homework/hola_keviny22/lib/hola.rb b/week6/homework/hola_keviny22/lib/hola.rb new file mode 100644 index 0000000..8289027 --- /dev/null +++ b/week6/homework/hola_keviny22/lib/hola.rb @@ -0,0 +1,5 @@ +class Hola + def self.hi + puts "Hello world!" + end +end diff --git a/week7/exercises/hello/features/greeter_says_hello.feature b/week7/exercises/hello/features/greeter_says_hello.feature new file mode 100644 index 0000000..8db8862 --- /dev/null +++ b/week7/exercises/hello/features/greeter_says_hello.feature @@ -0,0 +1,10 @@ +Feature: greater says hello + +In order to start learning RSpec and Cucumber +As a reader of the Rspec book +I want a greater to say hello + + Scenario: greeter says hello + Given a greeter + When I send it the greet message + Then I should see "Hello Cucumber!" diff --git a/week7/exercises/hello/features/step_definitions/greeter_steps.rb b/week7/exercises/hello/features/step_definitions/greeter_steps.rb new file mode 100644 index 0000000..fe7700e --- /dev/null +++ b/week7/exercises/hello/features/step_definitions/greeter_steps.rb @@ -0,0 +1,17 @@ +class CucumberGreeter + def greet + "Hello Cucumber!" + end +end + +Given /^a greeter$/ do + @greeter = CucumberGreeter.new +end + +When /^I send it the greet message$/ do + @message = @greeter.greet +end + +Then /^I should see "([^"]*)"$/ do |greeting| + @message.should == greeting +end diff --git a/week7/exercises/hello/spec/greeter_spec.rb b/week7/exercises/hello/spec/greeter_spec.rb new file mode 100644 index 0000000..a4eb1f1 --- /dev/null +++ b/week7/exercises/hello/spec/greeter_spec.rb @@ -0,0 +1,14 @@ +class RSpecGreeter + def greet + "Hello RSpec!" + end +end + +describe "RSpec Greeter" do + it "should say 'Hello RSpec!' when it receives the greet() message" do + greeter = RSpecGreeter.new + greeting = greeter.greet + greeting.should == "Hello RSpec!" + end +end + diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..424db3b --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,10 @@ +class PirateTranslator + def say(str) + @reply = "Ahoy Matey" + end + + def translate + @reply + "\n Shiber Me Timbers You Scurvey Dogs!!" + + end +end diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index a3287c1..b404f38 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -100,8 +100,8 @@ :A1 => :X, :A2 => :O, :A3 => :X, :B1 => :X, :B2 => :O, :B3 => :X, :C1 => :O, :C2 => :X, :C3 => :O - } - @game.determine_winner + } + @game.determine_winner end When /^there are no open spaces left on the board$/ do @@ -121,4 +121,4 @@ @game.board[arg1.to_sym] = ' ' @game.should_receive(:get_player_move).twice.and_return(@taken_spot, arg1) @game.player_move.should eq arg1.to_sym -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..07237cd --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,131 @@ +class TicTacToe + + SYMBOLS = [:X,:O] + + + attr_accessor :player, :board + def initialize current_player=nil, player_s=nil + @current_player = current_player || [:player, :computer].sample + choose_symbols(player_s) + + @board = { + :A1=>" ",:A2=>" ",:A3=>" ", + :B1=>" ",:B2=>" ",:B3=>" ", + :C1=>" ",:C2=>" ",:C3=>" " + } + end + + + def welcome_player + "Welcome #{@player}" + end + + def current_player + {:computer => "Computer", :player => @player}[@current_player] + end + + def over? + false if spots_open? + true if player_won? || computer_won? + end + + def computer_move + + spot = open_spots.sample + @board[spot] = computer_symbol + @current_player = :player + end + + def indicate_palyer_turn + puts "#{@player}'s Move:" + end + + def player_move + spot = get_player_move.to_sym + until open_spots.include?(spot) + puts "That spot is taken, please enter another spot:" + spot = get_player_move.to_sym + end + @board[spot] = player_symbol + + @current_player = :computer + spot + end + + def current_state + ret = " 1 | 2 | 3\n" + ret << "A | #{@board[:A1]}| #{@board[:A2]}| #{@board[:A3]}\n" + ret << "---|---|---|---\n" + ret << "B | #{@board[:B1]}| #{@board[:B2]}| #{@board[:B3]}\n" + ret << "---|---|---|---\n" + ret << "C | #{@board[:C1]}| #{@board[:C2]}| #{@board[:C3]}\n" + + end + + def determine_winner + #return false if spots_open? + + if @board[:A1] == @board[:B1] && @board[:B1] == @board[:C1] && @board[:A1] != " " + @winner = @board[:A1] + elsif @board[:A2] == @board[:B2] && @board[:B2] == @board[:C2] && @board[:A2] != " " + @winner = @board[:A2] + elsif @board[:A3] == @board[:B3] && @board[:B3] == @board[:C3] && @board[:A3] != " " + @winner = @board[:A3] + elsif @board[:A1] == @board[:A2] && @board[:A2] == @board[:A3] && @board[:A1] != " " + @winner = @board[:A1] + elsif @board[:B1] == @board[:B2] && @board[:B2] == @board[:B3] && @board[:B1] != " " + @winner = @board[:B1] + elsif @board[:C1] == @board[:C2] && @board[:C2] == @board[:C3] && @board[:C1] != " " + @winner = @board[:C1] + #else + # @winner = "draw" + end + #return false if spots_open? + over? + true + end + + def player_won? + true if @winner == @player_symbol[:player] + end + + def draw? + true unless player_won? || computer_won? + end + + def computer_won? + @winner == @player_symbol[:computer] + end + + def player_symbol + @player_symbol[:player] + end + + def computer_symbol + @player_symbol[:computer] + end + + def get_player_move + gets.chomp + end + + def spots_open? + true if @board.values.any?{|v| v == " "} + end + + def open_spots + spots = Array.new + @board.each do |key, value| + if value == " " + spots << key + end + end + spots + end + + def choose_symbols(player_s) + player_s ||=SYMBOLS.sample + @player_symbol = {:computer => SYMBOLS.reject{|s| s==player_s}.first, :player => player_s} + end + +end diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..1445b22 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -1,9 +1,15 @@ - Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? +This missing_method is a built in Ruby hook method that is called when a method cannot be found. It raises an exception eith NoMethodError or NameError. +The missing_method can be overridden in your own class by writing your own missing_method in that class with your own behavior defined in that method. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +An eigenclass (aka singleton class, aka anonymous class, aka. metaclass) is defined as 'an exclusive stash of methods, tailormade for that object and not shared with other objects' and can be used to define a class method. +REF: The Well Grounded Rubyist, by David A. Black + +A singletome method (aks class method) lives in the singleton class 3. When would you use DuckTypeing? How would you use it to improve your code? 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? 5. What is the difference between a singleton class and a singleton method?