Help on this Method


class Hello
def self.best_sentence(sentences,desired_words)
ranked_sentences = sentences.sort_by do |s|
s.words.length - (s.downcase.words - desired_words).length
end
ranked_sentences.last
end

def words
scan(/\w[\w’-]*/)
end

end

puts Hello.best_sentence("[This is a test, This is a ruby
test]",%q{ruby test})

The error always prompting me an undefined method `sort_by’ for “[This
is a test, This is a ruby test]”:String (NoMethodError)

Hello Suresh,

this is because Array#sort_by is undefined. Which means there is no
method sort_by in the Array class. You can check this by looking at the
documentation for Array:

There is however, a method Array#sort_by! (The exclamation mark is
important.) The exclamation mark means (by convention) that a method
changes the state of the object, which is another way of saying that it
changes the values of (at least) one of the object’s variables.

B. Onzo wrote in post #1185125:

Joe Gain wrote in post #1185124:

Hello Suresh,

this is because Array#sort_by is undefined. Which means there is no
method sort_by in the Array class. You can check this by looking at the
documentation for Array:

Class: Array (Ruby 2.3.0)

There is however, a method Array#sort_by! (The exclamation mark is
important.) The exclamation mark means (by convention) that a method
changes the state of the object, which is another way of saying that it
changes the values of (at least) one of the object’s variables.

sort_by comes from Enumerable, but that has nothing to do
with the problem at hand.

The problem is actually given in the error message:

undefined method `sort_by’ for “[This is a test, This is a ruby
test]”:String (NoMethodError)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sorry, wanted to underline the pertinent part:

“[This is a test, This is a ruby test]”:String

The problem is still the same when i tried to
1.include Enumerable
2.sort_by!
3.sort!
4.sort

Joe Gain wrote in post #1185124:

Hello Suresh,

this is because Array#sort_by is undefined. Which means there is no
method sort_by in the Array class. You can check this by looking at the
documentation for Array:

Class: Array (Ruby 2.3.0)

There is however, a method Array#sort_by! (The exclamation mark is
important.) The exclamation mark means (by convention) that a method
changes the state of the object, which is another way of saying that it
changes the values of (at least) one of the object’s variables.

sort_by comes from Enumerable, but that has nothing to do
with the problem at hand.

The problem is actually given in the error message:

undefined method `sort_by’ for “[This is a test, This is a ruby
test]”:String (NoMethodError)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

B. Onzo is totally right! My bad. Here was I thinking you need to read
the Error message?! :smiley:

(Actually, I was a bit distracted by what was going on in the block,
calling Hello#word all the time.)

The syntax error is due to you trying to call an Array method on a
String object.

This is an Array of String(s):

sentences = []
sentences[0] = “I like ruby”
sentences[1] = “You like ruby”
sentences[2] = “We all like ruby”

Or, more commonly

more_sentences = [ “This that and”, “the other” ]

So, now you have your Array and you can use all the Array methods to do
stuff with the “sentences” (which are Strings).

Joe Gain wrote in post #1185128:

B. Onzo is totally right! My bad. Here was I thinking you need to read
the Error message?! :smiley:

(Actually, I was a bit distracted by what was going on in the block,
calling Hello#word all the time.)

The syntax error is due to you trying to call an Array method on a
String object.

This is an Array of String(s):

sentences = []
sentences[0] = “I like ruby”
sentences[1] = “You like ruby”
sentences[2] = “We all like ruby”

Or, more commonly

more_sentences = [ “This that and”, “the other” ]

So, now you have your Array and you can use all the Array methods to do
stuff with the “sentences” (which are Strings).

now they are saying this
undefined method `words’ for “I like ruby”:String (NoMethodError)

which i place the method words inside the Hello class

undefined method `words’ for “I like ruby”:String (NoMethodError)
which i place the method words inside the Hello class

Well - if the method words() resides in class Hello, then it is
not available on class String.

You need to make sure to call .words() only on objects of class
Hello then - have a look why you have a String object when you
instead expect a Hello object.