What does a ruby method ending with an "=" mean?
以"="结尾的ruby方法是什么意思?
See the available methods in this print out:
请参阅本手册中可用的方法:
2.2.0 :066 > obj.methods(false)
=> [:label=, :label, :description=, :description, :thumbnail=, :thumbnail, :attribution=, :attribution, :license=, :license, :logo=, :logo, :see_also=, :seeAlso=, :see_also, :seeAlso, :related=, :related, :within=, :within, :metadata=, :metadata, :sequences=, :sequences, :structures=, :structures, :viewing_hint=, :viewingHint=, :viewing_hint, :viewingHint, :viewing_direction=, :viewingDirection=, :viewing_direction, :viewingDirection, :service=, :service]
For example whats this difference between label= and label?
例如,label=和label之间有什么区别?
4 个解决方案
#1
3
the methods ending with "=" are setting the instance variable
以“=”结尾的方法是设置实例变量。
look at the answer here: why-use-rubys-attr-accessor-attr-reader-and-attr-writer
看看这里的答案:why-use-rubys- attro -reader- reader- reader- reader- reader。
#2
9
foo=
is no different than any other method, except:
foo=与其他方法没有区别,除了:
- it requires precisely one argument and
- 它只需要一个论证
- Ruby permits you to add spaces before the
=
character. - Ruby允许您在=字符之前添加空格。
class Foo
def foo=(other)
puts 'hi'
end
end
Foo.new.foo = 7
hi
class Goo
def goo=
puts 'hi'
end
end
Goo.new.goo=
Ruby says, "I'm waiting for an argument...". So we provide one:
Ruby说:“我在等一个争论……”所以我们提供:
4
and then she complains about what she asked you to do:
然后她抱怨她要你做的事:
#=> ArgumentError: wrong number of arguments (1 for 0)
=
methods are typically used to create a setter for an instance variable (if attr_acccessor
or attr_writer
is not used):
=方法通常用于为实例变量创建setter(如果不使用attr_acccessor或attr_writer):
class Foo
def initialize(foo)
@foo=foo
end
# getter
def foo
@foo
end
# setter
def foo=(other)
@foo = other
end
end
f = Foo.new('dog')
f.foo
#=> "dog"
f.foo = 'cat'
#=> "cat"
f.foo
#=> "cat"
#3
2
It is equivalent of setter methods in other languages, it is just convention so it looks more natural to say
它相当于其他语言中的setter方法,它只是一种约定,所以它看起来更自然
obj.description="Fun Site"
vs
vs
obj.setDescription("Fun Site")
#4
1
There is nothing special about methods that end in =
以=结尾的方法没有什么特别之处
You can see this by running the code below:
你可以通过下面的代码看到这一点:
def bob=
puts "bob="
end
p send('bob='.to_sym)
What is special is the '=' infix operator. When you write self.bob = "bill"
. It is interpreted as self.send('bob='.to_sym, "bill")
.
特殊的是'='中缀运算符。当你写自我。鲍勃=“比尔”。它被解释为self.send('bob=')。to_sym,“法案”)。
Putting a ?
at the end of a method is a hint that it returns a boolean (true/false). Methods that end in !
hint that the method affects the instance. See String#chomp vs String#chomp.
把一个吗?在方法的末尾有一个提示,提示它返回一个布尔值(true/false)。方法结束!提示该方法影响实例。看到字符串# # chomp chomp vs字符串。
You can find out more about ruby operators at http://www.tutorialspoint.com/ruby/ruby_operators.htm and more about naming conventions at https://github.com/bbatsov/ruby-style-guide#naming
您可以在http://www.tutorialspoint.com/ruby/ruby_operators.htm上了解更多关于ruby操作符的信息,并在https://github.com/bbatsov/ruby-style-guide#命名中了解更多关于命名约定的信息
#1
3
the methods ending with "=" are setting the instance variable
以“=”结尾的方法是设置实例变量。
look at the answer here: why-use-rubys-attr-accessor-attr-reader-and-attr-writer
看看这里的答案:why-use-rubys- attro -reader- reader- reader- reader- reader。
#2
9
foo=
is no different than any other method, except:
foo=与其他方法没有区别,除了:
- it requires precisely one argument and
- 它只需要一个论证
- Ruby permits you to add spaces before the
=
character. - Ruby允许您在=字符之前添加空格。
class Foo
def foo=(other)
puts 'hi'
end
end
Foo.new.foo = 7
hi
class Goo
def goo=
puts 'hi'
end
end
Goo.new.goo=
Ruby says, "I'm waiting for an argument...". So we provide one:
Ruby说:“我在等一个争论……”所以我们提供:
4
and then she complains about what she asked you to do:
然后她抱怨她要你做的事:
#=> ArgumentError: wrong number of arguments (1 for 0)
=
methods are typically used to create a setter for an instance variable (if attr_acccessor
or attr_writer
is not used):
=方法通常用于为实例变量创建setter(如果不使用attr_acccessor或attr_writer):
class Foo
def initialize(foo)
@foo=foo
end
# getter
def foo
@foo
end
# setter
def foo=(other)
@foo = other
end
end
f = Foo.new('dog')
f.foo
#=> "dog"
f.foo = 'cat'
#=> "cat"
f.foo
#=> "cat"
#3
2
It is equivalent of setter methods in other languages, it is just convention so it looks more natural to say
它相当于其他语言中的setter方法,它只是一种约定,所以它看起来更自然
obj.description="Fun Site"
vs
vs
obj.setDescription("Fun Site")
#4
1
There is nothing special about methods that end in =
以=结尾的方法没有什么特别之处
You can see this by running the code below:
你可以通过下面的代码看到这一点:
def bob=
puts "bob="
end
p send('bob='.to_sym)
What is special is the '=' infix operator. When you write self.bob = "bill"
. It is interpreted as self.send('bob='.to_sym, "bill")
.
特殊的是'='中缀运算符。当你写自我。鲍勃=“比尔”。它被解释为self.send('bob=')。to_sym,“法案”)。
Putting a ?
at the end of a method is a hint that it returns a boolean (true/false). Methods that end in !
hint that the method affects the instance. See String#chomp vs String#chomp.
把一个吗?在方法的末尾有一个提示,提示它返回一个布尔值(true/false)。方法结束!提示该方法影响实例。看到字符串# # chomp chomp vs字符串。
You can find out more about ruby operators at http://www.tutorialspoint.com/ruby/ruby_operators.htm and more about naming conventions at https://github.com/bbatsov/ruby-style-guide#naming
您可以在http://www.tutorialspoint.com/ruby/ruby_operators.htm上了解更多关于ruby操作符的信息,并在https://github.com/bbatsov/ruby-style-guide#命名中了解更多关于命名约定的信息