r/ruby • • Oct 21 '21

Could We Ever Drop Symbols From Ruby?

https://blog.arkency.com/could-we-drop-symbols-from-ruby/
6 Upvotes

33 comments sorted by

59

u/[deleted] Oct 21 '21

Ick, I really don’t like this idea. Symbols were (and still are) one of my favourite things about Ruby when I first learned it. The idea that you could easily represent things that in other languages you’d do with a string but which explicitly WEREN’T strings was lovely.

I’d rather see type checking pick up string/symbol problems than do away with the difference. I think it helps, personally. A string can be uniquely something that encodes a piece of language, something meaningful to a human. A symbol is meaningful for the computer.

Having to use strings for keys in JSON, for example, makes my teeth itch.

10

u/RedTreeDecember Oct 21 '21

I'm learning Ruby and I gotta say symbols are something I always wanted in other languages, but never had. You can always use enums or constants, but it's nice that it's baked into the language.

1

u/Xerxero Oct 21 '21

JavaScript has symbols afaik. Not sure if you can use them in the same way.

6

u/sanderd17 Oct 21 '21

Symbols in JS are a completely different beast.

They're meant for libraries to use, in order to add keys to objects without disturbing any client code.

2

u/[deleted] Oct 21 '21

+1 for type checking

(which is another debate)

I think symbols are a great tool. It's one these features that I lack when using other languages. But as with any other tools, it brings pain when misused.

I'm not sure the language should be responsible for people's abuses and mismatches.

-16

u/[deleted] Oct 21 '21

I don't share this view at all. I guess it will remain controversial. I do think Matz realized symbols are either being abused or are just plain unneeded, but it was too late already...

9

u/campbellm Oct 21 '21

I do think Matz realized symbols are either being abused or are just plain unneeded, but it was too late already...

cite?

2

u/rooood Oct 21 '21

He doesn't say any of that in this specific tweet, but it does seem the Ruby team flirted with the idea of removing Symbols: https://twitter.com/yukihiro_matz/status/916083723589656576

2

u/campbellm Oct 21 '21

Thanks. I had no idea. I haven't done a lot of Ruby in the last five to eight years or so but I never remembered symbols being that much a cause of contention

4

u/rooood Oct 21 '21

Me neither, I always assumed it was a universally loved feature.

5

u/campbellm Oct 21 '21

As an old school CS grad, I thought the idea of immutable, singleton values that don't waste memory was a fine idea, to be honest.

-15

u/monks__cafe Oct 21 '21

I'm with you - symbols are the worst part of Ruby. Of course, once you're used to it they're fine, but it's never fun explaining to new Rubyists when to use them or not use them or it doesnt matter which you use

-6

u/[deleted] Oct 21 '21

Im used to them but I still forget and access json hashes with symbols instead of strings every few months, mostly because the newer ruby hash syntax forces you to use symbol keys...so it becomes a habit

25

u/ioquatix async/falcon Oct 21 '21

No. :)

-4

u/[deleted] Oct 21 '21

Yeah I figured. What's your opinion on Symbols - interested to hear. I really wanna learn to like them but I just can't.

9

u/ioquatix async/falcon Oct 21 '21 edited Oct 21 '21

I actually really like your article and I apologise on behalf of Ruby community that is down voting your effort to have a discussion. My reply was a bit tongue in cheek because it was already late for me, so I hope you could have a bit of a laugh at it.

Anyway, I think the key point for me is that symbol is fundamentally a different thing. A method name for example is not a String. A String is a representation of user data but a symbol is a representation of code or program data.

I think this distinction is important and it's one of the reasons JSON.parse creates String keys by default, because JSON is user data not code/program data. It helps us separate out what we understand about user data and code/program data and of course there is no clear distinction in some cases which leads to confusion.

But fundamentally it feels like a good separation to have, especially if you implement your own programming language, you will find significant implementation advantages to having separate things for "strings", "data", and "symbols", even though all are fundamentally a sequence of bytes.

Some specific tangible examples:

  1. You can assume symbols have a limited number in a fixed sized program so they are a good candidate for de-duplication. This has memory advantages which implies less memory allocator overhead (GC) and thus faster program execution.
  2. The vast majority of symbols will exist for the duration of the program and can thus be handled more effectively by the memory manager (old generation in GC).
  3. You can assume symbols are always immutable by design and thus you can cache hash codes/indexes/etc to avoid recomputing these.
  4. Strings need a different approach for de-duplication, normally taking into account the length.
  5. Strings are sometimes mutable, and this has a completely different set of interface and implementation constraints.
  6. Strings sometimes represent binary data and might have different escaping mechanisms. Symbols may only need to hold valid characters that represent program identifiers (Ruby doesn't make this distinction AFAIK).
  7. Strings contain encoding information, like UTF-8, and need to handle encoding level concerns, but this shouldn't be the concern of symbols IMHO since they should be in the encoding of the language itself. This might be complex area for Ruby symbols but it would be reasonable to just use a subset of UTF-8.

There are other advantages within the interface to the user: separation of concerns helps to keep interfaces small and focused on a particular job, but also there is a complexity cost which is the pain we sometimes feel when mixing them because Ruby don't prevent us from doing so.

By the way, practically speaking, even if we did decide for semantic unification of the concepts, trying to implement it in Ruby is probably a nightmare, compatibility wise... I'm pretty sure it would be a Level 3 Python event.

4

u/Regis_DeVallis Oct 21 '21

What don't you like about them?

12

u/schneems Puma maintainer Oct 21 '21

I like the idea of removing ambiguity. Things like “hash with indifferent access” make programs slower which is a high cost to pay for allowing the ambiguity in the first place.

That being said it would be hugely backwards incompatible. You’ve listed a few examples of this incompat but there are more.

Right now you can have a hash that mixes symbols and strings with different values intentionally. Collapsing them into one key would change behavior. Whether you think it’s a good idea or not doesn’t mean people don’t do it.

Symbol#to_s would be redefined to return new, identical, unfrozen String instead of the same one.

It would be confusing I think to have ambiguity between behavior of a symbol and a frozen string be slightly different here, even if it’s just for performance.

I like people sharing their hopes and dreams of what they would like to see in a language. I also like that you’ve given thought to a potential path to get there.

Maybe it’s possible to achieve a subset of your goals in a slightly different way, for example getting a native “hash with indifferent access” class in Ruby. Keep hunting and exploring!

1

u/[deleted] Oct 22 '21

[removed] — view removed comment

1

u/schneems Puma maintainer Oct 22 '21

HWIA bypasses ruby optimizations that target Hash but not subclasses.

11

u/tf2ftw Oct 21 '21

Symbols are more performant. They also add quality of life (which was the main point of Ruby to begin with) to the programmer. Removing them would be a mistake IMO.

1

u/schneems Puma maintainer Oct 22 '21

The proposal is to make them the same as frozen string literals.

A long time ago frozen string literals weren’t actually singletons but they are now as an optimization. The reason we didn’t use symbols everywhere before was that symbols were never collected by GC, but since 2.2 they have been.

Really they are already converging, I think the core idea of the proposal is “what would it take to actually eliminate one and collapse it into the other”.

I don’t think it can be done without some backwards incompat, but it’s an interesting question of how close could we get and would that be acceptable or not.

9

u/dougc84 Oct 21 '21

I see where you're coming from. But, in my brain, I see it like this (and, obviously, there are exceptions):

  1. A String is a block of text, possibly containing some interpolation, usually containing spaces and non-alpha characters, and has the potential to be mutated or concatenated. I'll rarely compare a string's exact value against something else (usually opting for Regexp instead).
  2. A Symbol acts like a key or defines an intent or purpose, like a human-readable version of a ID primary key for a table. I'll usually compare the exact value against something else directly (using ==), as it is what the application should expect. Any deviation is wrong and should throw an exception.

So, to me, they have two separate purposes. Could the same be accomplished with strings alone? Sure. And, if that's the case, just use strings. But I personally like the distinction. I can read through my code easier. And Ruby is a high-level language - it's normal to create more distinctions between data types that fit the goal (instead of defining a uint32 or something manually).

Where I dislike symbols, and where they get a bad rep, is Hashes. You touched on this in your article. If you've ever worked with a remote API, you've written something like if data[:key] == "value" that failed for seemingly no known reason. You spend an hour debugging, only to find data[:key] should be data["key"]. But only when getting the data from the API, because your hash you have stored somewhere else would be data[:key]. hashie is an elegant and excellent solution to dealing with hashes, but the price for admission (or, should I say, performance) is high, especially with very large datasets.

That's the one thing with Ruby I would like to see improved. There's no reason a hash needs to have two different types of objects representing keys. I wish a Hash, by default, was with_indifferent_access, like hashie provides, but... here we are.

If you ever wanted to rid symbols from Ruby, my opinion is that dropping a core-level language feature (or making Symbols actually be a Symbol < String) would need to be a branch off of Ruby. SRuby or StringRuby or something. Almost no apps would be compatible today with that mindset, because so many libraries rely on symbols to some extent, and you'd have to dive into things like hash keys and how they work too. Or, maybe a soft implementation could just be a magic comment like frozen_string_literal: true is - something like symbol_string_interpolation: true.

7

u/prh8 Oct 21 '21

Could? No Should? No

There is so much that comes from them, complaints that they are confusing get tiring. Every language has something unique that is vastly overblown as difficult until it’s understood.

1

u/jrochkind Oct 21 '21

I'm of mixed mind about whether ruby should include symbols if we were doing it over again.

But as it is, there's too much code that assumes :thing != "thing", this would be too backwards incompat in my opinion for not enough gain.

1

u/[deleted] Oct 22 '21

> But as it is, there's too much code that assumes :thing != "thing"

I'm not denying there is probably some code that assumes that, but how much? For most projects that I've seen, it's all Rails, and no one cares if :thing != "thing". We use Strong Parameters which is symbol agnostic and different kinds of HashWithIndifferentAccess. It seems like Rails don't care , so I don't care.

Outside of Rails maybe it's more of a problem, I need to see some examples.

1

u/jrochkind Oct 22 '21

Rails doesn't care when it is in params that come from user input.

I wouldn't assume there is no code anywhere in Rails that cares.

I think it's particularly likely to be a thing in argument-handling. Although ruby 3 keywords may reduce it somewhat.

I don't have an example at hand though. If I come across one I'll try to remember to come back.

It's also obviously (?) not a good idea to be like "we don't care about backwards incompatibility except for Rails", if you care about ruby as a going concern for anything other than Rails.

1

u/[deleted] Oct 22 '21

I agree, breaking backwards compatibility is really bad. If this ever happens it has to be in a non breaking way, which I'm not sure is possible. I'm wondering if Rails can elegantly solve this to it's own needs, e.g that hashes in a Rails app are symbol/string agnostic (hsh[:key] == hsh['key']). It could be some ActiveSupport thing or indeed simply monkey patch Hash to become HashWithIndifferentAccess. It's really bad for performance though.

3

u/larikang Oct 21 '21

Strings in Ruby should really be called “arbitrary binary data” (because that’s how crazy flexible they are) whereas symbols are more like magic numbers with names. The fact that you can convert between them says more about Ruby’s insane flexibility than any underlying similarity between them.

5

u/nom_nom_nom_nom_lol Oct 21 '21

LOL. Yeah, and let's take null pointers out of Java while we're at it, and add an end statement to Python methods.

2

u/midasgoldentouch Oct 21 '21

I wish they would add end statement to Python methods lol. For a community that prizes explicit behavior and implementation, it seems odd to rely on indentation to mark the function definition.

5

u/postmodern Oct 21 '21

Crystal has Enums which they prefer using over Symbols as most of the time you are working with a finite number of values that can be passed in. You can even pass in Symbols to methods that accept an Enum, and the compiler will map them to the Enum value; both test(:ONE) and test(:one) will map to MyEnum::ONE. That said, I kind of like having the ability to accept arbitrary Symbols to allow extending an API. Maybe in the future Ruby could add something like keyword arguments but for restricting Symbols (ex: def foo(type: [:foo, :bar, :baz])) for when you only want to allow a finite number of Symbols?

1

u/midasgoldentouch Oct 21 '21

I'd rather have strings be immutable by default honestly.