Why is the method not found

<meta http-equiv="content-type" content="text/html; 

charset=ISO-8859-1">

Hello,

I try to solve this challenge :

Define a new instance method on the Array class called second, which returns the second item in an array (similar to the way .first and .last work in Ruby).

So I did this :

class Array

  def second
     self.at(1)
  end
 
end

With this test :

Test.assert_equals(4, Array.second([3,4,5]) )

but as soon as  I try the test I see this error:

': undefined method `second' for Array:Class (NoMethodError)

Which I find wierd because I made that method.

Roelof


Create new arrays after modify the class

irb(main):138:0> class Array
irb(main):139:1>
irb(main):140:1* def second
irb(main):141:2> self.at(1)
irb(main):142:2> end
irb(main):143:1>
irb(main):144:1* end
=> :second
irb(main):145:0> %w[1 2 3].second
=> “2”

already created Arrays should have not your custom methods

Thread name: “why is the method not found.”
Mail number: 1
Date: Fri, Dec 05, 2014
In reply to: Roelof W.

You didn’t define a class method, you defined an instance method.
You want to do:

[3,4,5].second

From: Roelof W. [email protected]
To: Ruby users [email protected]
Date: 12/05/2014 02:38 PM
Subject: why is the method not found.
Sent by: “ruby-talk” [email protected]

Hello,

I try to solve this challenge :

Define a new instance method on the Array class called second, which
returns the second item in an array (similar to the way .first and .last
work in Ruby).

So I did this :

class Array

def second
self.at(1)
end

end

With this test :

Test.assert_equals(4, Array.second([3,4,5]) )

but as soon as I try the test I see this error:

‘: undefined method `second’ for Array:Class (NoMethodError)

Which I find wierd because I made that method.

Roelof

The problem is with your test. You are calling it as a class method, not
an instance method.

Try something like this instead?

Test.assert_equals(“4", %w(3 4 5).second )


Sent from Mailbox

On Fri, Dec 5, 2014 at 8:40 PM, Lázaro Armando [email protected]
wrote:

irb(main):145:0> %w[1 2 3].second
=> “2”

already created Arrays should have not your custom methods

xi ~ % ruby -e ‘a = [1,2,3]; class Array; def second; self[1]; end;
end; p a.second’
2

%w(a b c) is same as [“a”, “b”, “c”]. it is an easy way to define
array of words.

Thanks,

Can someone explain what the %w does.

Roelof


Raj S. schreef op 5-12-2014 20:48:
The problem is with your test. You are calling it as a class method, not an instance method.

Try something like this instead?

Test.assert_equals(???4", %w(3 4 5).second )

???
Sent from Mailbox


On Fri, Dec 5, 2014 at 11:41 AM, L??zaro Armando <[email protected]> wrote:

Create new arrays after modify the class

irb(main):138:0> class Array
irb(main):139:1>
irb(main):140:1* def second
irb(main):141:2> self.at(1)
irb(main):142:2> end
irb(main):143:1>
irb(main):144:1* end
=> :second
irb(main):145:0> %w[1 2 3].second
=> "2"

already created Arrays should have not your custom methods



Thread name: "why is the method not found."
Mail number: 1
Date: Fri, Dec 05, 2014
In reply to: Roelof W.
>
> Hello,
>
> I try to solve this challenge :
>
> Define a new instance method on the Array class called second, which returns
> the second item in an array (similar to the way .first and .last work in Ruby).
>
> So I did this :
>
> class Array
>
> def second
> self.at(1)
> end
>
> end
>
> With this test :
>
> Test.assert_equals(4, Array.second([3,4,5]) )
>
> but as soon as I try the test I see this error:
>
> ': undefined method `second' for Array:Class (NoMethodError)
>
> Which I find wierd because I made that method.
>
> Roelof
>
>



irb(main):147:0> [“uno”,“dos”,“tres”]==%w[uno dos tres]
=> true

easies writing an array

Thread name: “Re: why is the method not found.”
Mail number: 5
Date: Fri, Dec 05, 2014
In reply to: Roelof W.