Why is the method not found

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
In reply to: Roelof Wobben

···

Date: Fri, Dec 05, 2014

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

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

[3,4,5].second

···

From: Roelof Wobben <r.wobben@home.nl>
To: Ruby users <ruby-talk@ruby-lang.org>
Date: 12/05/2014 02:38 PM
Subject: why is the method not found.
Sent by: "ruby-talk" <ruby-talk-bounces@ruby-lang.org>

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

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

···

On Fri, Dec 5, 2014 at 8:40 PM, Lázaro Armando <lazaro@hcg.sld.cu> 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

--
Michael Fellinger

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 <lazaro@hcg.sld.cu> 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 Wobben

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

Thanks,

  Can someone explain what the %w does.

  Roelof

  Raj Sahae 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](https://www.dropbox.com/mailbox)

On Fri, Dec 5, 2014 at 11:41 AM, Lázaro Armando lazaro@hcg.sld.cu > 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 Wobben

        >


        > 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
In reply to: Roelof Wobben

···

Date: Fri, Dec 05, 2014

Thanks,

Can someone explain what the %w does.

Roelof

Raj Sahae 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 <lazaro@hcg.sld.cu> 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 Wobben
        >
        > 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
        >
        >

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

···

On Sat, Dec 6, 2014 at 1:35 AM, Roelof Wobben <r.wobben@home.nl> wrote:

Thanks,

Can someone explain what the %w does.

Roelof

Raj Sahae 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 <lazaro@hcg.sld.cu> 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 Wobben
>
> 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
>
>