Ruby:参考资料,了解有关为自己赋值的更多信息

Ruby:参考资料,了解有关为自己赋值的更多信息,ruby,reference,self,Ruby,Reference,Self,我无法找到有关此主题的文档或任何参考资料: 有人知道我能读到的关于这件事的更多信息吗 编辑:根据评论,我对这个问题做如下修改: 因此,我们发现数组和字符串可以通过self的数组表示法进行操作: self[i]= 但这并不是操纵自我价值的全部原因。关于自我的范围以及它在当前语境中的含义,有大量的参考资料,但关于self操纵方法,我发现的不多 如果我想编写自己版本的String的chomp或其他方法?我是否被锁定使用self[0]…self[I]?其他课程呢 谢谢 首先阅读(即使它根本没有提到Ru

我无法找到有关此主题的文档或任何参考资料:

有人知道我能读到的关于这件事的更多信息吗

编辑:根据评论,我对这个问题做如下修改:

因此,我们发现数组和字符串可以通过self的数组表示法进行操作:

self[i]=
但这并不是操纵自我价值的全部原因。关于自我的范围以及它在当前语境中的含义,有大量的参考资料,但关于
self
操纵方法,我发现的不多

如果我想编写自己版本的
String
chomp或其他方法?我是否被锁定使用
self[0]…self[I]
?其他课程呢

谢谢

首先阅读(即使它根本没有提到Ruby)

长话短说:

  • Ruby从其他语言中借用了很多概念,
    self
    来自Smalltalk
  • self
    在Smalltalk中被称为伪变量,这意味着它是可变的,但它是由运行时环境设置的,而不是由程序或程序员设置的
  • self
    始终引用消息接收者
    super
    引用该消息的超类,该超类由引用
    super
    所在的方法实现。(很高兴您没有要求
    super
  • Ruby中的
    self
    与Smalltalk中一样,始终引用当前对象,而当前对象可能是类的实例,甚至是类本身。因此,如果您在类端定义方法(仅可在类上调用),即使有
    self
    引用对象,也就是类。因此,在Ruby中可以只使用
    self
    ,而不必写下类的名称来表示接收者。这对重构有一点帮助
如果你已经掌握了所有这些,那么看看下面的内容,它告诉你更多的技巧如何使用
self
、类、特征类和其他一些有趣的东西。

首先阅读(即使它根本没有提到Ruby)

长话短说:

  • Ruby从其他语言中借用了很多概念,
    self
    来自Smalltalk
  • self
    在Smalltalk中被称为伪变量,这意味着它是可变的,但它是由运行时环境设置的,而不是由程序或程序员设置的
  • self
    始终引用消息接收者
    super
    引用该消息的超类,该超类由引用
    super
    所在的方法实现。(很高兴您没有要求
    super
  • Ruby中的
    self
    与Smalltalk中一样,始终引用当前对象,而当前对象可能是类的实例,甚至是类本身。因此,如果您在类端定义方法(仅可在类上调用),即使有
    self
    引用对象,也就是类。因此,在Ruby中可以只使用
    self
    ,而不必写下类的名称来表示接收者。这对重构有一点帮助
如果您已经了解了所有这些,请看下面的内容,它将告诉您如何使用
self
、类、特征类和其他一些有趣的东西。

(因为这篇评论有点长…)

实际上,您不能更改
self
的值,但可以更改
self
的属性,这就是您的示例中发生的情况

让我详细说明一下。假设您有一个类
Foo
,您可以这样做:

f = Foo.new
f.bar = 3
puts f.bar # => 9
class Foo
  def bar=(val)
    @bar = val**2 # square the given value and assign it to the instance
  end             # variable @bar

  def bar
    @bar          # return the instance variable @bar -- a shortcut for this is
  end

  # we could get rid of the second method, though, but using attr_reader:
  attr_reader :bar
end
class Foo
  def [](val)
    val * 2 # Ruby just takes the value you put between [] and gives it to you as
  end       # the first parameter
end
2
”??这里实际发生的情况是,您正在使用参数
1
调用
f
上的方法
bar=
。方法
bar=
可以如下所示:

f = Foo.new
f.bar = 3
puts f.bar # => 9
class Foo
  def bar=(val)
    @bar = val**2 # square the given value and assign it to the instance
  end             # variable @bar

  def bar
    @bar          # return the instance variable @bar -- a shortcut for this is
  end

  # we could get rid of the second method, though, but using attr_reader:
  attr_reader :bar
end
class Foo
  def [](val)
    val * 2 # Ruby just takes the value you put between [] and gives it to you as
  end       # the first parameter
end
好吧,那么这个呢

f = Foo.new
puts f[5] # => 10
10
”?!是的。同样,
[]
只适用于普通的旧方法。大概是这样的:

f = Foo.new
f.bar = 3
puts f.bar # => 9
class Foo
  def bar=(val)
    @bar = val**2 # square the given value and assign it to the instance
  end             # variable @bar

  def bar
    @bar          # return the instance variable @bar -- a shortcut for this is
  end

  # we could get rid of the second method, though, but using attr_reader:
  attr_reader :bar
end
class Foo
  def [](val)
    val * 2 # Ruby just takes the value you put between [] and gives it to you as
  end       # the first parameter
end
最后:

f = Foo.new
f[:bar] = 99
puts f[:bar] # => 100
是的,你猜对了,这只是另一个方法调用。例如:

class Foo
  @my_hash = {}

  def []=(key, val)         # Ruby gives us the value between the [] as the first
    @my_hash[key] = val + 1 # parameter and the value after the = as the second,
  end                       # and we use them to set a value on an internal
                            # instance variable...
  def [](key)
    @my_hash[key]           # ...and we can use the "getter" to get a value from 
  end                       # the instance variable.
end
你是对的,这些东西并不是都包含在一个单一的,方便的来源,所以我希望这有帮助。如果您需要进一步解释,请随时发表评论。

(因为这篇评论有点长…)

实际上,您不能更改
self
的值,但可以更改
self
的属性,这就是您的示例中发生的情况

让我详细说明一下。假设您有一个类
Foo
,您可以这样做:

f = Foo.new
f.bar = 3
puts f.bar # => 9
class Foo
  def bar=(val)
    @bar = val**2 # square the given value and assign it to the instance
  end             # variable @bar

  def bar
    @bar          # return the instance variable @bar -- a shortcut for this is
  end

  # we could get rid of the second method, though, but using attr_reader:
  attr_reader :bar
end
class Foo
  def [](val)
    val * 2 # Ruby just takes the value you put between [] and gives it to you as
  end       # the first parameter
end
2
”??这里实际发生的情况是,您正在使用参数
1
调用
f
上的方法
bar=
。方法
bar=
可以如下所示:

f = Foo.new
f.bar = 3
puts f.bar # => 9
class Foo
  def bar=(val)
    @bar = val**2 # square the given value and assign it to the instance
  end             # variable @bar

  def bar
    @bar          # return the instance variable @bar -- a shortcut for this is
  end

  # we could get rid of the second method, though, but using attr_reader:
  attr_reader :bar
end
class Foo
  def [](val)
    val * 2 # Ruby just takes the value you put between [] and gives it to you as
  end       # the first parameter
end
好吧,那么这个呢

f = Foo.new
puts f[5] # => 10
10
”?!是的。同样,
[]
只适用于普通的旧方法。大概是这样的:

f = Foo.new
f.bar = 3
puts f.bar # => 9
class Foo
  def bar=(val)
    @bar = val**2 # square the given value and assign it to the instance
  end             # variable @bar

  def bar
    @bar          # return the instance variable @bar -- a shortcut for this is
  end

  # we could get rid of the second method, though, but using attr_reader:
  attr_reader :bar
end
class Foo
  def [](val)
    val * 2 # Ruby just takes the value you put between [] and gives it to you as
  end       # the first parameter
end
最后:

f = Foo.new
f[:bar] = 99
puts f[:bar] # => 100
是的,你猜对了,这只是另一个方法调用。例如:

class Foo
  @my_hash = {}

  def []=(key, val)         # Ruby gives us the value between the [] as the first
    @my_hash[key] = val + 1 # parameter and the value after the = as the second,
  end                       # and we use them to set a value on an internal
                            # instance variable...
  def [](key)
    @my_hash[key]           # ...and we can use the "getter" to get a value from 
  end                       # the instance variable.
end

你是对的,这些东西并不是都包含在一个单一的,方便的来源,所以我希望这有帮助。如果您需要进一步的解释,请随时发表评论。

真的没有什么可学的了
self
是对当前“内部”对象的引用。您可以像操作任何其他对象一样操作
self
(但也可以直接访问私有和受保护的方法)。如果你想知道更多信息,谷歌搜索第一页上的大多数链接都非常可靠:是的,但尽管你可以通过
self[0]=
访问字符串,但这只能得到第一个字符<很遗憾,code>self=
不是有效的操作。在那些li中,关于自我[0]或类似的东西真的不多