ruby中的字面常量$/是啥呢?

2019-03-25 13:32|来源: 网路

在ruby文档中有如下一句:
ios.gets(sep=$/) → string or nil
sep参数的默认值是$/,不明白是啥意思?

相关问答

更多
  • 你也可以将你的常量改为一个类方法: def self.secret 'xxx' end private_class_method :secret 这使得它可以在班级的所有实例中访问,但不能在外面访问。 You can also change your constant into a class method: def self.secret 'xxx' end private_class_method :secret This makes it accessible within all i ...
  • 不完全是你想要的,但你只是没有在A类内部定义CONST,而是在它的元类中,因此我保存了对...的引用。 class A class << self ::AA = self CONST = 1 end end puts AA::CONST Not exactly what you wanted, but you simply haven't defined CONST inside class A but in its metaclass, which I have therefor ...
  • 将进行哪些优化取决于编译器。 在您的情况下,C和C ++编译器通常都有足够的信息来将源代码优化为相同的机器代码。 换句话说,它实际上并不太依赖于文字中的内容以及此代码中的常量 。 话虽如此,术语文字和常量的含义在C和C ++中有显着差异(并且您同时标记了问题C和C ++)。 在C 60和3.0f是常数 ,但y不是常数 。 如果愿意,可以调用y一个const限定变量 ,但它在C术语中不是常量 ,在某种意义上,单个y不是C中的常量表达式 。 至于文字 ,在C语言中,术语“ 文字”仅适用于字符串文字(以及C99中 ...
  • 主要的是通过使用CONSTANT符号,你向读者说明。 小写,冷冻字符串给人的印象是可以设置,迫使某人回去阅读RDoc。 The main thing is that by using the CONSTANT notation, you're making it clear to the reader. the lower case, frozen string gives the impression is might be settable, forcing someone to go back and ...
  • Class.constants ? Class.constants?
  • 我把课改成了模块。 并且该方法不返回类的实例和模块的名称! 当然,如果你有机会使用静态方法,那将是有效的,在我看来我无法负担。 如何在没有模块的情况下做到这一点,我不知道。 I changed the class to the module. And the method does not return an instance of the class, and the name of the module! Of course it will work if you have the opportunit ...
  • 使用括号来显式调用方法: puts Myconstant #⇒ 'This is the constant' puts Myconstant() #⇒ 'This is the method' Use parentheses to explicitly call the method: puts Myconstant #⇒ 'This is the constant' puts Myconstant() #⇒ 'This is the method'
  • 你不能这样做。 问题不在于常量是否是私有的。 Ruby禁止在方法定义中创建常量。 def foo Foo = :foo end # => SyntaxError: ... dynamic constant assignment You cannot do that. The problem is not about whether or not a constant is private. Ruby prohibits creation of a constant in a method defini ...
  • 没错, 常量就像ruby中的变量一样,但如果你改变它们,你会得到一个警告。 此外,与单纯的变量有一个区别:即使在另一个类或模块内部定义了常量,也可以访问常量,例如,给出此片段: module Constants PI = 3,1415 other = "variable" end 你可以达到PI做Constants::PI而Constants::other将无法工作。 That's right, constants are just like variables in ruby, but you ...
  • 很容易将其归结为一般情况: 如果一个对象不再有任何引用,它可以被垃圾收集 。 请注意,我说对象 ,而不是变量 。 变量不是垃圾收集,对象是。 现在让我们看看你给出的例子: class Foo; end Bar = Class.new {} 如果将分配给它的常量(如果有的话)重新分配给不同的值(例如Bar = nil )并且没有该类的实例并且没有从中继承的类,那么Class实例将仅被垃圾收集。 foo = Class.new do self::NAME = "Bar" end foo::NAME # ...