Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined method 'new'

Tags:

ruby

I have a fairly simple class that begins:

class Binding
  include Observable
  def initialize(variable)
    @variable = variable
    @state = variable.dup
    @log = Log.instance.log
  end
  # and so on...

In a testcase setip for Binding I have @test_binding = Binding.new(@test_variable) but I get the error

"NoMethodError: undefined method new' for Binding:Class C:/Users/Tim/RubymineProjects/LPA/Tests/binding_test.rb:25:insetup'

I've looked at some of the other questions here with almost the same title, but none of them seem to apply.

Can anybody tell me what I'm doing wrong?

like image 473
digitig Avatar asked Oct 16 '25 04:10

digitig


1 Answers

Can anybody tell me what I'm doing wrong?

Actually you are not doing anything wrong in that code. The fact is that the Binding class already exists in Ruby therefore what you are actually doing is reopening another class rather than defining a new one.

Particularly the Binding class doesn't have the new class method, which is the cause of the problem.

You have two reasonable choices now:

  1. Rename the class
  2. Put the class into a module

The first implies that you have to come up with another name, which is hardly what you want. The other can be thought as putting a class into a module/package/container of your own so that you distinguish what classes are part of your library and what are built ins.

like image 103
Shoe Avatar answered Oct 17 '25 18:10

Shoe