Ruby 为什么我会得到;未定义的方法';插入语';对于main:Object";?

Ruby 为什么我会得到;未定义的方法';插入语';对于main:Object";?,ruby,methods,datamapper,Ruby,Methods,Datamapper,我正在使用DataMapper在MySQL中创建行。要在我的“Ryne”表中创建新行,我需要: a = {:saying, "Ppffftt..!"} Ryne.create a 所以我对自己说,“既然我一直都在用这个,为什么不制定一个更‘圆滑’的方法来做到这一点……” 但是,我在IRB中为插入语句(“Pffffft..”的输出是: NoMethodError: undefined method `insertsaying' for main:Object from (irb):2 这个方法

我正在使用DataMapper在MySQL中创建行。要在我的“Ryne”表中创建新行,我需要:

a = {:saying, "Ppffftt..!"}
Ryne.create a
所以我对自己说,“既然我一直都在用这个,为什么不制定一个更‘圆滑’的方法来做到这一点……”

但是,我在IRB中为
插入语句(“Pffffft..”
的输出是:

NoMethodError: undefined method `insertsaying' for main:Object
from (irb):2
这个方法本身是在一个.rb文件中定义的,我目前在IRB中需要这个文件。除了
DataMapper.setup
调用之外,.rb文件的其余部分如下所示:

DataMapper::Logger.new($stdout, :debug)
dm = DataMapper.setup :default, "mysql://php:phpwebsite@xxx.xxx.xxx.xxx/visit"
dm.resource_naming_convention = DataMapper::NamingConventions::Resource::Underscored


class Ryne
    include DataMapper::Resource
    is :reflective
    reflect /.*/
end

DataMapper.auto_upgrade!

def insertsaying(x)
    h = {:saying, x}
    Ryne.create h 
end 

我不确定我做错了什么,但我相信这是一个范围问题。我是否需要在方法中要求其他内容?

您是否在IRB中执行所有这些操作?IRB中是否也定义了方法?否。该方法是在我需要的.rb脚本中定义的,其中包括表映射和DataMapper.setup调用。为了便于查看,我更新了原始问题以显示.rb文件的其余部分。您确定编写的
{:saying,x}
正确吗?请不要使用类似的裸函数。把它们放在一个班里。Ruby是亲OO语言:)
DataMapper::Logger.new($stdout, :debug)
dm = DataMapper.setup :default, "mysql://php:phpwebsite@xxx.xxx.xxx.xxx/visit"
dm.resource_naming_convention = DataMapper::NamingConventions::Resource::Underscored


class Ryne
    include DataMapper::Resource
    is :reflective
    reflect /.*/
end

DataMapper.auto_upgrade!

def insertsaying(x)
    h = {:saying, x}
    Ryne.create h 
end