Forum: Ruby Help on this Method

Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-10-04 10:57
-------------------------------------------------------
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)
6c7fb11d4f3fa354a51a3ea0375d895f?d=identicon&s=25 Joe Gain (timeout)
on 2016-10-04 14:47
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:

https://ruby-doc.org/core-2.3.0/Array.html#method-i-sort_by

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.
150e3d60e1da33d0ef45a3fab66d6019?d=identicon&s=25 B. Onzo (bonzo_goat)
on 2016-10-04 15:04
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:
>
> https://ruby-doc.org/core-2.3.0/Array.html#method-i-sort_by
>
> 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)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
150e3d60e1da33d0ef45a3fab66d6019?d=identicon&s=25 B. Onzo (bonzo_goat)
on 2016-10-04 15:06
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:
>>
>> https://ruby-doc.org/core-2.3.0/Array.html#method-i-sort_by
>>
>> 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
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-10-04 17:47
The problem is still the same when i tried to
1.include Enumerable
2.sort_by!
3.sort!
4.sort
6c7fb11d4f3fa354a51a3ea0375d895f?d=identicon&s=25 Joe Gain (timeout)
on 2016-10-04 18:17
B. Onzo is totally right! My bad. Here was I thinking you need to read
the Error message?! :D

(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).
Be955f2578b8971802ceab589fe24ad3?d=identicon&s=25 Suresh Ilankovan (sureshhey)
on 2016-10-04 18:45
Joe Gain wrote in post #1185128:
> B. Onzo is totally right! My bad. Here was I thinking you need to read
> the Error message?! :D
>
> (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
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2016-10-05 15:56
> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.