This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by wonka
over 10 years

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?

Answer 512b84f89113314f6000600b

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.

points
Submitted by Alex J
over 10 years

9 comments

larrysellers over 10 years

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.

ninjakid almost 10 years

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

Malcolm clark almost 10 years

That was an awesome explanation

Vigipoti almost 10 years

Thanks.

spydermonkee over 9 years

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
Alex Gaw over 9 years

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?

Vladimir Hadzic about 9 years

Thank you!

NswHacker about 9 years

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.

Phan Mạnh Cường almost 9 years

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

Answer 535954e8282ae337850001ea

2 votes

Permalink

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

points
Submitted by Shaii_Goldzz .
about 9 years

Answer 5321afb98c1ccc79080037a9

1 vote

Permalink

strings = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]

Add your code below!

symbols = [] 

strings.each do |x| 
    symbols.push(x.intern ) 
end
points
Submitted by Gabriel César
about 9 years

Answer 52f805c5282ae3783800084c

0 votes

Permalink

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

points
Submitted by branimir sever
over 9 years

1 comments

mhmd trbls about 9 years

c

Answer 53595431548c35b24600019a

0 votes

Permalink

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

points
Submitted by Shaii_Goldzz .
about 9 years

Answer 53e2b4ea9c4e9d6ded0004e7

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?

points
Submitted by Paul Noecker
almost 9 years

Answer 5252cd8d548c353be301123a

-19 votes

Permalink

Thnask

points
Submitted by SIRTEDDYIII
over 9 years