This forum is now read-only. Please use our new forums at discuss.codecademy.com.

2760 points
B9c1685f32cec1df1527d6134fceeb96?s=140&d=retro
Submitted by
wonka
about 6 years ago

Intern and to_sym?

  1. Why have multiple ways to do the same things?
  2. Is one more efficient with memory?
  3. Why not just use the one that is more efficient, can they each do something different?

53 votes

permalink

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 5*2 or 5+5, or (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_sym tells 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 Array#collect, String#succ (successor) is the same as String#next, Hash#each is the same as Hash#each_pair, and Numeric#abs is the same as Numeric#magnitude.

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.

3117 points
Picture
Submitted by
Alex J
about 6 years ago

9 Comments

525e33d05118a85f05e3bc266987889e?s=140&d=retro larrysellers about 6 years ago

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.

52dc922a52f8630a5a001008_322480677 ninjakid almost 6 years ago

you certainly write a lot Alex J. I've seen other things you've written and have been very impressed.

Picture Malcolm clark almost 6 years ago

That was an awesome explanation

46c5125b88743e83095f07962af2acae?s=140&d=retro Vigipoti over 5 years ago

Thanks.

525f2923548c352b49000020_205397364 spydermonkee over 5 years ago

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!

  • Excellent explanation, btw

C60414ec939f53a638a15bd5b6abe799?s=140&d=retro Alex Gaw over 5 years ago

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?

01a26986e94ce1a103e75afb739b443d?s=140&d=retro Vladimir Hadzic about 5 years ago

Thank you!

50b126894e8f7279929bd4bb0d0f39ad?s=140&d=retro NswHacker about 5 years ago

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.

Picture Phan Mạnh Cường almost 5 years ago

answer very good :) ty sir.i think i loved Ruby.trying go to pro Ruby


2 votes

permalink

symbols = Array.new
strings.each do |x|
symbols.push(x.intern)
end

1066 points
51b13f4f1782161144ced451d5228ac6?s=140&d=retro
Submitted by
Shaii_Goldzz .
about 5 years ago


1 vote

permalink

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

Add your code below!

symbols = [] 

strings.each do |x| 
    symbols.push(x.intern ) 
end

1809 points
2fd48dd1aebfedc2732e9791806e286d?s=140&d=retro
Submitted by
Gabriel César
about 5 years ago


0 votes

permalink

@Alex J, very nice and easy to understand explanation. Off topic, what then would be best first time programming language?

285 points
31d7f1a76157019babf2fe8dc3db3cc5?s=140&d=retro
Submitted by
branimir sever
over 5 years ago

1 Comment

C5be4747210bf91117d2a0c8331ef36a?s=140&d=retro mhmd trbls about 5 years ago

c


0 votes

permalink

:one => 1,
:two => 2, # Fill in these two blanks!
:three => 3,

1066 points
51b13f4f1782161144ced451d5228ac6?s=140&d=retro
Submitted by
Shaii_Goldzz .
about 5 years ago


0 votes

permalink

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

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?

443 points
5286db15548c35d70300182d_364305898
Submitted by
Paul Noecker
almost 5 years ago


-19 votes

permalink

Thnask

338 points
521a9955548c35e0a1000b70_672368482
Submitted by
SIRTEDDYIII
over 5 years ago