Skip to content

Commit

Permalink
remove hash_ and move specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bpurinton committed Feb 8, 2023
1 parent 7ba0627 commit 627bfde
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 98 deletions.
28 changes: 20 additions & 8 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
{
"editor.tabSize": 2,
"emmet.includeLanguages": {
"erb": "html"
},
"files.associations": {
"*.erb": "erb"
}
}
"editor.tabSize": 2,
"emmet.includeLanguages": {
"erb": "html"
},
"files.associations": {
"*.erb": "erb"
},
"files.exclude": {
"**/.git": true,
".vscode": true,
".bundle": true,
".*": true,
"*.ru": true,
"Dockerfile": true,
"Gem*": true,
"install-packages": true,
"main.rb": true,
"README.md": true,
},
}
37 changes: 1 addition & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@
# Ruby Practice

1. Open a file (e.g. `hash_person.rb`) in the editor window.
1. Modify the file per the instructions on top.
1. Run your Ruby file by typing `ruby ` and then the name of the file you want to run in the terminal. If we want to run `hash_person.rb`, we can write the command:

```bash
ruby hash_person.rb
```

Remember, if there are multiple files with similar names, start typing the name and then just press <kbd>Tab</kbd> on your keyboard to let the terminal complete the name. You rarely need to type full filenames out — use **tab completion**!

1. To re-run this command, you can use the <kbd>Up ↑</kbd> and <kbd>Down ↓</kbd> arrow keys to look at the history of commands you've run in a terminal.
1. When you think you have the required output, run `rails grade` and proceed when the test passes without errors.
If you are struggling, **try to experiment directly in the IRB environment** by typing `irb` into the terminal and pressing enter. This will start an interactive Ruby terminal, where you can enter individual lines of Ruby to see their output. If you start `irb` then the terminal will no longer be in the `bash` environment so things like `rails grade` won't work. You will need to open a second terminal with the plus (+) icon and switch between the `irb` and `bash` terminals as needed. Alternatively type `exit` at the IRB terminal prompt to return to the `bash` environment.

## Specs
<details>
<summary>Click here to see names of each test</summary>

hash_person.rb prints Hash of '{:name => "Osi", :age => 24, :occupation => "Songwriter"}', when input is 'Osi 24 Songwriter'

hash_person.rb prints Hash of '{:name => "Lia", :age => 32, :occupation => "Engineer"}', when input is 'Lia 32 Engineer'

hash_find_value.rb prints Hash of 'Could not find the integer 4', when input is '4'

hash_find_value.rb prints Hash of '100 is under the key: a.', when input is '100'

hash_find_value.rb prints Hash of '22 is under the key: e.', when input is '22'

hash_find_value.rb prints Hash of '32 is under the key: e.', when input is '32'

hash_dig.rb prints '80' by retriving the value from the Hash

hash_list.rb prints 'James', 'Yolanda', 'Red', and 'Fatimah' using variables, loops, if statements, and Hash methods'
</details>
This project is associated with the [`Hash` chapter](https://firstdraft.github.io/appdev-textbook/hash-chapter.html). The steps to open and run a GitPod project can be found [here](https://firstdraft.github.io/appdev-textbook/string.html#start-gitpod-project).
9 changes: 8 additions & 1 deletion hash_dig.rb → dig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@

#### SOLUTION:

# p sample_hash.fetch(:class).fetch(:student).fetch("marks").fetch("history")
# p sample_hash.fetch(:class).fetch(:student).fetch("marks").fetch("history")


# ~~~~~ Specs (make it do these things) ~~~~~
#
# dig.rb prints '80' by retriving the value from the Hash
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 changes: 13 additions & 0 deletions hash_find_value.rb → find_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@
sample_hash = {:a => 100, :b => 200, :c => 300, :d => 400, :e => rand(200), :f => 600, :g => 0 }

p "Enter an integer to find:"


# ~~~~~ Specs (make it do these things) ~~~~~
#
# find_value.rb prints Hash of 'Could not find the integer 4', when input is '4'
#
# find_value.rb prints Hash of '100 is under the key: a.', when input is '100'
#
# find_value.rb prints Hash of '22 is under the key: e.', when input is '22'
#
# find_value.rb prints Hash of '32 is under the key: e.', when input is '32'
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 changes: 6 additions & 0 deletions hash_list.rb → list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@
{ :name => "Carl", :age => 9 },
]


# ~~~~~ Specs (make it do these things) ~~~~~
#
# list.rb prints 'James', 'Yolanda', 'Red', and 'Fatimah' using variables, loops, if statements, and Hash methods'
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 changes: 9 additions & 0 deletions hash_person.rb → person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@
# Make sure the value of the :age key is an Integer

p "Enter a name, age, and occupation separated by spaces:"


# ~~~~~ Specs (make it do these things) ~~~~~
#
# person.rb prints Hash of '{:name => "Osi", :age => 24, :occupation => "Songwriter"}', when input is 'Osi 24 Songwriter'
#
# person.rb prints Hash of '{:name => "Lia", :age => 32, :occupation => "Engineer"}', when input is 'Lia 32 Engineer'
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106 changes: 53 additions & 53 deletions spec/scripts/hash_spec.rb
Original file line number Diff line number Diff line change
@@ -1,178 +1,178 @@

describe "hash_person.rb" do
describe "person.rb" do

it "prints Hash of '{:name => \"Osi\", :age => 24, :occupation => \"Songwriter\"}', when input is 'Osi 24 Songwriter'", points: 1 do
# Un-require hash_person.rb
hash_person = $".select{|r| r.include? 'hash_person.rb'}
$".delete(hash_person.first)
# Un-require person.rb
person = $".select{|r| r.include? 'person.rb'}
$".delete(person.first)

allow_any_instance_of(Object).to receive(:gets).and_return("Osi 24 Songwriter\n")

response = /\{:name=>"Osi", :age=>24, :occupation=>"Songwriter"\}/

output = with_captured_stdout { require_relative('../../hash_person')}
output = with_captured_stdout { require_relative('../../person')}
output = "empty" if output.empty?
expect(output.match?(response)).to be(true),
"Expected output to be 'Enter a name, age, and occupation separated by spaces:\\n{:name=>\"Osi\", :age=>24, :occupation=>\"Songwriter\"}', but was #{output}."
end
end


describe "hash_person.rb" do
describe "person.rb" do

it "prints Hash of '{:name => \"Lia\", :age => 32, :occupation => \"Engineer\"}', when input is 'Lia 32 Engineer'", points: 1 do
# Un-require hash_person.rb
hash_person = $".select{|r| r.include? 'hash_person.rb'}
$".delete(hash_person.first)
# Un-require person.rb
person = $".select{|r| r.include? 'person.rb'}
$".delete(person.first)

allow_any_instance_of(Object).to receive(:gets).and_return("Lia 32 Engineer\n")

response = /\{:name=>"Lia", :age=>32, :occupation=>"Engineer"\}/

output = with_captured_stdout { require_relative('../../hash_person')}
output = with_captured_stdout { require_relative('../../person')}
output = "empty" if output.empty?
expect(output.match?(response)).to be(true),
"Expected output to be 'Enter a name, age, and occupation separated by spaces:\\n{:name=>\"Lia\", :age=>32, :occupation=>\"Engineer\"}', but was #{output}."
end
end

describe "hash_find_value.rb" do
describe "find_value.rb" do

it "prints Hash of 'Could not find the integer 4', when input is '4'", points: 1 do
# Un-require hash_find_value.rb
hash_find_value = $".select{|r| r.include? 'hash_find_value.rb'}
$".delete(hash_find_value.first)
# Un-require find_value.rb
find_value = $".select{|r| r.include? 'find_value.rb'}
$".delete(find_value.first)

allow_any_instance_of(Object).to receive(:gets).and_return("4\n")
allow_any_instance_of(Kernel).to receive(:rand).and_return(2)

response = /Could not find the integer 4/i

output = with_captured_stdout { require_relative('../../hash_find_value')}
output = with_captured_stdout { require_relative('../../find_value')}
output = "empty" if output.empty?
expect(output.match?(response)).to be(true),
"Expected output to be 'Enter an integer to find:\\nCould not find the integer 4', but was #{output}."
end
end

describe "hash_find_value.rb" do
describe "find_value.rb" do

it "prints Hash of '100 is under the key: a.', when input is '100'", points: 1 do
# Un-require hash_find_value.rb
hash_find_value = $".select{|r| r.include? 'hash_find_value.rb'}
$".delete(hash_find_value.first)
# Un-require find_value.rb
find_value = $".select{|r| r.include? 'find_value.rb'}
$".delete(find_value.first)

allow_any_instance_of(Object).to receive(:gets).and_return("100\n")
allow_any_instance_of(Kernel).to receive(:rand).and_return(12)

response = /100 is under the key: a/i

# expect { require_relative("../../hash_find_value") }.to output(response).to_stdout
output = with_captured_stdout { require_relative('../../hash_find_value')}
# expect { require_relative("../../find_value") }.to output(response).to_stdout
output = with_captured_stdout { require_relative('../../find_value')}
output = "empty" if output.empty?
expect(output.match?(response)).to be(true),
"Expected output to be 'Enter an integer to find:\\n100 is under the key: a', but was #{output}."
end
end

describe "hash_find_value.rb" do
describe "find_value.rb" do

it "prints Hash of '22 is under the key: e.', when input is '22'", points: 1 do
# Un-require hash_find_value.rb
hash_find_value = $".select{|r| r.include? 'hash_find_value.rb'}
$".delete(hash_find_value.first)
# Un-require find_value.rb
find_value = $".select{|r| r.include? 'find_value.rb'}
$".delete(find_value.first)

allow_any_instance_of(Object).to receive(:gets).and_return("22\n")
allow_any_instance_of(Kernel).to receive(:rand).and_return(22)

response = /22 is under the key: e/i

# expect { require_relative("../../hash_find_value") }.to output(response).to_stdout
output = with_captured_stdout { require_relative('../../hash_find_value')}
# expect { require_relative("../../find_value") }.to output(response).to_stdout
output = with_captured_stdout { require_relative('../../find_value')}
output = "empty" if output.empty?
expect(output.match?(response)).to be(true),
"Expected output to be 'Enter an integer to find:\\n22 is under the key: e', but was #{output}."
end
end

describe "hash_find_value.rb" do
describe "find_value.rb" do

it "prints Hash of '32 is under the key: e.', when input is '32'", points: 1 do
# Un-require hash_find_value.rb
hash_find_value = $".select{|r| r.include? 'hash_find_value.rb'}
$".delete(hash_find_value.first)
# Un-require find_value.rb
find_value = $".select{|r| r.include? 'find_value.rb'}
$".delete(find_value.first)

allow_any_instance_of(Object).to receive(:gets).and_return("32\n")
allow_any_instance_of(Kernel).to receive(:rand).and_return(32)

response = /32 is under the key: e/i

# expect { require_relative("../../hash_find_value") }.to output(response).to_stdout
output = with_captured_stdout { require_relative('../../hash_find_value')}
# expect { require_relative("../../find_value") }.to output(response).to_stdout
output = with_captured_stdout { require_relative('../../find_value')}
output = "empty" if output.empty?
expect(output.match?(response)).to be(true),
"Expected output to be 'Enter an integer to find:\\n32 is under the key: e', but was #{output}."
end
end


describe "hash_dig.rb" do
describe "dig.rb" do

it "prints '80' by retriving the value from the Hash", points: 1 do
dig_file = "hash_dig.rb"
dig_file = "dig.rb"
file_contents = File.read(dig_file)
File.foreach(dig_file).with_index do |line, line_num|
if (line.match(/\s*p.{0,2}80/i) || line.match(/\s*puts.{0,2}80/i))

expect(line).to_not match(/\s*p.{0,2}80/),
"Expected 'hash_dig.rb' to NOT literally print '80', but did anyway."
"Expected 'dig.rb' to NOT literally print '80', but did anyway."
expect(line).to_not match(/\s*puts.{0,2}80/),
"Expected 'hash_dig.rb' to NOT literally print '80', but did anyway."
"Expected 'dig.rb' to NOT literally print '80', but did anyway."
end
end

# Un-require hash_dig.rb
hash_dig = $".select{|r| r.include? 'hash_dig.rb'}
$".delete(hash_dig.first)
# Un-require dig.rb
dig = $".select{|r| r.include? 'dig.rb'}
$".delete(dig.first)

response = /80/

# expect { require_relative("../../hash_dig") }.to output(response).to_stdout
output = with_captured_stdout { require_relative('../../hash_dig')}
# expect { require_relative("../../dig") }.to output(response).to_stdout
output = with_captured_stdout { require_relative('../../dig')}
output = "empty" if output.empty?
expect(output.match?(response)).to be(true),
"Expected output to be '80', but was #{output}."

end
end

describe "hash_list.rb" do
describe "list.rb" do

it "prints 'James', 'Yolanda', 'Red', and 'Fatimah' using variables, loops, if statements, and Hash methods'", points: 1 do
list_file = "hash_list.rb"
list_file = "list.rb"
file_contents = File.read(list_file)
File.foreach(list_file).with_index do |line, line_num|
if !line.include?(":name") && (line.match(/\s*p.{0,2}(james|yolanda|red|fatimah)/i) || line.match(/\s*puts.{0,2}(james|yolanda|red|fatimah)/i))

expect(line).to_not match(/James/i),
"Expected 'hash_list.rb' to NOT literally print 'James', but did anyway."
"Expected 'list.rb' to NOT literally print 'James', but did anyway."
expect(line).to_not match(/Yolanda/i),
"Expected 'hash_list.rb' to NOT literally print 'Yolanda', but did anyway."
"Expected 'list.rb' to NOT literally print 'Yolanda', but did anyway."
expect(line).to_not match(/Red/i),
"Expected 'hash_list.rb' to NOT literally print 'Red', but did anyway."
"Expected 'list.rb' to NOT literally print 'Red', but did anyway."
expect(line).to_not match(/Fatimah/i),
"Expected 'hash_list.rb' to NOT literally print 'Fatimah', but did anyway."
"Expected 'list.rb' to NOT literally print 'Fatimah', but did anyway."
end
end

# Un-require hash_list.rb
hash_list = $".select{|r| r.include? 'hash_list.rb'}
$".delete(hash_list.first)
# Un-require list.rb
list = $".select{|r| r.include? 'list.rb'}
$".delete(list.first)

response = /.?James.?\n.?Yolanda.?\n.?Red.?\n.?Fatimah.?\n/i

# expect { require_relative("../../hash_list") }.to output(response).to_stdout
output = with_captured_stdout { require_relative('../../hash_list')}
# expect { require_relative("../../list") }.to output(response).to_stdout
output = with_captured_stdout { require_relative('../../list')}
output = "empty" if output.empty?
expect(output.match?(response)).to be(true),
"Expected output to be 'James\nYolanda\nRed\nFatimah\n', but was #{output}."
Expand Down

0 comments on commit 627bfde

Please sign in to comment.