0

Ruby newbie here working on loops with classes. I was supposed create a method that would take a string and add exclamation points to the end of each word (by making it an array with .split) and join the 'exclaimed' words as a string again. I've been at this for two hours already and decided I should seek help. I have a handful of ideas but I keep coming up with a NoMethod error. Below is one of ways that made sense to me but of course, it doesn't work. I've also added specs at the very end.

class StringModifier
  attr_accessor :string
  def initialize(string)
    @string = string
  end

  def proclaim
    new_array = []
    string.split.each do |word|
      new array = "#{word}!"
        new_array.join
    end
    new_array
  end
end

SPECS

describe StringModifier do
  describe "#proclaim" do
    it "adds an exclamation mark after each word" do
      blitzkrieg_bop = StringModifier.new("Hey ho let's go").proclaim
      expect(blitzkrieg_bop).to eq("Hey! ho! let's! go!")
    end
  end
end
1
  • new array = "#{word}!" is a typo? It should be new_array = "#{word}!" Commented Feb 21, 2015 at 18:47

1 Answer 1

1

Write your method as:

def proclaim
  string.split.map { |word| "#{word}!" }.join(" ")
end

Or write it as :

def proclaim
  a = string.split
  ("%s! " * a.size % a).strip
end

Tested :

[30] pry(main)> a = "Hey ho let's go".split
=> ["Hey", "ho", "let's", "go"]
[31] pry(main)> ("%s! " * a.size % a).strip
=> "Hey! ho! let's! go!"
[32] pry(main)>
Sign up to request clarification or add additional context in comments.

3 Comments

.join will join without space, you should do a .join(' ')
@MohammadAbuShady Thanks... Friend.. Overconfidence killing me :)
I was gonna answer the same answer my self, I wouldn't have noticed if i hadn't tested it lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.