-1

如何在函数中用 ruby​​ 创建一个对象,这样我就有了类似的东西:

myobj = {
"s": "Hello World"
"y": "There
}

erb可以访问吗?

4

7 回答 7

6

例子:

foo = Object.new

def foo.bar
  s = "Hello World"
  y = "There"
end
于 2013-06-11T14:13:07.713 回答
4

如果您想要封装一些数据而不声明整个类,那么一种方法是使用Struct

Struct 是一种使用访问器方法将多个属性捆绑在一起的便捷方式,而无需编写显式类。

Myobj = Struct.new(:s, :y)
myobj = Myobj.new
myobj.s = "Hello World"
myobj.y = "There"

阅读文档以获取更多信息。

于 2013-06-11T14:19:57.857 回答
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 中输入整个内容,或者将其写入文件并请求它。

于 2013-06-11T14:19:06.893 回答
2

您显示的对象是 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"}
于 2013-06-11T14:48:10.417 回答
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

Dogs 的体重存储在实例变量@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

作为一项家庭作业,定义一种可以将狗的体重减轻一倍的方法,并使其发挥作用。

于 2013-06-11T17:05:02.357 回答
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   
于 2013-06-11T14:17:04.777 回答
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
于 2013-06-11T14:28:15.450 回答