Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the necessary code: rubybasics2.rd # Strings and Regular Expressions # P

ID: 3744461 • Letter: H

Question

Here is the necessary code:

rubybasics2.rd

# Strings and Regular Expressions

# Part I
def hello(name)
# YOUR CODE HERE
end

# Part II
def starts_with_consonant? s
# YOUR CODE HERE
end

# Part III
def binary_multiple_of_4? s
# YOUR CODE HERE
end

rubybasics2_spec.rb

require 'rubybasics2.rb'

RSpec.configure do |config|
config.filter_run_excluding :disabled => true
end

describe "#hello" do
it "should be defined" do
expect { hello("Testing") }.not_to raise_error()#::NoMethodError)
end

it "The hello method returns the correct string [30 points]" , points: 30 do
expect(hello("Dan").class).to eq(String)
expect(hello("Dan")).to eq('Hello, Dan'), "Incorrect results for input: "Dan""
expect(hello("BILL")).to eq('Hello, BILL'), "Incorrect results for input: "BILL""
expect(hello("Mr. Ilson")).to eq('Hello, Mr. Ilson'), "Incorrect results for input: "Mr. Ilson""
end
end

describe "#starts_with_consonant?", :disabled => true do
it "should be defined" do
expect { starts_with_consonant?("d") }.not_to raise_error()#::NoMethodError)
end
it 'classifies true cases [10 points]' , points: 10 do
expect(starts_with_consonant?('v')).to be_truthy, "'v' is a consonant"
['v', 'vest', 'Veeee', 'crypt'].each do |string|
expect(starts_with_consonant?(string)).to be_truthy, "Incorrect results for input: "#{string}""
end
end
it 'classifies false cases [10 points]' , points: 10 do
expect(starts_with_consonant?('a')).to be_falsy, "'a' is not a consonant"
['asdfgh', 'Unix'].each do |string|
expect(starts_with_consonant?(string)).to be_falsy, "Incorrect results for input: "#{string}""
end
end
it 'works on the empty string [5 points]' , points: 5 do
expect(starts_with_consonant?('')).to be_falsy
end
it 'works on nonletters [5 points]' , points: 5 do
expect(starts_with_consonant?('#foo')).to be_falsy
end
end

describe "#binary_multiple_of_4?", :disabled => true do
it "should be defined" do
expect { binary_multiple_of_4?("yes") }.not_to raise_error()#::NoMethodError)
end
it "classifies valid binary numbers [30 points]" , points: 30 do
["1010101010100", "0101010101010100", "100", "0"].each do |string|
expect(binary_multiple_of_4?(string)).to be_truthy, "Incorrect results for input: "#{string}""
end
["101", "1000000000001"].each do |string|
expect(binary_multiple_of_4?(string)).not_to be_truthy, "Incorrect results for input: "#{string}""
end
end
it "rejects invalid binary numbers [10 points]" , points: 10 do
expect(binary_multiple_of_4?('a100')).to be_falsy, "'a100' is not a valid binary number!"
expect(binary_multiple_of_4?('')).to be_falsy, "The empty string is not a valid binary number!"
end
end

Questions:

1. When you run rspec, what will the test expect to return when it runs hello("Dan")?

Hint: Copy/paste directly from the file to avoid spelling errors! Be sure to include the quotes!

2. When you run rspec, what will the test print to the console if it fails the hello("Dan") equality test?

Hint: Copy/paste directly from the file to avoid spelling errors! Be sure to include the entire error message including the quotes but not the comma!

3. Look at the name method in the lib/rubybasics2.rb file.

Define a method hello(name) that takes a string representing a name and returns the string "Hello, " concatenated with the name.

Run rspec in the terminal window to test. Once everything is passing, upload your screenshot to this question.

4. When you run rspec, what will the test print to the console if it fails the test expect(starts_with_consonant?('v')).to be_truthy test?

Hint: Copy/paste directly from the file to avoid spelling errors! Be sure to include the entire error message including the quotes but not the comma or space!

5. Look at the starts_with_consonant? method in the lib/rubybasics2.rb file.

Define a method starts_with_consonant?(s) that takes a string and returns true if it starts with a consonant and false otherwise. (For our purposes, a consonant is any letter other than A, E, I, O, U.)

Note: Be sure it works for both upper and lower case and for nonletters!

Run rspec in the terminal window to test. Once everything is passing, upload your screenshot to this question.

7. Remove the , :disabled => true from the #binary_multiple_of_4? collection in the spec/rubybasics2_spec.rb file. Don't forget to save your changes!

Look at the binary_multiple_of_4? method in the lib/rubybasics2.rb file.

Define a method binary_multiple_of_4?(s) that takes a string and returns true if the string represents a binary number that is a multiple of 4.

Note: Be sure it returns false if the string is not a valid binary number!

Run rspec in the terminal window to test. Once everything is passing, upload your screenshot to this question.

Then, upload zipped project folder.

Explanation / Answer

Below is the code of 2 methods asked


def hello name
"Hello, " + name
end

def starts_with_consonant? s
!!(s[0] =~ /[bcdfghjklmnprstvwxyz]+/i)
end


def binary_multiple_of_4? s
if s =~ /^[0-1]+$/
return s.to_i(2) % 4 == 0 unless s == "0"
end
false
end