From: "Jesús Gabriel y Galán" Date: 2010-10-11T18:56:45+09:00 Subject: Re: Some newbie questions - bare with me ;) On Mon, Oct 11, 2010 at 11:35 AM, egervari wrote: > Question 1 > I was curious what ":" before a identifier was exactly. It seems that > it's often used as a map key... but what is it exactly? > > Printing :bob to the screen results in "bob"... but testing for > equality between :bob and "bob" results in a falsehood. > > I figure this has to be some literal notation for something, but what? It's a Symbol. http://ruby-doc.org/core/classes/Symbol.html A Symbol is an object of which there only exists one. Every time you use the literal :bob, it's the same object. They are usually used as Hash keys, as flags, and so on. > Question 2 > In RSpec, it uses == for equality. I overrode the eql?() method in > object, but == doesn't use it. I am used to languages providing an > equals() method that you can override. So if you want 1:1 equals, you > must use eql? and == is just comparing the object id's? So does that > mean that the literal 1 uses the same object no matter where that > literal is used? > > It just seems a little weird to me that if everything is an object... > then my assumption would be that == would compare the actual state and > not the object references like it does in Java. Can you clear this up? > Thanks! Check this: http://ruby-doc.org/core/classes/Object.html methods ==, eql? and equals? Also this: http://blog.rubybestpractices.com/posts/rklemme/018-Complete_Class.html There's a very good explanation about the equivalence concepts and best practices in Ruby. > Question 3 > Ruby seems to @ in front of an identifier to denote that it's an > instance variable. I'm curious, what is the reason for this? And if > these variables are in fact private to the object, is there some kind > of short-hand to create getter/setters?, or do you have to define > these 2 def's yourself? I guess if that's the case, it's no worse than > in Java, but Scala provides several ways to get around this. I'm > curious if such shorthands exist. attr_accessor :a # creates methods a= and a attr_reader # creates method a attr_writer # creates method a= Jesus.