0

I am trying to write a very simple method in Ruby which takes a string and an array of words and checks if the string contains any of the words and if it does it replaces them with their uppercase.

I made an attempt but its not great due to my level of Ruby skills.

def(my_words,my_sentence)
  #split the sentence up into an array of words
  my_sentence_words =  my_sentence.split(/\W+/)
  #nested loop that checks the words array for each brand 
  my_sentence_words.each do |i|
    my_words.each do |i|
      #if it finds a brand in the words and sets them to be uppercase
      if my_words[i] == my_sentence_words[i]
        my_sentence_words[i] == my_sentence_words[i].up.case
      end
    end
  end

  #put the words array into one string
  words.each do |i|
    new_sentence = ("" + my_sentence_words[i]) + " "
  end
end

I am getting: can't convert string into integer error

4
  • For one thing you are using i twice, use j for the inner loop. Second, you didn't give your function a name. List the full error. Commented Dec 10, 2013 at 18:15
  • 2
    provide meaningful example data Commented Dec 10, 2013 at 18:18
  • 1
    "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." Commented Dec 10, 2013 at 18:22
  • As you gain experience with Ruby you'll find that you don't loop on indices nearly as often as you may be accustomed with C and the like. You'll be relying more on each and the numerous methods in the Enumerable "mixin" (module), nearly all of which are implemented with each. Commented Dec 10, 2013 at 19:25

3 Answers 3

3
def convert(mywords,sentence)
 regex = /#{mywords.join("|")}/i
 sentence.gsub(regex) { |m| m.upcase }
end
convert(%W{ john james jane }, "I like jane but prefer john")
#=> "I like JANE but prefer JOHN"
Sign up to request clarification or add additional context in comments.

2 Comments

Note convert(%W{ JANE }, "I like JANE") => "I like " and convert(%W{ JANE }, "I like jane") => "I like jane". You need to add /i and remove !.
Be careful with /#{mywords.join("|")}/i. It will create a pattern that allows sub-string matches, not whole-word matches. /\b#{mywords.join("|")}\b/i would be better.
3

This will work better. It loops through the brands, searches for each, and replaces with the uppercase version.

brands = %w(sony toshiba)
sentence = "This is a sony. This is a toshiba."

brands.each do |brand|
  sentence.gsub!(/#{brand}/i, brand.upcase)
end

Results in the string.

"This is a SONY. This is a TOSHIBA."

For those who like Ruby foo!

sentence.gsub!(/#{brands.join('|')}/i) { |b| b.upcase }

And in a function

def capitalize_brands(brands, sentence)
  sentence.gsub(/#{brands.join('|')}/i) { |b| b.upcase }
end

5 Comments

Thanks, also one other question for example if I am using this code in my rails app and wanted to take a string and replace a wordd with a link to a url so it would go to brands/sony etc. How could I put a link inside of a string?
Just use link_to b.upcase, 'YOUR_URL'
mutt, does link_to require a require?
It shouldn't if you are using it in a helper.
It appears to be from ActionView::Helpers, which I presume is used with Rails. That's what I meant about a require, as the question is not tagged with Rails.
0

You get this error because i doesn't start from 0 as you expected, in each method i is an element of array, and has string type, it's a first word from your sentence:

my_sentence_words = ["word"]
my_sentence_words.each do |i|
  puts i.length #=> 4
  puts i.type   #=> String
  puts i        #=> word
end

So you try to call my_sentence_words[word] instead of my_sentence_words[0]. You can try method each_index that passes index of element instead of element itself`:

def check(str, *arr)
  upstr = str.split(' ')
  upstr.eachindex do |i|       #=> i is index
    arr.each_index do  |j|  
      upstr[i].upcase! if upstr[i] == arr[j]        
    end
  end
  upstr
end

check("This is my sentence", "day", "is", "goal", "may", "my")
#=>["This", "IS", "MY", "sentence"]

Comments

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.