diff --git a/RubyFall2012.sublime-project b/RubyFall2012.sublime-project new file mode 100644 index 0000000..8c372fb --- /dev/null +++ b/RubyFall2012.sublime-project @@ -0,0 +1,8 @@ +{ + "folders": + [ + { + "path": "/home/nikky/Repositories/github/RubyFall2012" + } + ] +} diff --git a/RubyFall2012.sublime-workspace b/RubyFall2012.sublime-workspace new file mode 100644 index 0000000..81170b3 --- /dev/null +++ b/RubyFall2012.sublime-workspace @@ -0,0 +1,1083 @@ +{ + "auto_complete": + { + "selected_items": + [ + [ + "core", + "core_stats" + ], + [ + "non", + "non_assign_stat_grid" + ], + [ + "female", + "female_first_names" + ], + [ + "male", + "male_first_names" + ], + [ + "AVAIL", + "AVAILABLE_RACES" + ], + [ + "get", + "get_with_array" + ], + [ + "list", + "list_characters" + ], + [ + "chara", + "characters" + ], + [ + "character", + "character_unique_traits" + ], + [ + "last", + "last_names" + ], + [ + "generate", + "generate_sex" + ], + [ + "select", + "select_race" + ], + [ + "err", + "error_check" + ], + [ + "en", + "element" + ] + ] + }, + "buffers": + [ + { + "file": "week3/exercises/monster.rb", + "settings": + { + "buffer_size": 310, + "line_ending": "Unix" + } + }, + { + "file": "week3/exercises/named_thing.rb", + "settings": + { + "buffer_size": 167, + "line_ending": "Unix" + } + }, + { + "file": "week3/exercises/vampire.rb", + "settings": + { + "buffer_size": 186, + "line_ending": "Unix" + } + }, + { + "file": "week3/exercises/monsters.rb", + "settings": + { + "buffer_size": 830, + "line_ending": "Unix" + } + } + ], + "build_system": "", + "command_palette": + { + "height": 392.0, + "selected_items": + [ + [ + "beau", + "BeautifyRuby" + ], + [ + ":w", + ":w - Save" + ], + [ + "project", + "Project: Close" + ], + [ + ":u", + "Snippet: class .. < Test::Unit::TestCase .. end" + ], + [ + ":W", + ":w - Save" + ], + [ + "install", + "Package Control: Install Package" + ], + [ + "instll", + "Package Control: Install Package" + ], + [ + "in", + "Indentation: Reindent Lines" + ], + [ + "ann", + "Git: Toggle Annotations" + ], + [ + "anno", + "Git: Toggle Annotations" + ], + [ + "bea", + "BeautifyRuby" + ], + [ + "git stat", + "Git: Status" + ], + [ + "remove", + "Package Control: Remove Package" + ], + [ + "remo", + "Package Control: Remove Package" + ], + [ + "tidy", + "Tidy HTML" + ], + [ + "package", + "Package Control: Install Package" + ], + [ + "hg", + "SublimeHg: Open Menu" + ], + [ + "nex", + "SublimeLint: Next Error" + ], + [ + "next", + "SublimeLint: Next Error" + ], + [ + "lint", + "SublimeLint: Next Error" + ], + [ + "rem", + "Package Control: Remove Package" + ], + [ + "tid", + "Tidy HTML" + ], + [ + "inten", + "Indentation: Reindent Lines" + ], + [ + "tag", + "Tag: Auto-Format Tags on Document" + ], + [ + "zencoding", + "Zen Coding: Endcode/decode base64" + ], + [ + "zen coding", + "Set Syntax: ZenCoding" + ], + [ + "zen ", + "Zen Coding: Evaluate math expression" + ], + [ + "zen", + "Zen Coding: Zen as you type" + ], + [ + "html", + "Tidy HTML" + ], + [ + "sublime", + "SublimeLinter: Show Error List" + ], + [ + "package in", + "Package Control: Install Package" + ], + [ + "Package Control: ", + "Package Control: Discover Packages" + ], + [ + "inden", + "Indentation: Reindent Lines" + ], + [ + "indent", + "Indentation: Reindent Lines" + ], + [ + "ident", + "Indentation: Reindent Lines" + ], + [ + "hg ", + "SublimeHg: Open Menu" + ], + [ + "html ", + "Tidy HTML" + ], + [ + "intent", + "Indentation: Reindent Lines" + ], + [ + "rein", + "Indentation: Reindent Lines" + ], + [ + "open", + "View: Toggle Open Files in Side Bar" + ], + [ + "install pack", + "Package Control: Install Package" + ], + [ + "package re", + "Package Control: Remove Package" + ], + [ + "package install", + "Package Control: Install Package" + ], + [ + "sftp", + "SFTP: Browse Server…" + ], + [ + "package install", + "Package Control: Install Package" + ] + ], + "width": 593.0 + }, + "console": + { + "height": 241.0 + }, + "distraction_free": + { + "menu_visible": true, + "show_minimap": false, + "show_open_files": false, + "show_tabs": false, + "side_bar_visible": false, + "status_bar_visible": false + }, + "file_history": + [ + "/home/nikky/Repositories/github/RubyFall2012/week2/homework/simon_says_spec.rb", + "/home/nikky/Repositories/github/RubyFall2012/week2/homework/questions.txt", + "/home/nikky/Repositories/github/RubyFall2012/week2/homework/simon_says.rb", + "/home/nikky/Repositories/www/choosing_cms.html", + "/home/nikky/Repositories/www/start.html", + "/home/nikky/Repositories/www/index.html", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/Preferences.sublime-settings", + "/home/nikky/Nikky_Sim_0.00.00/source/nikky.c", + "/home/nikky/Nikky_Sim_0.00.00/source/vocab.c", + "/home/nikky/Nikky_Sim_0.00.00/source/ui.c", + "/home/nikky/Nikky_Sim_0.00.00/license.txt", + "/home/nikky/Nikky_Sim_0.00.00/source/build.sh", + "/home/nikky/Repositories/github/UW-Wordpress-Theme/index.php", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/Automatic Backups.sublime-settings", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/BeautifyRuby/BeautifyRuby.sublime-settings", + "/home/nikky/Repositories/github/Mass-Email-o-nator/mailer3.rb", + "/home/nikky/Repositories/github/Mass-Email-o-nator/configuration.rb", + "/home/nikky/Repositories/github/Mass-Email-o-nator/configuration.sample.rb", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/Default/Preferences.sublime-settings", + "/home/nikky/Repositories/github/Mass-Email-o-nator/doop", + "/home/nikky/Repositories/github/staff-website/doop", + "/home/nikky/Repositories/www/access.html", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/SublimeLinter.sublime-settings", + "/home/nikky/Repositories/www/basic-group.html", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/SublimeLinter/README.md", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/SublimeLinter/SublimeLinter.sublime-settings", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/SublimeLinter/Default (Linux).sublime-keymap", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/sublimelint/SublimeLint.sublime-settings", + "/home/nikky/Repositories/www/web-development-servers.html", + "/home/nikky/Repositories/www/mysql/mysql-admin.html", + "/home/nikky/Repositories/www/mysql/mysql-installed.html", + "/home/nikky/Repositories/www/www.sublime-project", + "/home/nikky/Repositories/www/mysql/index.html", + "/home/nikky/Repositories/www/using-servers.html", + "/home/nikky/Repositories/www/website-urls.html", + "/home/nikky/Repositories/www/mailing-lists.html", + "/home/nikky/Repositories/dnd-4e-character-generator/lessons_learned.md", + "/home/nikky/Repositories/dnd-4e-character-generator/class_test.rb", + "/home/nikky/Repositories/dnd-4e-character-generator/libGen.rb", + "/home/nikky/Repositories/dnd-4e-character-generator/array_object_test.rb", + "/home/nikky/Repositories/learn_c_hard_way/ex11.c", + "/home/nikky/Repositories/learn_c_hard_way/ex11", + "/home/nikky/Repositories/learn_c_hard_way/ex3.c", + "/home/nikky/Repositories/learning-ruby/mailer3/mailer3.rb", + "/home/nikky/Repositories/www/.wwwinstrc", + "/home/nikky/Repositories/www/publish.html", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/ZenCoding/Default.sublime-keymap", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/HtmlTidy/HtmlTidy.sublime-settings", + "/home/nikky/Repositories/www/ssh.html", + "/home/nikky/Repositories/www/mysql/phpmyadmin.html", + "/home/nikky/Repositories/www/mysql/phpmyadmin-upgrade.html", + "/home/nikky/Repositories/www/phpmyadmin.html", + "/home/nikky/Repositories/www/phpmyadmin-upgrade.html", + "/home/nikky/Repositories/www/snippets/ssh_web_dev.html", + "/home/nikky/Repositories/www/mysql/mysql-security.html", + "/home/nikky/Repositories/www/mysql/mysql-desktop.html", + "/home/nikky/Repositories/www/mysql/mysql-client.html", + "/home/nikky/Repositories/www/mysql/mysql_backup.sh", + "/home/nikky/Repositories/learning-ruby/mailer3/configuration.rb", + "/home/nikky/Repositories/learning-ruby/mailer3/libMailer3.rb", + "/home/nikky/Repositories/learning-ruby/mailer3/do_mail.rb", + "/home/nikky/Repositories/learning-ruby/mailer3/settings.yml", + "/home/nikky/Repositories/learning-ruby/mailer3/settings.yaml", + "/home/nikky/Repositories/www/mysql/mysql-script.html", + "/home/nikky/Repositories/www/mysql/mysql-lists.html", + "/home/nikky/Repositories/www/mysql/mysql-access-3.5.html", + "/home/nikky/Repositories/www/mysql/mysql-installed.old.html", + "/home/nikky/Dropbox/config/deploy.rb", + "/home/nikky/learning-ruby/mailer3/mailer3.rb", + "/tmp/sublime-sftp-browse-1344966977/nykida_net/home/nikky/.vimrc", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/sftp_servers/nikky_ovid", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/sftp_servers/nykida_net", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/Default/Default (Linux).sublime-keymap", + "/home/nikky/Dropbox/config/sublime-text-2/Packages/User/Distraction Free.sublime-settings" + ], + "find": + { + "height": 36.0 + }, + "find_in_files": + { + "height": 93.0, + "where_history": + [ + "" + ] + }, + "find_state": + { + "case_sensitive": false, + "find_history": + [ + "cheese", + "mediawiki", + "nav-coll", + "dropdowns", + "Walker_Menu", + "uw_drop", + "menu", + "head", + "shared_netid", + "cc", + "message_parse_block", + " @message_parse_block", + "@message_parse_block", + "ruler", + "width", + "@config", + "$debug", + " $debug", + "$debug", + "debug", + "$debug", + "log", + "log_event", + "html", + "map", + "ruby", + "backing up", + "for", + "home pages", + "Individual", + "ossible", + "gets", + "generate_stats", + ".class", + "class", + "Elf", + "halfling", + "Elf", + "Tiefling", + "Dragonborn", + "Warrior", + "Scaffold_new_character", + "characters", + "male_names", + "unique_traits", + "load_class_stats", + "generate_stats", + "nil", + "btn-info", + "btn", + "large", + "primary", + "btn", + "fugu", + "Click for more information on development", + "strong", + "b>", + "Tutorial Introduction", + "Backing up your", + "@", + "div", + "Renée is the coolest teacher ever!"+ "
Click Here" end + +get '/yousuck' do + stream do |out| + out << "fail\n" + sleep 0.5 + out << "\nsauce" + sleep 1 + end +end \ No newline at end of file diff --git a/week10/class_materials/sinatra.rb b/week10/class_materials/sinatra.rb new file mode 100644 index 0000000..f7a9b48 --- /dev/null +++ b/week10/class_materials/sinatra.rb @@ -0,0 +1,153 @@ +require 'sinatra' + +get '/' do + "

NIKKY IS AWESOME YAY WHAT A CHAMP

" + "Find out why" +end + +get '/coolest' do + "Because he is, fool" +end + +get '/yousuck' do + stream do |out| + out << "fail\n" + sleep 0.5 + out << "\nsauce" + sleep 1 + end + +end + +get '/pledge' do +%Q{ + + + +KUOW Pledge Drive + + + +
+-------------------------------------------------------------------------------+
+|                __  ___  __    __    ______   ____    __    ____               |
+|               |  |/  / |  |  |  |  /  __  \  \   \  /  \  /   /               |
+|               |  '  /  |  |  |  | |  |  |  |  \   \/    \/   /                |
+|               |    <   |  |  |  | |  |  |  |   \            /                 |
+|               |  .  \  |  `--'  | |  `--'  |    \    /\    /                  |
+|               |__|\__\  \______/   \______/      \__/  \__/                   |
+|            .______    __       _______  _______   _______  _______            |
+|            |   _  \  |  |     |   ____||       \ /  _____||   ____|           |
+|            |  |_)  | |  |     |  |__   |  .--.  |  |  __  |  |__              |
+|            |   ___/  |  |     |   __|  |  |  |  |  | |_ | |   __|             |
+|            |  |      |  `----.|  |____ |  '--'  |  |__| | |  |____            |
+|            | _|      |_______||_______||_______/ \______| |_______|           | 
+|                _______  .______       __  ____    ____  _______               |
+|               |       \ |   _  \     |  | \   \  /   / |   ____|              |
+|               |  .--.  ||  |_)  |    |  |  \   \/   /  |  |__                 |
+|               |  |  |  ||      /     |  |   \      /   |   __|                |
+|               |  '--'  ||  |\  \----.|  |    \    /    |  |____               |
+|               |_______/ | _| `._____||__|     \__/     |_______|              |
+|                                                                               |
+|                          Strength Through Unity                               |
+|                        Unity Through Public Radio                             |
++-------------------------------------------------------------------------------+
+
+
+ +} +end \ No newline at end of file diff --git a/week10/class_materials/struct.rb b/week10/class_materials/struct.rb new file mode 100644 index 0000000..8a8986a --- /dev/null +++ b/week10/class_materials/struct.rb @@ -0,0 +1,11 @@ +Struct.new('Person', :full_name, :email) do + def name + "hello my name is #{:full_name}" + end +end +p = Struct::Person.new('Renee', 'renee@awesome.us') + +# or + +Person = Struct.new(:full_name, :email) +p = Person.new('renee', 'nikky@awesome.us') \ No newline at end of file diff --git a/week10/index.html b/week10/index.html new file mode 100644 index 0000000..6a79207 --- /dev/null +++ b/week10/index.html @@ -0,0 +1 @@ +
Find out why \ No newline at end of file diff --git a/week10/index.html.1 b/week10/index.html.1 new file mode 100644 index 0000000..6a79207 --- /dev/null +++ b/week10/index.html.1 @@ -0,0 +1 @@ +Find out why \ No newline at end of file diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index 50bc054..176565b 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,13 +1,12 @@ class Book - - attr_accessor :title, :pages - - def initialize(title, pages) + attr_reader :title, :author, :isbn + def initialize(title, page_count, author="N/A", isbn="N/A") @title = title - @pages = pages + @author = author + @isbn = isbn + @page_count = page_count end - def page_count - "Page count is #{@pages}" + "Page count is #{@page_count}" end end diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index bb22819..df39b89 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -5,10 +5,13 @@ @book = Book.new("Harry Potter", 200) end it "should respond to title" do - @book.should respond_to "title" + @book.should respond_to :title end it "should return the page count" do @book.page_count.should eq "Page count is 200" end + it "should respond to isbn" do + @book.should respond_to :isbn + end end diff --git a/week2/exercises/mad_libs.rb b/week2/exercises/mad_libs.rb old mode 100644 new mode 100755 index 3af5583..e63fc7c --- a/week2/exercises/mad_libs.rb +++ b/week2/exercises/mad_libs.rb @@ -1,10 +1,9 @@ -puts "Please enter a noun" +#!/usr/bin/env ruby +puts "Please enter a ballin' noun" noun = gets.chomp -puts "Please enter an adjective" +puts "Please enter a slammin' adjective" adjective = gets.chomp -puts "Please enter a past tense action verb" +puts "Please enter a past tense action-y verb" verb_past_tense = gets.chomp -puts "What does the #{noun} say?" -says = gets.chomp -story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard and says #{says}" +story = "The #{adjective} #{noun} #{verb_past_tense} around champville, home of the awesome squad." puts story diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 4cfc0a3..105fc4d 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -2,6 +2,32 @@ Please Read The Chapters on: Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins +## My Answers + +Q1. What is the difference between a Hash and an Array? + +A1. Hashes and arrays are both indexed collections of objects, however, while an array is indexed by non-negative integers, a hash +is indexed by other objects referred to as keys. Symbols are often used as keys. + +Q2. When would you use an Array over a Hash and vice versa? + +A2. Arrays are useful when you just need an ordered list or a collection of objects. Hashes are better for when you need to have a name or some +useful reference to these objects, such as for configuration variables or other settings. + +Q3. What is a module? Enumerable is a built in Ruby module, what is it? + +A3. A module is a portable set of methods that can be imported into a specific class. Enumerable is a module that allows you to add various collection iteration methods + +Q4. Can you inherit more than one thing in Ruby? How could you get around this problem? + +A4. No; you must use mixins. + +Q5. What is the difference between a Module and a Class? + +A5. Classes define a set of objects, an object cannot not be initialized using a module. + +## UWE Answers + 1. What is the difference between a Hash and an Array? An array is an ordered list of items that are referenced by their index (order), a hash is a collection of items that can be referenced by a key and have no order. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index fa35ccd..3e3f7e1 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,20 +1,40 @@ module SimonSays - def echo(st) - st - end - - def shout(st) - st.upcase - end + def echo(text) + text + end + def shout(text) + text.upcase + end + def repeat(text,times=2) + ("#{text} " * times).rstrip + end + def start_of_word(text,letters=1) + text[0,letters] + end + def first_word(text,words=1) + # Could also be done with a regex + text.split(" ").first + end +end - def first_word(st) - st.split.first - end +# UWE-Ruby Answers +module SimonSays + def echo(st) + st + end + + def shout(st) + st.upcase + end + + def first_word(st) + st.split.first + end + + def start_of_word(st,i) + st[0...i] + end - def start_of_word(st,i) - st[0...i] - end - def repeat(st, t=2) return "Go Away!" if t==0 ([st]*t).join(' ') diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index a17ac8d..5bfba8f 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -8,7 +8,10 @@ it "should echo hello" do echo("hello").should == "hello" end - + it "should echo cheese" do + echo("cheese").should == "cheese" + end + it "should echo bye" do echo("bye").should == "bye" end diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index f8916e3..e6c60fc 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,5 +1,22 @@ class Calculator def sum(array) + # Nikky answers + return 0 if array.empty? + array.inject(:+) + end + def multiply(array) + return 0 if array.empty? + array.inject(:*) + end + def power(n1,n2) + n1**n2 + end + def factorial(n) + return 0 if n.zero? + (1..n).inject(:*) + end +end +# Class answers array.inject(0){|sum, x| sum +x} end diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..260aa3b 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,7 +1,7 @@ require "#{File.dirname(__FILE__)}/calculator" describe Calculator do - + before do @calculator = Calculator.new end @@ -10,23 +10,57 @@ it "computes the sum of an empty array" do @calculator.sum([]).should == 0 end - + it "computes the sum of an array of one number" do @calculator.sum([7]).should == 7 end - + it "computes the sum of an array of two numbers" do @calculator.sum([7,11]).should == 18 end - + it "computes the sum of an array of many numbers" do @calculator.sum([1,3,5,7,9]).should == 25 end end - - # Once the above tests pass, + + # Once the above tests pass, # write tests and code for the following: describe "#multiply" do +# Nikky Answers + it "multiplies two numbers" do + @calculator.multiply([5,6]).should == 30 + end + + it "multiplies an array of numbers" do + @calculator.multiply([5,6,10]).should == 300 + end + end + + describe "#power" do + it "raises one number to the power of another number" do + @calculator.power(5,6).should == 15625 + end + end + + # http://en.wikipedia.org/wiki/Factorial + describe "#factorial" do + it "computes the factorial of 0" do + @calculator.factorial(0).should == 0 + end + it "computes the factorial of 1" do + @calculator.factorial(1).should == 1 + end + it "computes the factorial of 2" do + @calculator.factorial(2).should == 2 + end + it "computes the factorial of 5" do + @calculator.factorial(5).should == 120 + end + it "computes the factorial of 10" do + @calculator.factorial(10).should == 3628800 + end +# Class Answers it "multiplies two numbers" do @calculator.multiply(2,2).should eq 4 end @@ -62,7 +96,5 @@ it "computes the factorial of 10" do @calculator.fac(10).should eq 3628800 end - end - end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index 08067b8..e748eac 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -4,6 +4,35 @@ Please Read: - Chapter 7 Regular Expressions - Chapter 22 The Ruby Language: basic types (symbols), variables and constants +Nikky Answers +============= +Q1. What is a symbol? + +A1. A symbol is a constant that has a consistent name. + +Q2. What is the difference between a symbol and a string? + +A2. A symbol is a consistent object, that is guarenteed to be unique. A string is a changeable object. + +Q3. What is a block and how do I call a block? + +A3. A block of a chunk of code that contains the variables when it was initialized. You can call a block by `yield` +or Proc::call. + +Q4. How do I pass a block to a method? What is the method signature? + +A4. At the end of the method call between curly brackets, such as do_this_for_reals { block here }, which can then be called with yield. +If you'd rather pass in a proc, method_name(arg1, argv2, &proc) + +Q5. Where would you use regular expressions? + +A5. Where wouldn't you use a regular expression? They're useful to match patterns, +pull specific chunks of strings that match patterns out, and all sorts of other such +tidbits. Not to mention substitution! + +Class Answers +============= + 1. What is a symbol? A symbol is a static name or identifier. @@ -26,3 +55,4 @@ end 5. Where would you use regular expressions? Regular expressions are used for pattern matching and replacement with strings. An example would be if I wanted to write a syntax checker for some text that checked if each sentance ended with a period, started with a space and then a capital letter. +S diff --git a/week4/class_materials/OddNumber.rb b/week4/class_materials/OddNumber.rb new file mode 100644 index 0000000..32a3a7b --- /dev/null +++ b/week4/class_materials/OddNumber.rb @@ -0,0 +1,17 @@ +class OddNumber + attr_accessor :number + def initialize(n) + @value = n + end + def secc + if @value.even? + new_val = OddNumber.new(@value + 1 ) + else + new_val = OddNumber.new(@value + 2 ) + end + new_val + end + def <=> (o) + @value <=> o.value + end +end diff --git a/week4/class_materials/timer.rb b/week4/class_materials/timer.rb index 67b7681..5eeb4e6 100644 --- a/week4/class_materials/timer.rb +++ b/week4/class_materials/timer.rb @@ -1,8 +1,14 @@ class Timer - def self.time_code(n=1) + def self.time_code(n=1,&my_code) + # or yield start_time = Time.now - n.times{yield} + n.times { my_code.call } end_time = Time.now - (end_time - start_time) / n.to_f + (end_time - start_time) / n.to_f +# def self.time_code(n=1) +# start_time = Time.now +# n.times{yield} +# end_time = Time.now +# (end_time - start_time) / n.to_f end -end \ No newline at end of file +end diff --git a/week4/class_materials/timer_spec.rb b/week4/class_materials/timer_spec.rb index f71414a..4be1235 100644 --- a/week4/class_materials/timer_spec.rb +++ b/week4/class_materials/timer_spec.rb @@ -2,20 +2,31 @@ describe Timer do - it "should report the time difference" do - Time.stub(:now).and_return(0,3) - time_difference = Timer.time_code do - end - time_difference.should be_within(0.1).of(3.0) - end + it "should report the time difference" do + # The first time Time.now is run, return 0, every time thereafter, 3 + Time.stub(:now).and_return(0,3) + time_difference = Timer.time_code do + end + time_difference.should be_within(0.1).of(3.0) + end - it "should run our code" do - flag = false - Timer.time_code do - flag = true - end - flag.should be_true - end + it "should run our code" do + flag = false + Timer.time_code do + flag = true + end + flag.should be_true + end + it "should run our code multiple times" do + counter = 0 + result = Timer.time_code(17) {counter += 1} + counter.should eq 17 + end + it "should give the average time" do + Time.stub(:now).and_return(0,10) + result = Timer.time_code(10) { } + result.should be_within(0.1).of(1) + end it "should run our code multiple times" do counter = 0 diff --git a/week4/exercises/worker.rb b/week4/exercises/worker.rb index fad4e2f..9355a9e 100644 --- a/week4/exercises/worker.rb +++ b/week4/exercises/worker.rb @@ -1,5 +1,13 @@ class Worker - def self.work(n=1) - n.times.inject(nil){yield} - end -end \ No newline at end of file + def self.work(n=1) + result = '' + n.times do + result = yield + end + result + end +end +# def self.work(n=1) +# n.times.inject(nil){yield} +# end +#end diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..f4bf3de 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,29 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? + +Ruby reads files line-by-line using an IO object. + 2. How would you output "Hello World!" to a file called my_output.txt? + +File.open("my_output.txt","w") do |f| + file.puts "Hello World!" +end + 3. What is the Directory class and what is it used for? + +(I assume this is the Dir class). Dir allows you to interact with directories on the system +that is running the Ruby code. You can change directories, list contents, and other similar +directory-related functions. + 4. What is an IO object? + +An IO object allows you to do much more with IO than puts/gets allows. Essentially, it allows two-way communication +with another resource. You can read an write from files, as +well as interact with other servers and protocols across TCP/IP sockets. + 5. What is rake and what is it used for? What is a rake task? + +Rake is Make... but in Ruby. You can use it to automate certain "tasks" such as cleaning up +test instances, files, and automating other functionality. A task is a certain activity that you define, such as +"clean." diff --git a/week5/exercises/Rakefile b/week5/exercises/Rakefile index ce332af..70a559f 100644 --- a/week5/exercises/Rakefile +++ b/week5/exercises/Rakefile @@ -1,10 +1,35 @@ task :test do puts "Hello World!" end +<<<<<<< HEAD +task :list_names do + File.open("names","r") do |n| + n.each do |l| + puts l + end + end +end +task :create_directory do + Dir.mkdir("class") unless Dir.exists?("class") +end +task :populate_directory => [:create_directory] do + parse_name_file("names") + +end + +def parse_name_file(file) + f = File.open(file,"r") + Dir.cd("class") + f.readlines.each do |n| + Dir.mkdir(n) + end +end +======= task :make_class_dir do Dir.mkdir("class") end task :output +>>>>>>> 5a2c257e99f58173ae91aeee5ce8af92c39e2ab6 diff --git a/week5/exercises/names b/week5/exercises/names index 9eec0ec..552edd2 100644 --- a/week5/exercises/names +++ b/week5/exercises/names @@ -1,35 +1,33 @@ -Ben +name +Brian +Renee +Yan +Nell BrianT -BrianWard -BryanWilliams -Cheri -Chris B -ChristopherF -ChristopherT +Emily Danny -Dimitry +Chris +ChrisF +Mallory +Greg +Tim Eddie -Emily -Gregory +Steven Josh -Karen -Kat +Nikky +ChrisB Kelli +Ben +Cheri +Strand Kris -Mallory -Nell -NicholasP -Nicole -Nikky -Peter -Price -Renée -Ryan Santy +Nicholas +Karen +Bryan Serene -Sol -Steven -Strand -Timothy +Peter +Price Will -Yan +Nicole +Sol diff --git a/week7/exercises/features/step_definitions/converter.rb b/week7/exercises/features/step_definitions/converter.rb index 4cb1264..b3dee08 100644 --- a/week7/exercises/features/step_definitions/converter.rb +++ b/week7/exercises/features/step_definitions/converter.rb @@ -12,6 +12,6 @@ def convert end def Fahrenheit_convertion - (((@value - 32.0) /5.0) * 9.0).round(1) + (((@value - 32.0) /9.0) * 5.0).round(1) end end \ No newline at end of file diff --git a/week7/exercises/features/step_definitions/puppy.rb b/week7/exercises/features/step_definitions/puppy.rb new file mode 100644 index 0000000..bdc86c7 --- /dev/null +++ b/week7/exercises/features/step_definitions/puppy.rb @@ -0,0 +1,20 @@ +class Puppy + def name=(arg1) + @name = arg1 + end + def pet + wag_tail + end + def wag_tail + "Yayyyy!" + end + def ring_bell + wag_tail + end + def take_dog_out + @dog_walked = true + end + def pee? + @dog_walked? false : true + end +end \ No newline at end of file diff --git a/week7/exercises/features/step_definitions/puppy_steps.rb b/week7/exercises/features/step_definitions/puppy_steps.rb new file mode 100644 index 0000000..5f1d96e --- /dev/null +++ b/week7/exercises/features/step_definitions/puppy_steps.rb @@ -0,0 +1,35 @@ +Given /^we have a puppy$/ do + @puppy = Puppy.new +end + +Given /^its name is Fred$/ do + @puppy.name = "Fred" +end + +When /^we pet the puppy$/ do + @result = @puppy.pet +end + +Then /^the puppy wags its tail$/ do + @result.should == "Yayyyy!" +end + +Given /^its name is Bella$/ do + @puppy.name = "Bella" +end + +When /^we ring the bell$/ do + @result = @puppy.ring_bell +end + +When /^it wags its tail$/ do + @result.should == "Yayyyy!" +end + +Then /^we must take it out$/ do + @puppy.take_dog_out.should == true +end + +Then /^then it will not pee on the floor$/ do + @puppy.pee?.should == false +end diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb index d2a6f6a..3411649 100644 --- a/week7/homework/features/step_definitions/pirate.rb +++ b/week7/homework/features/step_definitions/pirate.rb @@ -1,3 +1,21 @@ +# nikky version +class Pirate + def say(str1) + @phrase = str1 + end + def translate + @phrases = { 'Hello Friend' => 'Ahoy Matey' } + #false if @phrase.nil? + @result = @phrases.fetch(@phrase, "derp") + end + def say_result + @result + end + def end_convo + "Shiber Me Timbers You Scurvey Dogs!!" + end +end +# class answer class PirateTranslator PIRATE_WORDS = { "Hello Friend" => "Ahoy Matey" @@ -14,4 +32,4 @@ def translate def lookup_pirate(str) PIRATE_WORDS[str] end -end \ No newline at end of file +end diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index faf1a7f..5b00215 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -1,3 +1,25 @@ +# class answers + +Gangway /^I have a PirateTranslator$/ do + @parrot = Pirate.new +end + +Blimey /^I say 'Hello Friend'$/ do + @parrot.say("Hello Friend").should == "Hello Friend" +end + +Blimey /^I hit translate$/ do + @parrot.translate +end + +Letgoandhaul /^it prints out 'Ahoy Matey'$/ do + @parrot.say_result.should == "Ahoy Matey" +end + +Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do + @parrot.end_convo.should == 'Shiber Me Timbers You Scurvey Dogs!!' +#mine +end Gangway /^I have a (\w+)$/ do |arg| @translator = Kernel.const_get(arg).new 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..a9bd88e 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -35,7 +35,7 @@ Then /^the computer prints "(.*?)"$/ do |arg1| @game.should_receive(:puts).with(arg1) - @game.indicate_palyer_turn + @game.indicate_player_turn end Then /^waits for my input of "(.*?)"$/ do |arg1| diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 19bcc72..4e9c106 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -1,129 +1,304 @@ class TicTacToe - SYMBOLS = [:X,:O] - attr_accessor :player, :board - - def initialize(current_player=nil,player_sym=nil) - setup_board - @current_player = current_player || [:computer, :player].sample - choose_player_symbol(player_sym) - end - - def computer_symbol - @player_symbol[:computer] - end - - def player_symbol - @player_symbol[:player] - end - - def current_player - {:computer => "Computer", - :player => @player}[@current_player] - end - - def welcome_player - "Welcome #{@player}" - end - - def indicate_palyer_turn - puts "#{@player}'s Move:" - end - - def get_player_move - gets.chomp - end - - def player_move - move = get_player_move.to_sym - until open_spots.include?(move) - move = get_player_move.to_sym - end - @board[move] = player_symbol - @current_player = :computer - move - end - - def computer_move - move = get_computer_move - @board[move] = computer_symbol - @current_player = :player - move - end - - def get_computer_move - @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k}.sample - end - - def current_state - row1 = "#{@board[:A1]}|#{@board[:A2]}|#{@board[:A3]}\n" - row2 = "#{@board[:B1]}|#{@board[:B2]}|#{@board[:B3]}\n" - row3 = "#{@board[:C1]}|#{@board[:C2]}|#{@board[:C3]}\n" - row1 + "-"*row1.size+"\n"+ - row2 + "-"*row2.size+"\n"+ - row3 + "-"*row3.size+"\n"+ - "******" - end - - def determine_winner - p_spots = @board.select{|k,v| v==player_symbol} - c_spots = @board.select{|k,v| v==computer_symbol} - - player_spots = p_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } - computer_spots = c_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } - @player_win = false - @computer_win = false - [:A, :B, :C].each do |l| - return if @player_win = player_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] - return if @computer_win = computer_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] - end - - [1,2,3].each do |l| - return if @player_win = player_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] - return if @computer_win = computer_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] - end - - return if @player_win = p_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] - return if @player_win = p_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] - return if @computer_win = c_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] - return if @computer_win = c_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] - end - - def player_won? - !!@player_win - end - - def computer_won? - !!@computer_win - end - - def draw? - !player_won? && !computer_won? - end - - def over? - player_won? || computer_won? || !spots_open? - end - - def spots_open? - !open_spots.empty? - end - - def open_spots - @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k} - end - -private - def setup_board - @board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', - :B1 => ' ', :B2 => ' ', :B3 => ' ', - :C1 => ' ', :C2 => ' ', :C3 => ' '} - end - - def choose_player_symbol(player_sym=nil) - player_sym ||= SYMBOLS.sample - @player_symbol = { - :computer => SYMBOLS.reject{|s| s==player_sym}.first, - :player => player_sym - } - end + attr_accessor :player, :player_symbol, :computer_symbol, :board, :player_won, :computer_won + SYMBOLS = [:X, :O] + PLAYERS = [:computer, :player] + def initialize(first_player=nil, player_symbol=nil) + @current_player = nil + @players = [:player, :computer] + @player = "Anonymous" + @board = { + A1: " ", A2: " ", A3: " ", + B1: " ", B2: " ", B3: " ", + C1: " ", C2: " ", C3: " " + } + if first_player != nil + @current_player = first_player + else + random_first_turn + end + if player_symbol != nil + @player_symbol = player_symbol + @computer_symbol = (SYMBOLS-[@player_symbol])[0] + else + random_symbols + end + end + def welcome_player + "Welcome #{get_player_reference}" + end + def indicate_player_turn + puts "#{get_player_reference}'s Move:" + end + # This method returns what we call the player. + def get_player_reference + if @player.nil? + "Anonymous" + else + @player + end + end + def random_first_turn + @current_player = PLAYERS.shuffle[0] + end + def random_symbols + shuffled_symbols = SYMBOLS.shuffle + @player_symbol, @computer_symbol = shuffled_symbols[0], shuffled_symbols[1] + end + def current_player + if @current_player == :player + get_player_reference + else + "Computer" + end + end + def get_player_move + gets.chomp + end + def open_spots + @board.select { |k,v| v == " "} + end + def computer_move + move = open_spots.keys.sample + next_player(:player) + process_move(:computer, move) + end + def player_move + move = 'false' + until validate_move?(move.to_sym) + move = get_player_move.to_sym + end + next_player(:computer) + process_move(:player,move) + end + + # This validates a move, registers it, and flips the next turn bit + def process_move(player, move) + return 1 unless validate_move?(move) + @board[move] = @computer_symbol if player == :computer + @board[move] = @player_symbol if player == :player + move + end + # This checks to see if the move falls upon an open spot + def validate_move?(move) + return true if open_spots.include?(move) + false + end + def next_player(player) + @current_player = player + # @current_player = (PLAYERS - [@current_player])[0] + end + def current_state + output = '' + @board.select {|k,v| k.to_s =~ /A/}.each do |k,v| + output << v.to_s unless v == " " + output << " " if v == " " + output << " | " + end + output << "\n _ _ _ \n" + @board.select {|k,v| k.to_s =~ /B/}.each do |k,v| + output << v.to_s unless v == " " + output << " " if v == " " + output << " | " + end + output << "\n _ _ _ \n" + @board.select {|k,v| k.to_s =~ /C/}.each do |k,v| + output << v.to_s unless v == " " + output << " " if v == " " + output << " | " + end + output + # @board + end + def determine_winner + victory = false + @player_won = false + @computer_won = false + if @board[:A1] == @board[:A2] && @board[:A2] == @board[:A3] && @board[:A1] != " " + victory = true + winner_symbol = @board[:A1] + elsif @board[:B1] == @board[:B2] && @board[:B2] == @board[:B3] && @board[:B1] != " " + victory = true + winner_symbol = @board[:B1] + elsif @board[:C1] == @board[:C2] && @board[:C2] == @board[:C3] && @board[:C1] != " " + victory = true + winner_symbol = @board[:C1] + elsif @board[:A1] == @board[:B1] && @board[:B1] == @board[:C1] && @board[:A1] != " " + victory = true + winner_symbol = @board[:A1] + elsif @board[:A2] == @board[:B2] && @board[:B2] == @board[:C2] && @board[:A2] != " " + victory = true + winner_symbol = @board[:A2] + elsif @board[:A3] == @board[:B3] && @board[:B3] == @board[:C3] && @board[:A3] != " " + victory = true + winner_symbol = @board[:A3] + elsif @board[:A1] == @board[:B2] && @board[:B2] == @board[:C3] && @board[:A1] != " " + victory = true + winner_symbol = @board[:A1] + elsif @board[:A3] == @board[:B2] && @board[:B2] == @board[:C1] && @board[:A3] != " " + victory = true + winner_symbol = @board[:A3] + elsif open_spots.empty? + draw + else + victory = false + end + if victory == true && winner_symbol == @player_symbol + @player_won = true + return true + elsif victory == true && winner_symbol == @computer_symbol + @computer_won = true + return true + end + victory + end + def player_won? + return true if @player_won + false + end + def computer_won? + return true if @computer_won + false + end + def spots_open? + return true unless open_spots.empty? + false + end + def over? + return true if player_won? || computer_won? || draw? + false + end + def draw + @draw = true + end + def draw? + true if @draw + end +# class TicTacToe +# SYMBOLS = [:X,:O] +# attr_accessor :player, :board + +# def initialize(current_player=nil,player_sym=nil) +# setup_board +# @current_player = current_player || [:computer, :player].sample +# choose_player_symbol(player_sym) +# end + +# def computer_symbol +# @player_symbol[:computer] +# end + +# def player_symbol +# @player_symbol[:player] +# end + +# def current_player +# {:computer => "Computer", +# :player => @player}[@current_player] +# end + +# def welcome_player +# "Welcome #{@player}" +# end + +# def indicate_palyer_turn +# puts "#{@player}'s Move:" +# end + +# def get_player_move +# gets.chomp +# end + +# def player_move +# move = get_player_move.to_sym +# until open_spots.include?(move) +# move = get_player_move.to_sym +# end +# @board[move] = player_symbol +# @current_player = :computer +# move +# end + +# def computer_move +# move = get_computer_move +# @board[move] = computer_symbol +# @current_player = :player +# move +# end + +# def get_computer_move +# @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k}.sample +# end + +# def current_state +# row1 = "#{@board[:A1]}|#{@board[:A2]}|#{@board[:A3]}\n" +# row2 = "#{@board[:B1]}|#{@board[:B2]}|#{@board[:B3]}\n" +# row3 = "#{@board[:C1]}|#{@board[:C2]}|#{@board[:C3]}\n" +# row1 + "-"*row1.size+"\n"+ +# row2 + "-"*row2.size+"\n"+ +# row3 + "-"*row3.size+"\n"+ +# "******" +# end + +# def determine_winner +# p_spots = @board.select{|k,v| v==player_symbol} +# c_spots = @board.select{|k,v| v==computer_symbol} + +# player_spots = p_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } +# computer_spots = c_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } +# @player_win = false +# @computer_win = false +# [:A, :B, :C].each do |l| +# return if @player_win = player_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] +# return if @computer_win = computer_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] +# end + +# [1,2,3].each do |l| +# return if @player_win = player_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] +# return if @computer_win = computer_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] +# end + +# return if @player_win = p_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] +# return if @player_win = p_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] +# return if @computer_win = c_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] +# return if @computer_win = c_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] +# end + +# def player_won? +# !!@player_win +# end + +# def computer_won? +# !!@computer_win +# end + +# def draw? +# !player_won? && !computer_won? +# end + +# def over? +# player_won? || computer_won? || !spots_open? +# end + +# def spots_open? +# !open_spots.empty? +# end + +# def open_spots +# @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k} +# end + +# private +# def setup_board +# @board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', +# :B1 => ' ', :B2 => ' ', :B3 => ' ', +# :C1 => ' ', :C2 => ' ', :C3 => ' '} +# end + +# def choose_player_symbol(player_sym=nil) +# player_sym ||= SYMBOLS.sample +# @player_symbol = { +# :computer => SYMBOLS.reject{|s| s==player_sym}.first, +# :player => player_sym +# } +# end end diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index cf7847f..f64b949 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -8,7 +8,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..0daa578 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,32 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + +method_missing is method that returns a boolean depending on weather an object will +respond to a certain method. It can be used to detect if a specific object will work for +whatever methods you want to run (for instance, instead of checking to see if something is an array, instead see if it responds to .each?, which means you can use a file object as well.) + + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +Eigenclass is synonymous to 'singleton class' in Ruby, and is an anonymous class. They contain singleton methods for a specific object. + 3. When would you use DuckTypeing? How would you use it to improve your code? + +DuckTyping is good for cases where you don't need a specific object "class" but rather +a supported method. For instance, instead of requiring an array object, you can instead check to see if it supports something like .each, and then you can support arrays, hashes, I/O, etc. etc. They're more flexible and refactoring/testing is easier. + + 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 acted upon a specific class without instantiating it first; such as File.open. + +Instance methods are ran against instantiated objects, like File.new (class method) and then File.each (instance method). + +`instance_eval` and `class_eval` both set `self` to whatever object you so choose, run the code inside the block, and then reset `self`. + +However, `class_eval` sets an instance method, whereas instance_eval will set a class_method. So it's kind of backwards opposite day. + 5. What is the difference between a singleton class and a singleton method? + +A singleton method is a per-object method. A singleton class is the anonymous class that holds singleton methods for a particular object. diff --git a/week7/homework/tic-tac-toe_old.rb b/week7/homework/tic-tac-toe_old.rb new file mode 100644 index 0000000..4bcfe98 --- /dev/null +++ b/week7/homework/tic-tac-toe_old.rb @@ -0,0 +1 @@ +d diff --git a/week8/class_materials/meta_example.rb b/week8/class_materials/meta_example.rb new file mode 100644 index 0000000..3b16a9f --- /dev/null +++ b/week8/class_materials/meta_example.rb @@ -0,0 +1,46 @@ +class Book +end + + +b = Book.new +b1 = Book.new + +b.instance_eval do + def cheese + puts "cheesy" + end +end + +b.cheese +# b1.cheese <= this should error out +class Object + def call_chain + "self" + end +end + +module NamedThing + def call_chain + "#{NamedThing.super}" + end +end + +module Speaker + def call_chain + "#{self}.#{super}" + end +end + +class Animal + def call_chain + "#{self}.#{super}" + end +end + +class Person < Animal + include NamedThing + include Speaker + def call_chain + "#{self}.#{super}" + end +end \ No newline at end of file diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index 0f85a23..6f71c72 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -1,9 +1,31 @@ class Couch - def initialize(pillows, cushions) - @pillows = pillows - @cushions = cushions - end + def initialize(pillows, cushions) + @pillows = pillows + @cushions = cushions + end +## nikky + # def pillow_colors + # @pillows.join(", ") + # end + # def cushion_colors + # @cushions.join(", ") + # end + def method_missing(method, *args, &block) + puts "You called #{method} with #{args.join(' ')}" + end + def to_str + "I am a couch" + end + [:pillows, :cushions].each do |s| + define_method("how_many_#{s}") do + instance_variable_get("@#{s}").count + end + define_method("#{s.to_s.chomp('s')}_colors") do + instance_variable_get("@#{s}").join(', ') + end + end +## class [:pillows, :cushions].each do |s| define_method("how_many_#{s}") do