Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't the arguments to File.new symbols instead of strings?

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.
like image 647
Seth Carnegie Avatar asked Sep 24 '11 21:09

Seth Carnegie


People also ask

How do I convert a Symbol to a string in Ruby?

Converting between symbols to strings is easy - use the . to_s method. Converting string to symbols is equally easy - use the . to_sym method.

How do you assign symbols in Ruby?

A Symbol object is created by prefixing an operator, string, variable, constant, method, class name with a colon (:). The symbol object will be unique for each different name but does not refer to a particular instance of the name, for the duration of a program's execution.

What are symbols in Ruby?

What's a Symbol in Ruby? A symbol is a unique instance of the Symbol class which is generally used for identifying a specific resource. A resource can be: a method.


2 Answers

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.

ArgumentError.is_a? Class
=> True

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.

like image 115
kojaktsl Avatar answered Sep 20 '22 21:09

kojaktsl


It is simply a relic from previous languages.

like image 20
weexpectedTHIS Avatar answered Sep 24 '22 21:09

weexpectedTHIS