diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..77e2497 Binary files /dev/null and b/.DS_Store differ diff --git a/.rvmrc b/.rvmrc deleted file mode 100644 index 14c561b..0000000 --- a/.rvmrc +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env bash - -# This is an RVM Project .rvmrc file, used to automatically load the ruby -# development environment upon cd'ing into the directory - -# First we specify our desired [@], the @gemset name is optional, -# Only full ruby name is supported here, for short names use: -# echo "rvm use 2.0.0" > .rvmrc -environment_id="ruby-2.0.0-p247@RubyFall2013" - -# Uncomment the following lines if you want to verify rvm version per project -# rvmrc_rvm_version="1.22.11 (stable)" # 1.10.1 seems like a safe start -# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || { -# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading." -# return 1 -# } - -# First we attempt to load the desired environment directly from the environment -# file. This is very fast and efficient compared to running through the entire -# CLI and selector. If you want feedback on which environment was used then -# insert the word 'use' after --create as this triggers verbose mode. -if [[ -d "${rvm_path:-$HOME/.rvm}/environments" - && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]] -then - \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id" - for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"* - do - if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]] - then \. "${__hook}" || true - fi - done - unset __hook - if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced - then - if [[ $- == *i* ]] # check for interactive shells - then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null) -" # show the user the ruby and gemset they are using in green - else printf "%b" "Using: $GEM_HOME -" # don't use colors in non-interactive shells - fi - fi -else - # If the environment file has not yet been created, use the RVM CLI to select. - rvm --create "$environment_id" || { - echo "Failed to create RVM environment '${environment_id}'." - return 1 - } -fi diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0b681fb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: ruby +rvm: + - "1.8.7" + - "1.9.2" + - "1.9.3" + - jruby-18mode # JRuby in 1.8 mode + - jruby-19mode # JRuby in 1.9 mode + - rbx-18mode +# uncomment this line if your project needs to run something other than `rake`: +# script: bundle exec rspec spec diff --git a/README.md b/README.md index 4d1f3c3..5647b3e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +<<<<<<< HEAD RubyFall2013 ============ @@ -57,7 +58,16 @@ Week 10 * Final due! * Project due! * Interesting Stuff +<<<<<<< HEAD +======= +jonashaskins_midterm +==================== + +keeps my midter +>>>>>>> a909850fc83ae544cc25011759e2c6217f254bb5 +======= [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/UWE-Ruby/rubyfall2013/trend.png)](https://bitdeli.com/free "Bitdeli Badge") +>>>>>>> 8b3b197baa14a59c207aca832acf7ce570e9e099 diff --git a/midterm/instructions_and_questions.txt b/instructions_and_questions.txt similarity index 100% rename from midterm/instructions_and_questions.txt rename to instructions_and_questions.txt diff --git a/midterm/mid_term_spec.rb b/mid_term_spec.rb similarity index 100% rename from midterm/mid_term_spec.rb rename to mid_term_spec.rb diff --git a/test b/test new file mode 100644 index 0000000..e69de29 diff --git a/week1/.DS_Store b/week1/.DS_Store new file mode 100644 index 0000000..b5c6050 Binary files /dev/null and b/week1/.DS_Store differ diff --git a/week1/class_materials/name_printer_spec.rb b/week1/class_materials/name_printer_spec.rb new file mode 100644 index 0000000..4598c16 --- /dev/null +++ b/week1/class_materials/name_printer_spec.rb @@ -0,0 +1,8 @@ +describe "NamePrinter Application" do + + context "When Somethings Happens When I'm Logged In" do + it "should say Something" do + "Something".should eq "Something" + end + end +end diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index 1e0a8ef..13103e1 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -43,11 +43,14 @@ # When this example fails, # it will show "expected" as 2, and "actual" as 1 - 1.should eq 2 + 1.should eq 1 end - - it "supports placeholder examples that lack code (like this one)" + + # jhjh + it "supports placeholder examples that lack code (like this one)" do + 1.should eq 1 + end it "requires that examples use expectations (like #should) to work properly" do @@ -71,21 +74,32 @@ end end + + ## JHJH added class tests + context "When Somethings Happens When I'm Logged In" do + it "should say Something" do + "Something".should eq "Something" + end + end + context "Examples for in-class test exploration" do it "should know order of operations" do # Fix the Failing Test # Order of Operations is Please Excuse My Dear Aunt Sally: # Parentheses, Exponents, Multiplication, Division, Addition, Subtraction - (1+2-5*6/2).should eq -13 + (1+2-5*6/2).should eq -12 end - it "should count the characters in your name" do - pending + + context "Count my name" do + it "should count the characters in your name" do + "Jonas".should have(5).characters + end end - it "should check basic math" + it "should check basic math" - it "should check basic spelling" + it "should check basic spelling" end diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..590ed60 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -1,15 +1,47 @@ Please read: Chapter 3 Classes, Objects, and Variables + p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +a instance of a class. 2. What is a variable? +a reference to a object 3. What is the difference between an object and a class? +a class is a combination of state and methods. an object is a instance of that class ( class instance ). + 4. What is a String? +A sequence of characters. 5. What are three messages that I can send to a string object? Hint: think methods +chomp, count, delete, empty? , length, upcase, downcase, split, squeeze! ... woah there are alot. + 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? + +single or double quotation marks. + +single quotes 'foo', foo becomes the value. +For Double quotes, "foo\nbar" ruby looks for substitutions, interpolation "#{foo}" and escape sequences "Foo \nBar" + +also %q ( single quotes ), %Q ( double quotes ) + +%q/foo bar/ +%Q!oh no #{crap}! + +you can choose a delimiter. + +Finally here documents. + +string = << HERE + some kinda text + about something + i forgot what +HERE + + + +check them in and send a pull request !! diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..ad7f623 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,14 +12,35 @@ 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 count the charaters" do + result = @my_string.count @my_string + result.should eq 66 + #puts "Count is #{result}" + + # These work to + # white space are characters to + # size = @my_string.size + # size.should eq 66 + # puts "Size is #{size}" + + # delete the white spaces ! + # @new_string = @my_string.delete(' ') + # size = @new_string.size + # size.should eq 55 + # puts "Size is #{size}" + + end + it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + 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"))' + #pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + "#{@my_string.encoding}".should eq "UTF-8" + #puts "Encoding of #{@my_string.inspect} is #{@my_string.encoding}" + + end end end diff --git a/week1/homework/strings_and_rspec_spec_org.rb b/week1/homework/strings_and_rspec_spec_org.rb new file mode 100644 index 0000000..ea79e4c --- /dev/null +++ b/week1/homework/strings_and_rspec_spec_org.rb @@ -0,0 +1,26 @@ +# encoding: utf-8 + +# Please make these examples all pass +# You will need to change the 3 pending tests +# You will need to write a passing test for the first example +# (Hint: If you need help refer to the in-class exercises) +# The two tests with the pending keyword, require some ruby code to be written +# (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 +end + diff --git a/week2/count_frequency.rb b/week2/count_frequency.rb new file mode 100644 index 0000000..f28fb63 --- /dev/null +++ b/week2/count_frequency.rb @@ -0,0 +1,9 @@ +def count_frequency(word_list) + counts = Hash.new(0) + for word in word_list + counts[word] += 1 + end + counts +end + +p count_frequency(["sparky", "the", "cat", "sat", "on", "the", "mat"]) diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index a6b943d..786683e 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,4 +1,33 @@ class Book +<<<<<<< HEAD + attr_accessor :title + attr_reader :page_count + #attr_reader :title #reader only ? + @@book_count = 0 + + #self. = class level + def self.book.count + ## @@ = class level varable + @@book_count + end + + def initalize title = "Not Set", page_count = 0 + @@book_count += 1 + @page_count = page_count + @title = title + end + + def test + @test = "Hellow!" + end + + def out_put_test + puts @test + puts @@book_count + end + +end +======= attr_accessor :title attr_reader :page_count @@ -24,4 +53,5 @@ def out_put_test puts @@book_count end -end \ No newline at end of file +end +>>>>>>> 4706ad52545867a37a0e21a394c5e1103020fe67 diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index c3b1292..1772960 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -2,6 +2,32 @@ describe Book do +<<<<<<< HEAD + context "::book_count" do + + it "should count how many books have been created" do + Book.new + Book.new + Book.new + Book.book_count.should eq 3 + end +end + + + + it "should should set some defaults" do + @book.new + end + it "should allow me to set the title" do + @boot.title = "Harry Potter" + @book.title.should eq "Harry Potter" + + + it "should return the page count" do + @book.page_count.should eq "Page count is 200" + end +end +======= context "::book_count" do @@ -46,4 +72,5 @@ end -end \ No newline at end of file +end +>>>>>>> 4706ad52545867a37a0e21a394c5e1103020fe67 diff --git a/week2/exercises/mad_libs.rb b/week2/exercises/mad_libs.rb index 3af5583..ac5d9b1 100644 --- a/week2/exercises/mad_libs.rb +++ b/week2/exercises/mad_libs.rb @@ -1,10 +1,10 @@ -puts "Please enter a noun" +print "Please enter a noun : " noun = gets.chomp -puts "Please enter an adjective" +print "Please enter an adjective : " adjective = gets.chomp -puts "Please enter a past tense action verb" +print "Please enter a past tense action verb : " verb_past_tense = gets.chomp -puts "What does the #{noun} say?" +print "What does the #{noun} say? : " says = gets.chomp story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard and says #{says}" puts story diff --git a/week2/exercises/spec.rb b/week2/exercises/spec.rb new file mode 100644 index 0000000..a90c483 --- /dev/null +++ b/week2/exercises/spec.rb @@ -0,0 +1,3 @@ +describe Book do + + context diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..f95391d 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,36 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +An array is integer indexed collection of objects. + array_example[0] is the first object in the array. + +A hash is also a indexed collection of object references, except +in that it can be indexed with objects of any types : strings,symbols, ect.. integers also. +but arrays are just indexed by integers. + +my_hash = { 'black' => 'knight' } + +black (key) referes to knight ( value ) 2. When would you use an Array over a Hash and vice versa? -3. What is a module? Enumerable is a built in Ruby module, what is it? +I would use a hash for ordered items, and for efficent search/lookup +The key index will result in the value(s). Also to count frequencys. Using they +key and incrementing the value as the key was counted. + +An array would work great if I needed to store a collection of objects and the iterate +over it. If I needed a que or a stack i'd use a array. Or if i needed to split a string, +or read in variable input to a method. + +3. What is a module? +a way of grouping together methods, classes and constants. Modules define a namespace. + +Enumerable is a built in Ruby module, what is it? +Its a mixin. collection classes, traversal and searching methods and the ability to sort! 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +No, Ruby is a single inheritance language. The child can only inherit from the parent. +You can get around the problem by using mixins, which provide a controlled multiple-inheritance like capability! 5. What is the difference between a Module and a Class? +a module can not be instantiated and a class can. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..93cb8b7 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,38 @@ +module SimonSays + def echo(says) + @says = says + end + + def shout(shouts) + @shouts = shouts + @shouts.upcase + end + + def repeat(word, *times) + @word = word + @times = times[0] + if @times == nil + @times = 2 + end + @word = "#{@word} " * @times + @word.rstrip + end + + def start_of_word(word, number) + a = word.split(//) + number = number - 1 + b = a[0..number] + b.join + end + + def first_word(word) + a = word.split(/ /) + a[0] + end + +end + +# Question .. is it correct to use instance variables here as in for +# echo/shout/repeat methods or better ( more ruby?? ) to use +# as in start_or_word and first_word ?? + diff --git a/week2/tester.rb b/week2/tester.rb new file mode 100644 index 0000000..bb703aa --- /dev/null +++ b/week2/tester.rb @@ -0,0 +1,20 @@ +require_relative 'count_frequency' +require 'test/unit' + +class TestCountFrequency < Test::Unit::TestCase + def test_empty_list + assert_equal({}, count_frequency([])) + end + def test_single_word + assert_equal({"cat" => 1}, count_frequency(["cat"])) + end + def test_two_different_words + assert_equal({"cat" => 1, "sat" => 1}, count_frequency(["cat", "sat"])) + end + def test_two_words_with_adjacent_repeat + assert_equal({"cat" => 2, "sat" => 1}, count_frequency(["cat", "cat", "sat"])) + end + def test_two_words_with_non_adjacent_repeat + assert_equal({"cat" => 2, "sat" => 1}, count_frequency(["cat", "sat", "cat"])) + end +end diff --git a/week2/words_from_strings.rb b/week2/words_from_strings.rb new file mode 100644 index 0000000..e48cfcb --- /dev/null +++ b/week2/words_from_strings.rb @@ -0,0 +1,5 @@ +def words_from_string(string) + string.downcase.scan(/[\w']+/) +end + +p words_from_string("But I didn't inhale, he said (emphatically)") diff --git a/week3/exercises/answers.jh b/week3/exercises/answers.jh new file mode 100644 index 0000000..4425c1a --- /dev/null +++ b/week3/exercises/answers.jh @@ -0,0 +1,27 @@ +1.9.3-p392 :001 > load 'monsters.rb' + => true +1.9.3-p392 :002 > $monsters.count{|m| m[:nocturnal]} + => 2 +1.9.3-p392 :005 > $monsters.select{|m| m[:nocturnal]}.map{|m| m[:name]} + => ["Vampire", "Werewolf"] + +1.9.3-p392 :007 > $monsters.select{|m| m[:nocturnal]}.map{|m| m[:name]} + => ["Vampire", "Werewolf"] + +1.9.3-p392 :009 > legs = 0 + => 0 + +1.9.3-p392 :011 > $monsters.each{|m| legs += m[:legs]} + => [{:name=>"Zombie", :nocturnal=>false, :dangers=>["bites", "scratches"], :vulnerabilities=>["fire", "decapitation"], :legs=>2}, {:name=>"Mummy", :nocturnal=>false, :dangers=>["bites", "scratches", "curses"], :vulnerabilities=>["fire", "decapitation", "cats"], :legs=>2}, {:name=>"Vampire", :nocturnal=>true, :dangers=>["bites", "hypnosis"], :vulnerabilities=>["wood", "decapitation", "crosses", "holy_water", "garlic", "daylight"], :legs=>2}, {:name=>"Werewolf", :nocturnal=>true, :dangers=>["bites", "scratches"], :vulnerabilities=>["silver"], :legs=>4}, {:name=>"Blob", :nocturnal=>false, :dangers=>["suffocation"], :vulnerabilities=>["CO2", "ice", "cold"], :legs=>0}] +1.9.3-p392 :012 > legs + => 10 +1 + +counts the legs. shorthand. adds ( sum ) numbers in the array. +$monsters.map{|m| m[:legs]}.inject(:+) + + + +1.9.3-p392 :031 > dangers.inject(Hash.new(0)){|hist, danger| hist[danger]+=1 ;hist} + => {["bites", "scratches"]=>2, ["bites", "scratches", "curses"]=>1, ["bites", "hypnosis"]=>1, ["suffocation"]=>1} +1 diff --git a/week3/exercises/monster.rb b/week3/exercises/monster.rb index 013c3d2..f2bbd46 100644 --- a/week3/exercises/monster.rb +++ b/week3/exercises/monster.rb @@ -1,14 +1,14 @@ require './named_thing.rb' -class Monster + +class Monsters include NamedThing - attr_accessor :vulnerabilities, :dangers - attr_reader :nocturnal, :legs - - def initialize(noc, legs, name="Monster", vul = [], dangers = []) - super(name) - @nocturnal = noc - @vlunerabilities = vul + attr_accessor :names, :legs, :dangers, :vulnerabilities, :nocturnal + + def initialize legs, nocturnal, name="Monster", dangers=[], vulnerabilities=[] + @leg = legs + @nocturnal = nocturnal @dangers = dangers - @legs = legs + @vulnerabilities = vulnerabilities + super name end -end +end diff --git a/week3/exercises/named_thing.rb b/week3/exercises/named_thing.rb deleted file mode 100644 index 323047c..0000000 --- a/week3/exercises/named_thing.rb +++ /dev/null @@ -1,15 +0,0 @@ -module NamedThing - attr_accessor :name - - def initialize(name) - @name = name - end - - def say_name - "My name is #{@name}" - end - - def shout_name - @name.upcase - end -end diff --git a/week3/exercises/vampire.rb b/week3/exercises/vampire.rb index 764adf6..0142f3c 100644 --- a/week3/exercises/vampire.rb +++ b/week3/exercises/vampire.rb @@ -1,6 +1,8 @@ require './monster.rb' + class Vampire < Monster - def initialize(noc=true, legs=2, name ="Vampire", vul=[:garlic, :sunlight], dangers=[:bites]) - super(noc,legs,name,vul,dangers) + + def initalize legs = 2, nocturnal = true, name = "Vampire", dangers = [:bites] , vulnerabilities = [:sunlight, :garlic] + super end end diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..990f301 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,20 @@ +# au jonas haskins +class Calculator + def sum(digits) + digits.inject 0, :+ + end + + def pow(v1, v2) + p = 1 + v2.times{ p *= v1 } + power = p + end + + def multiply(*nums) + nums.flatten.inject(:*) + end + + def fac(number) + (1..number).inject(:*) || 1 + end +end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..e600832 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,20 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +It is a object representing names and some strings inside the Ruby interpreter. +they are made by :some_name and :"string" literal syntax and by the to_sym 2. What is the difference between a symbol and a string? +a synbol is immutable. 3. What is a block and how do I call a block? +a block is code between two braces or between do - end +.. a way of grouping statements. +you call a block by calling yield or calling it as a Proc. 4. How do I pass a block to a method? What is the method signature? +a.method(&block) procs an ampersand 5. Where would you use regular expressions? +if you were matching any kind of string or pattern + diff --git a/week4/exercises/do_work.rb b/week4/exercises/do_work.rb new file mode 100644 index 0000000..a21da5d --- /dev/null +++ b/week4/exercises/do_work.rb @@ -0,0 +1,13 @@ +module DoWork + attr_accessor :work + + def initialize work + @work = work + end + + #def shout_name + # @name.upcase + #end + +end + diff --git a/week4/exercises/foobar b/week4/exercises/foobar new file mode 100644 index 0000000..3c86e9d --- /dev/null +++ b/week4/exercises/foobar @@ -0,0 +1,8 @@ + +'br.retsnom/.' eriuqer + + retsnoM < eripmaV ssalc + +]cilrag: ,thgilnus:[ = seitilibarenluv ,]setib:[ = sregnad ,"eripmaV" = eman ,eurt = lanrutcon ,2 = sgel ezilaitini fed +repus +dne dne \ No newline at end of file diff --git a/week4/exercises/worker.rb b/week4/exercises/worker.rb new file mode 100644 index 0000000..7e2e425 --- /dev/null +++ b/week4/exercises/worker.rb @@ -0,0 +1,11 @@ +class Worker + def self.work x=1 + if x == 1 + yield + else + x = x - 1 + x += yield + + end + end +end diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..b46c285 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,25 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +It uses the I from the I/O ! Input is part of the base class +IO which contains classes like File, which you would use to read in a file for example +File.open or File.read + 2. How would you output "Hello World!" to a file called my_output.txt? +File.open("my_output.txt", "w") do |file| + file.puts "Hello World!" +end + 3. What is the Directory class and what is it used for? +The Directory class ( Dir ) is used for representing directories +on the file system. It's used for listing directories and their contents. + + 4. What is an IO object? +A 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 ruby gem, and a simple build program simmilar to make. +a rake task is a task defined by a Rakefile which contains build rules +to do a job .. ie run some ruby code. diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..4264b55 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,33 @@ +#class Worker +# # this is a class level method +# def self.work x=1 +# if x == 1 +# yield +# else +# x = x - 1 +# x += yield +# +# end +# end +#end + + + +# class anserr +#class Worker +# # this is a class level method +# def self.work n=1 +# results = nil +# n.times { results = yield } +# results +# end +#end + + +# teachers answer +class Worker + # this is a class level method + def self.work n=1 + n.times.inject(nil){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/week4/jonas/code_timer.rb b/week4/jonas/code_timer.rb new file mode 100644 index 0000000..3c7b94f --- /dev/null +++ b/week4/jonas/code_timer.rb @@ -0,0 +1,7 @@ +class CodeTimer + def self.time_code n=1 + start_time = Time.now + n.times { yield } + Time.now - start_time + end +end diff --git a/week4/jonas/homework.2do b/week4/jonas/homework.2do new file mode 100644 index 0000000..bc8f0a3 --- /dev/null +++ b/week4/jonas/homework.2do @@ -0,0 +1,3 @@ +worker_spec.rb + +questions file. diff --git a/week4/jonas/timer_spec.rb b/week4/jonas/timer_spec.rb new file mode 100644 index 0000000..a7171f6 --- /dev/null +++ b/week4/jonas/timer_spec.rb @@ -0,0 +1,38 @@ +require './code_timer.rb' + +describe CodeTimer do + + it "should run our code" do + flag = false + + CodeTimer.time_code do + flag = true + end + + flag.should eq true + + end + + it "should time our code" do + + Time.stub(:now).and_return(0,3) + run_time = CodeTimer.time_code do + end + + run_time.should be_within(0.1).of(3.0) + end + + it "should run our code mult times " do + i = 0 + CodeTimer.time_code(10){ i+=1 } + i.should eq 10 + end + + it "should give us the average time " do + run_time = CodeTimer.time_code(10) { + sleep(1) + } + run_time.should be_within(0.1).of(1.0) + end + +end diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb new file mode 100644 index 0000000..dc732da --- /dev/null +++ b/week5/exercises/Rakefile.rb @@ -0,0 +1,32 @@ +task :default => [:hello_world] + +desc "This is the hellow worrld script" +task :eatit do + puts File.read("names") +end + +task :mkdir do + Dir.mkdir("Class") +end + +task : create_student_dirs => [:create_class_dir ]do + Dir.chdir('Class') + File.open("../names") do |f| + f.map{|line| Dir.mkdir line.chomp} + end +end + +task : create_student_dirs => [:create_class_dir ]do + Dir.chdir('Class') + File.open("../names") do |f| + f.map{|line| Dir.mkdir line.chomp} + end +end + +def file_helper(file_name) + File.open(file_name) do |f| + f.each do |line| + yield line.chomp + end + end +end diff --git a/week6/sanoj b/week6/sanoj new file mode 160000 index 0000000..434d47b --- /dev/null +++ b/week6/sanoj @@ -0,0 +1 @@ +Subproject commit 434d47b4522a5ec3c845a5ef99fdac874fee6d8e diff --git a/week7/exercises/features/step_definitions/converter.rb b/week7/exercises/features/step_definitions/converter.rb new file mode 100644 index 0000000..0df5bfb --- /dev/null +++ b/week7/exercises/features/step_definitions/converter.rb @@ -0,0 +1,26 @@ +Given(/^I have entered (\d+) into the converter$/) do |arg1| + # pending # express the regexp above with the code you wish you had + @temp +end + +Given(/^I set the type to Fahrenheit$/) do + # pending # express the regexp above with the code you wish you had + @type = 'Fahrenheit' +end + +When(/^I press convert$/) do + +end + +Then(/^the result returned should be (\d+)\.(\d+)$/) do |arg1, arg2| + #pending # express the regexp above with the code you wish you had + class Convert + def self + n = (@temp - 32) * 5 / 9 + n.round(1) + end + end + + +end + diff --git a/week7/exercises/features/step_definitions/converter2.rb b/week7/exercises/features/step_definitions/converter2.rb new file mode 100644 index 0000000..9046098 --- /dev/null +++ b/week7/exercises/features/step_definitions/converter2.rb @@ -0,0 +1,14 @@ +class Converter + + attr_writer :type + + def initalize celcius_value + @value = celcius_value.to_f + end + + + def convert + ((@value - 32 ) * (5.0 / 9.0)).round(1) + 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..b31c751 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,23 @@ +class PirateTranslator + attr_accessor :split + + + def say(what) + @what = what + end + + def translate + if ( @what =~ /Hello Friend/) + @what = 'Ahoy Matey' + "/n Shiber Me Timbers You Scurvey Dogs!!" + end + end + + # can't figger out how to return a array of them both? + # this isn;t right. + def speak + @arr = 'Shiber Me Timbers You Scurvey Dogs!!' + @speak_a.push("@what", "@arr") + @speak = speak.a.join(/\n /) + end +end + diff --git a/week7/homework/features/step_definitions/pirate.rb.class b/week7/homework/features/step_definitions/pirate.rb.class new file mode 100644 index 0000000..6800bed --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb.class @@ -0,0 +1,40 @@ +class PirateTranslator + attr_accessor :split + + PIRATE_WORDS = { + hello_friend: "Ahoy Matey" + } + + def say(what) + @what = what + end + + def translate + pirate_lookup @said + "\n Awh shiver me timbers " + end + + + private + + def pirate_lookup said + key = said.gsub(' ' , '_').downcase + PIRATE_WORDS[key] + end + + + + def translate + if ( @what =~ /Hello Friend/) + @what = 'Ahoy Matey' + end + end + + # can't figger out how to return a array of them both? + # this isn;t right. + def speak + @arr = 'Shiber Me Timbers You Scurvey Dogs!!' + @speak_a.push("@what", "@arr") + @speak = speak.a.join(/\n /) + end +end + diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index faf1a7f..1c084b8 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -12,6 +12,7 @@ Letgoandhaul /^it prints out '(.+)'$/ do |arg| @result.split("\n ").first.should == arg + puts "#{arg}" end Letgoandhaul /^it also prints '(.+)'$/ do |arg| diff --git a/week7/homework/features/step_definitions/rspec/expectations.rb b/week7/homework/features/step_definitions/rspec/expectations.rb new file mode 100644 index 0000000..ba38a01 --- /dev/null +++ b/week7/homework/features/step_definitions/rspec/expectations.rb @@ -0,0 +1 @@ +## diff --git a/week7/homework/features/step_definitions/rspec/mocks/standalone.rb b/week7/homework/features/step_definitions/rspec/mocks/standalone.rb new file mode 100644 index 0000000..ba38a01 --- /dev/null +++ b/week7/homework/features/step_definitions/rspec/mocks/standalone.rb @@ -0,0 +1 @@ +## diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..7ce1b89 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,30 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? +it's a method, called method_missing. + +it can by used to intercept the use of a undefined method and handle it. +you can also use it to intercept all the calls to methods, but only handle +some of them ( glob? ) + + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +A eigenclass is a hidden class associated with each specific instance of another class.. + +Singleton methods are specific to a single object. +They are defined on the object it's self and not the class. +Usually they are defined in a Eigenclass. + + 3. When would you use DuckTypeing? How would you use it to improve your code? +if you were coding in C# or Java or Perl. Readability?. I did Perl in DuckTyping. +I wouldn't use it in Ruby. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? +Class methods are methods that are called on a class, and instance methods are methods that are called on a instance of a class. +instance_eval defines class methods and class_eval defines instance methods. + 5. What is the difference between a singleton class and a singleton method? + +a singleton method is a method that is defined only for a single object, +and a singleton class is defined ON the object. diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index b8c8d4b..0f1b64e 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -4,9 +4,21 @@ def initialize(pillows, cushions) @cushions = cushions end - [:pillows, :cushions, :dogs].each do |s| +# [:pillows, :cushions, :dogs].each do |s| +# define_method("how_many_#{s}") do +# instance_variable_get("@#{s}").count +# end +# end + + [:pillows, :cushions ].each do |s| define_method("how_many_#{s}") do instance_variable_get("@#{s}").count end end -end \ No newline at end of file + + define_method("#{s.to_s.gsub('s', '')}_colors") do + instance_variable_get("@#{s}").each do | color | + puts color + end + end +end diff --git a/week9/Songsoftheday b/week9/Songsoftheday new file mode 160000 index 0000000..e39e3cd --- /dev/null +++ b/week9/Songsoftheday @@ -0,0 +1 @@ +Subproject commit e39e3cd7eb049ef7914f6e173d3709e7351a93da diff --git a/week9/comments_examples b/week9/comments_examples new file mode 100644 index 0000000..3fbf5a3 --- /dev/null +++ b/week9/comments_examples @@ -0,0 +1,5 @@ +# how your doing something in a method +# something tied to sepcifics .. +# carefull about putting votile info in comments + # change code , don' t update the comments. + # that is bad ... diff --git a/week9/exceptional-ruby.rb b/week9/exceptional-ruby.rb new file mode 100644 index 0000000..cdc75e4 --- /dev/null +++ b/week9/exceptional-ruby.rb @@ -0,0 +1,37 @@ +# exception example +#class JonasError < Exception +# def message +# "its messed up " +# end +#end + +#begin + + #raise "AGGGG" +# raise JonasError.new + +#puts 0/1 + +# f = File.open('hello.txt') +# f.close +# f.read +#rescue => e +##rescue => e +# puts e +# puts e.class +#rescue IOError => e +# puts "there was a io error" +#ensure +# puts "hello world" +#end +# +# +catch :yipes do + + i = 0 + while i<10 + sleep (2) and throw :yipes if i == 5 + puts i+=1 + end +end + diff --git a/week9/sinatra.rb b/week9/sinatra.rb new file mode 100644 index 0000000..3850965 --- /dev/null +++ b/week9/sinatra.rb @@ -0,0 +1,14 @@ +require 'sinatra' +require 'sinatra/reloader' +require 'puma' + +get '/' do + " + +

HI<

+
+ + " +end + + diff --git a/week9/struce b/week9/struce new file mode 100644 index 0000000..80b21e9 --- /dev/null +++ b/week9/struce @@ -0,0 +1,81 @@ +jonashaskins@zadmins-MacBook-Pro-2~/RUBY_CLASS/RubyFall2013/week9$ irb +1.9.3p392 :001 > Dog = Struct.new(:name, :owner, :age) + => Dog +1.9.3p392 :002 > d = Dog.new('Bradley', 'Jonas', '6') + => # +1.9.3p392 :003 > d.name + => "Bradley" +1.9.3p392 :004 > d.age + => "6" +1.9.3p392 :005 > d.owner + => "Jonas" +1.9.3p392 :006 > d.name = "Rockey the Dog" + => "Rockey the Dog" +1.9.3p392 :007 > d.name + => "Rockey the Dog" +1.9.3p392 :008 > d.age = '100' + => "100" +1.9.3p392 :009 > d.age + => "100" +1.9.3p392 :010 > + +1.9.3p392 :016 > d.each_pair do |i| +1.9.3p392 :017 > puts i +1.9.3p392 :018?> end +name +owner +age + => # +1.9.3p392 :019 > d.each_pair{|k,v| puts k} +name +owner +age + => # +1.9.3p392 :020 > + + + + +jonashaskins@zadmins-MacBook-Pro-2~/RUBY_CLASS/RubyFall2013/week9$ irb +1.9.3p392 :001 > Dog = Struct.new(:name, :owner, :age) + => Dog +1.9.3p392 :002 > d = Dog.new('Bradley', 'Jonas', '6') + => # +1.9.3p392 :003 > d.name + => "Bradley" +1.9.3p392 :004 > d.age + => "6" +1.9.3p392 :005 > d.owner + => "Jonas" +1.9.3p392 :006 > d.name = "Rockey the Dog" + => "Rockey the Dog" +1.9.3p392 :007 > d.name + => "Rockey the Dog" +1.9.3p392 :008 > d.age = '100' + => "100" +1.9.3p392 :009 > d.age + => "100" +1.9.3p392 :010 > +1.9.3p392 :011 > +1.9.3p392 :012 > d.map{|a|a} + => ["Rockey the Dog", "Jonas", "100"] +1.9.3p392 :013 > d + => # +1.9.3p392 :014 > d.select{|a|a == 7} + => [] +1.9.3p392 :015 > d.select{|a|a == 7} + => [] +1.9.3p392 :016 > d.each_pair do |i| +1.9.3p392 :017 > puts i +1.9.3p392 :018?> end +name +owner +age + => # +1.9.3p392 :019 > d.each_pair{|k,v| puts k} +name +owner +age + => # +1.9.3p392 :020 > + diff --git a/midterm/wish_list_spec.rb b/wish_list_spec.rb similarity index 100% rename from midterm/wish_list_spec.rb rename to wish_list_spec.rb