Intern and to_sym?
- Why have multiple ways to do the same things?
- Is one more efficient with memory?
- Why not just use the one that is more efficient, can they each do something different?
They are just two names for the same method. They do exactly the same thing, there is no difference at all except for the name.
Why have two ways of doing the same thing? Well, that’s a silly question. To be able to write expressive code. A language that only has one way to get from A to B is not a language at all. Even in arithmetics, you have countless ways of expressing the same idea, say, of multiplying 5 by 2: you can say
(1+1)*5, so there is always more than one way.
A better question would be why Ruby has two names for the same method. This is just to give the programmer the freedom to write human-readable code that makes the intention of what you’re trying to do transparent:
.to_symtells you that a string is being converted to a symbol
.intern, while performing precisely the same task, stresses the fact that it gets you the “internal representation” of the string – because Ruby converts all the string literals that you use in the code to symbols internally.
These synonyms (or aliases) are all over Ruby:
Array#map is the same as
String#succ (successor) is the same as
Hash#each is the same as
Numeric#abs is the same as
You can like it or hate it. Personally I like the expressivity about this – the fact that I can stress what I’m doing (for instance, I might be collecting, not “mapping”, even if it makes no difference whatsoever to Ruby). Another advantage is that if you forget one of the names you might still remember the other, so you don’t have to look up, you just use the one synonym your mind was more familiar with. Also, keeping several method names around makes it easier for a programmer to learn Ruby, because in all likelihood one of those names is very similar to what they already know from another programming language.
In my opinion Ruby should not be the first programming language one learns (because it can confuse beginners), but for people that have coded in a couple of other languages before, Ruby is a bliss.
symbols = Array.new strings.each do |x| symbols.push(x.intern) end
Add your code below!
symbols =  strings.each do |x| symbols.push(x.intern ) end
@Alex J, very nice and easy to understand explanation. Off topic, what then would be best first time programming language?
:one => 1, :two => 2, # Fill in these two blanks! :three => 3,
Add your code below!
strings.each do |s| symbols.push(s.to_sym) end
“For each s in strings, use .to_sym to convert s to a symbol and use .push to add that new symbol to symbols.” can we word this like normal humans first then work on wording it like a crazy monkey on a typewriter? for hints can we actually give relevant hints?
Popular free courses
- Free Course
Learn SQLIn this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency
- Free Course
- Free Course
Learn HTMLStart at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
So which should be the first language a beginner learns? And don’t say “It depends what you want to do” because beginners can’t do anything. Strictly to learn what code is and how the logic and syntax behind it works, what should a beginner learn? I’m finding Ruby pretty easy to learn, and my only experience was doing half the Python track before giving it up because of buggy lessons and poor explanations. I chose to give Ruby a go because Eric Weinstein wrote the whole course and he’s the best teacher on this site. Maybe the teacher matters more than the language one chooses.
you certainly write a lot Alex J. I’ve seen other things you’ve written and have been very impressed.
That was an awesome explanation
Maybe it’s a good idea to validate all questions. If I heard someone say that my question was silly, I probably would be discouraged from asking so many questions… and therefore seeing the light…eventually…with help!
Fantastic explanation! Would someone mind expounding a bit on what different intents might call for intern vs to_sym? That is, if I ever get to a point where I’m writing useful, legit-ass code, when might I want to stress the internal-representation aspect of converting to a symbol with intern?
Having more than one name for the same thing is simply “waste” in my eyes. It forces the user to remember all these names, and to understand that they actually do the same thing.
answer very good :) ty sir.i think i loved Ruby.trying go to pro Ruby