Symbols in Ruby – Short Explanation?

When I started learning Ruby and Ruby on Rails I was introduced to the concept of symbols. You are using a lot of symbols in Rails and it feels somewhat natural after a while but when you try to explain newbies what these symbols are actually good for, it is really hard to describe. It is especially hard because you could also use strings instead. At that point you don’t actually even know the difference between a string and a symbol is or what symbols are even good for.

I just played with the interactive ruby shell => irb and found out a short way of describing what symbols are good for:


$ irb
irb(main):001:0> "foo".object_id
=> 36730
irb(main):002:0> "foo".object_id
=> 23400
irb(main):003:0> "foo".object_id
=> 10710
irb(main):004:0> :foo.object_id
=> 145858
irb(main):005:0> :foo.object_id
=> 145858
irb(main):006:0> :foo.object_id
=> 145858
irb(main):007:0>

If you would use strings to name an object in some kind of association, you would actually create new objects every time, which is quite unnecessary. Symbols are just an efficient way to point to objects or pass arguments to a method or association without creating new string objects all over the place. It is like saying »The variable or object call :foo«. Hmm got it?

I know – it is probably a no-brainer, but I had difficulties explaining it properly.

Aus Grün mach Rot

Vor ein paar Jahren, als man Bündnis ’90 / Die Grünen noch ernstnehmen konnte war die Abschaltung von AKW’s noch ein Nischenwunsch. Heute schreibt sich die SPD dieses Ziel auf die Flagge. Wenn man diese Politiker und deren Versprechen doch nur ernstnehmen könnte. Passend zum Thema auch ein schönes Fundstück bei Fefe (omfg! ich verlinke auf Fefe’s Blog)

SPD will AKWs abschalten

Ruby on Rails debugging with breakpoints

I’m currently working on the software (called CCCash) which will be used to sell the tickets at the Chaos Comunication Camp 2007. During the development process we had a strange bug which we couldn’t really figure out. Pandur, who is also working on the software and who is more experienced than me in terms of RoR used breakpoints to figure this one out. I haven’t read the Pragmatic book this far so I didn’t know that this was even possible. It worked just as you would expect it. See the Ruby on Rails wiki for more informations. The more I dig into Ruby on Rails, the more I like it.

SelfHTML auf dem Chaos Communication Camp

Eben erst in meinem Feedreader entdeckt: SelfHTML WebDevVilage auf dem Chaos Comunication Camp 2007

Ich bin auf dem Camp und werde wohl mal vorbeischauen – vielleicht interessiert es ja auch den einen oder anderen meiner werten Leser. Lohnt sich sicher!

Trick 17 für »Gefährder«

Hausdurchsuchung

Aus der Micky Maus.