为什么 File.new 的参数不是符号而不是字符串?
我想知道为什么编写 File
库的人决定使用字符串而不是符号来确定文件打开模式的参数。
例如,现在是这样的:
f = File.new('file', 'rw')
但这不是一个更好的设计
f = File.new('file', :rw)
吗
f = File.new(:file, :rw)
?这似乎是使用它们的完美地方,因为参数绝对不需要是可变的。
我很想知道为什么会这样。
Update: I just got done reading a related question about symbols vs. strings, and I think the consensus was that symbols are just not as well known as strings, and everyone is used to using strings to index hash tables anyway. However, I don't think it would be valid for the designers of Ruby's standard library to plead ignorance on the subject of symbols, so I don't think that's the reason.
I was wondering why the people who wrote the File
library decided to make the arguments that determine what mode the file is opened in strings instead of symbols.
For example, this is how it is now:
f = File.new('file', 'rw')
But wouldn't it be a better design to do
f = File.new('file', :rw)
or even
f = File.new(:file, :rw)
for example? This seems to be the perfect place to use them since the argument definitely doesn't need to be mutable.
I am interested in knowing why it came out this way.
Update: I just got done reading a related question about symbols vs. strings, and I think the consensus was that symbols are just not as well known as strings, and everyone is used to using strings to index hash tables anyway. However, I don't think it would be valid for the designers of Ruby's standard library to plead ignorance on the subject of symbols, so I don't think that's the reason.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 ruby 历史方面的专家,但是当您想要方法的参数时,您确实有三个选择:字符串、符号和静态类。
例如,异常处理。每个异常实际上都是 Exception 类的一种类型。
因此,您可以拥有流的每个权限,这是它自己的类。但这需要为系统生成更多的类。
关于符号的事情是它们永远不会被删除。您生成的每个符号都会无限期保留;这就是为什么不鼓励轻易使用“.to_sym”方法。它会导致内存泄漏。
字符串更容易操作。如果您从用户那里获得了输入模式,则代码中的某处需要一个“.to_sym”,或者至少需要一个大的 switch 语句。使用字符串,您可以将用户输入直接传递给该方法(当然,如果您如此信任的话)。
另外,在 C 中,您将一个字符传递给文件 i/o 方法。 ruby 中没有字符,只有字符串。看看 ruby 是如何构建在 C 之上的,这可能就是它的来源。
I'm no expert in the history of ruby, but you really have three options when you want parameters to a method: strings, symbols, and static classes.
For example, exception handling. Each exception is actually a type of class Exception.
So you could have each permission for the stream be it's own class. But that would require even more classes to be generated for the system.
The thing about symbols is they are never deleted. Every symbol you generate is preserved indefinitely; it's why using the method '.to_sym' lightly is discouraged. It leads to memory leaks.
Strings are just easier to manipulate. If you got the input mode from the user, you would need a '.to_sym' somewhere in your code, or at the very least, a large switch statement. With a string, you can just pass the user input directly to the method (if you were so trusting, of course).
Also, in C, you pass a character to the file i/o method. There are no Chars in ruby, just strings. Seeing as how ruby is built on C, that could be where it comes from.
它只是以前语言的遗物。
It is simply a relic from previous languages.