如何在函数中的ruby中创建一个对象,以便我有类似的东西:
myobj = {
"s": "Hello World"
"y": "There
}
可以通过erb访问吗?
答案 0 :(得分:5)
示例:强>
foo = Object.new
def foo.bar
s = "Hello World"
y = "There"
end
答案 1 :(得分:3)
class MyClass
def initialize(y)
@s = 'Hello World'
@y = y
end
def s
@s
end
def y
@y
end
end
myobj = MyClass.new 'There'
myobj.s # => "Hello World"
myobj.y # => "There"
您可以在irb中输入整个内容,或将其写入文件并要求它。
答案 2 :(得分:3)
如果您想要的是封装一些数据而不声明整个类,那么使用Struct
的方法就是这样做Struct是一种使用访问器方法将多个属性捆绑在一起的便捷方式,而无需编写显式类。
Myobj = Struct.new(:s, :y)
myobj = Myobj.new
myobj.s = "Hello World"
myobj.y = "There"
阅读文档以获取更多信息。
答案 3 :(得分:2)
我原本只是想回复你的评论,但让我做出完整的答案。来自Gorfi的代码:
foo = Object.new
# What's going on here? What have we done? This:
# We took a constant "Object", to which the Object class is assigned.
# Then we sent it message ":new". Method #new is a constructor that
# creates a brand new instance of Object.
# Then we assigned that newly created object to the local variable "foo".
现在我们要在分配给foo的对象上定义一个单例方法。有多种方法可以做到这一点。但我们首先要确保我们知道单例方法是什么。通常,方法与类相关联。例如,类Dog
可能具有实例方法#bark
,这意味着所有Dog
实例都知道如何#bark
。另一个例子,所有对象都知道他们的#object_id
:
foo.object_id #=> some number
# Here, we sent a message :object_id to foo, which invoked the appropriate method and
# returned us the object id unique to the object instance foo. All the objects know this
# trick. But not all the objects can respond to the method `#bar`:
foo.bar #=> raises NoMethodError
所以Gorfi所做的是,他给了foo一个非常特殊的能力:回应#bar
。它被称为foo的单例方法,因为普通的Object
实例不知道如何响应#bar
。在我们写完之后:
def foo.bar
puts "I'm special, a singleton in my own set, I know how to respond to bar."
end
对象foo将响应bar:
foo.bar #=> see what happens
继续使用狗的例子,我们可以定义Dog
类
class Dog
def bark
puts "Bow, wow!"
end
end
Spot, Rover, Minnie = Dog.new, Dog.new, Dog.new
Spot.bark #=> Bow, wow!
Rover.bark #=> Bow, wow!
Minnie.bark #=> Bow, wow!
# All the Dog instances can bark.
# But only Minnie can sing:
def Minnie.sing
puts "Bauuuuu, uauuuuu, bauuuuu, uauuuuu, " +
"bow wow wow wow wow wow wow wauuuuuuuuuuuuuu!"
end
Minnie.sing #=> see what happens
Rover.sing #=> see what happens
现在,最后,让我们介绍一下狗的体重。每只狗自然都有它的重量。所以我们在Dog类中代表它。
class Dog # we reopen the Dog class first
attr_accessor :weight # we introduce dog weight
end
# and now
Spot.weight = 10
Rover.weight = 20
Minnie.weight = 8
# we now can get each dog's weight:
Spot.weight #=> 10
Minnie.weight #=> 8
狗的体重存储在实例变量@weight
中。因此,举例来说,让我们为狗定义喂食方法,将它们的体重增加1:
class Dog # reopen the class
def feed # define weight instance method
@weight = @weight + 1 # increment weight by 1
end
end
Spot.feed
Spot.weight #=> 11
Spot.feed
Spot.weight #=> 12
作为家庭作业,定义一种方法,将狗的体重减少一个,并使其发挥作用。
答案 4 :(得分:1)
与Javascript不同,您必须先创建一个类。通常你会将它存储在它的文件中。稍后你将它实例化为一个对象。
class MyClass
def initialize(var_s, var_y)
@var_s = var_s
@var_y = var_y
end
end
然后您可以使用 p 方法对其进行实例化并显示内容。
myObj = MyClass.new("Hello Word","There")
p myObj
答案 5 :(得分:1)
你展示的对象是Ruby中的哈希,除了你:
"s":
将符号创建为哈希键。"There
的报价。另外,myobj
是一个局部变量,所以你想要一个实例变量@myobj
?
以下是如何正确执行此操作的示例:
myobj = {
"s" => "Hello World",
"y" => "There"
}
myobj # => {"s"=>"Hello World", "y"=>"There"}
myobj = {
:s => "Hello World",
:y => "There"
}
myobj # => {:s=>"Hello World", :y=>"There"}
myobj = {
s: "Hello World",
y: "There"
}
myobj # => {:s=>"Hello World", :y=>"There"}
@myobj = {
"s" => "Hello World",
"y" => "There"
}
@myobj # => {"s"=>"Hello World", "y"=>"There"}
@myobj = {
s: "Hello World",
y: "There"
}
@myobj # => {:s=>"Hello World", :y=>"There"}
答案 6 :(得分:0)
喜欢这个
class Myobj
attr_reader :s, :y
def initialize()
@s, @y = "Hello World ", "There"
end
end
ob = Myobj.new
print ob.s, ob.y
给出
Hello World There