Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Final and Week 7 homework #114

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
299dff9
Update roster.txt
keviny22 Jan 15, 2014
1a04086
first commit
keviny22 Jan 16, 2014
c40b459
finished spec
keviny22 Jan 16, 2014
5fa0652
Merge remote-tracking branch 'upstream/master'
keviny22 Jan 17, 2014
f8888bd
Merge remote-tracking branch 'upstream/master'
keviny22 Jan 23, 2014
8b442af
week2
keviny22 Jan 23, 2014
3e66047
week2_fixed spaces
keviny22 Jan 23, 2014
b943c46
Merge remote-tracking branch 'upstream/master'
keviny22 Jan 24, 2014
d00e921
Merge remote-tracking branch 'upstream/master'
keviny22 Jan 31, 2014
3a7c71e
homework
keviny22 Feb 6, 2014
faff9cb
homework
keviny22 Feb 6, 2014
a821c83
Merge remote-tracking branch 'upstream/master'
keviny22 Feb 19, 2014
7ad4856
updates
keviny22 Feb 20, 2014
44272bb
updates
keviny22 Feb 20, 2014
de50733
updates
keviny22 Feb 21, 2014
2f64efb
updates
keviny22 Feb 21, 2014
47d5e9f
Merge remote-tracking branch 'upstream/master'
keviny22 Feb 21, 2014
872782a
Merge remote-tracking branch 'upstream/master'
keviny22 Mar 6, 2014
ae31454
updates
keviny22 Mar 7, 2014
c21262d
Final
keviny22 Mar 12, 2014
8780a9e
Final
keviny22 Mar 12, 2014
620b90d
updates
keviny22 Mar 12, 2014
8155e5f
updtes
keviny22 Mar 13, 2014
77d4184
updated_gemspec
keviny22 Mar 13, 2014
f13d274
updates
keviny22 Mar 13, 2014
2318f92
updates
keviny22 Mar 13, 2014
60a5433
Update roster.txt
keviny22 Jan 15, 2014
7fcb82b
first commit
keviny22 Jan 16, 2014
cfdfbc7
finished spec
keviny22 Jan 16, 2014
47fab1a
week2
keviny22 Jan 23, 2014
62bd3db
week2_fixed spaces
keviny22 Jan 23, 2014
84fc4fe
homework
keviny22 Feb 6, 2014
bb80d75
homework
keviny22 Feb 6, 2014
e7f4855
updates
keviny22 Feb 20, 2014
65f0d62
updates
keviny22 Feb 20, 2014
bf25d4d
updates
keviny22 Feb 21, 2014
dcf192e
updates
keviny22 Feb 21, 2014
148016e
updates
keviny22 Mar 7, 2014
3787c1b
Final
keviny22 Mar 12, 2014
9040fbb
Final
keviny22 Mar 12, 2014
8dde91d
updates
keviny22 Mar 12, 2014
647dabf
updtes
keviny22 Mar 13, 2014
66b196b
updated_gemspec
keviny22 Mar 13, 2014
c6973bd
updates
keviny22 Mar 13, 2014
00d6435
updates
keviny22 Mar 13, 2014
d42da6c
updates
keviny22 Mar 13, 2014
e0972ab
updates
keviny22 Mar 13, 2014
12fd8fc
fixed
keviny22 Mar 13, 2014
093640b
updates
keviny22 Mar 13, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.DS_Store
cookbooks
cookbook-reader.orig
cookbook-reader
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

1 change: 1 addition & 0 deletions .version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-2.1.0
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ Gem Project Requirements

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/UWE-Ruby/rubywinter2014/trend.png)](https://bitdeli.com/free "Bitdeli Badge")


7 changes: 7 additions & 0 deletions cookbook-reader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.gem
.bundle
pkg/*
coverage
*.swp
.idea/*
.DS_Store
1 change: 1 addition & 0 deletions cookbook-reader/.ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cookbook-reader
1 change: 1 addition & 0 deletions cookbook-reader/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.1.0
3 changes: 3 additions & 0 deletions cookbook-reader/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rvm:
- 2.1.0
script: "rake spec"
3 changes: 3 additions & 0 deletions cookbook-reader/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "http://rubygems.org"
gem 'rspec', :require => 'spec'
gemspec
20 changes: 20 additions & 0 deletions cookbook-reader/README.md
Original file line number Diff line number Diff line change
@@ -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}


7 changes: 7 additions & 0 deletions cookbook-reader/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rspec/core/rake_task'

desc 'Run specs'

RSpec::Core::RakeTask.new do |t|
t.rspec_opts = %w(--color)
end
5 changes: 5 additions & 0 deletions cookbook-reader/bin/cookbook-reader
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /usr/bin/ruby
require 'cookbook-reader/parser'
parser = CookbookReader::Parser.new(ARGV)
parser.parse
parser.display_data
23 changes: 23 additions & 0 deletions cookbook-reader/cookbook-reader.gemspec
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]'

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
37 changes: 37 additions & 0 deletions cookbook-reader/lib/cookbook-reader/options.rb
Original file line number Diff line number Diff line change
@@ -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
71 changes: 71 additions & 0 deletions cookbook-reader/lib/cookbook-reader/parser.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions cookbook-reader/spec/options_spec.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions cookbook-reader/spec/parser_spec.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions cookbook-reader/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
71 changes: 71 additions & 0 deletions midterm/answers.txt
Original file line number Diff line number Diff line change
@@ -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.


23 changes: 23 additions & 0 deletions midterm/even_number.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions midterm/even_number_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading