what are Symbols and how are they useful?

Started by Ryex, December 13, 2009, 11:55:59 pm

Previous topic - Next topic

Ryex

title is self explanatory. I've been using them in attr_accessor and attr_reader statements ever since i started but I never really understood what they were
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

G_G

attr_accessor can always be accessed and changed. So its pretty much allowed to be Read and to Write to it.
attr_reader is something you can only Read.

For example.
attr_accessor :allow_save
attr_reader :gold

You can change and read allow_save but you can only read and not change gold.

Ryex

yes yes I know that. I referring to the

:foo

in the

attr_accessor :foo

the :foo is a symbol. but what IS a symbol? how is  it useful? and why should I care?
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

legacyblade

I didn't know it was called a symbol. But to explain it:

classes can have what is called an instance variable. Every version of that class you make (for instance, the game_actor class) will have it's own set of those instance variables. By default, only that particular instance of the class can access its instance variables (only arshes can see his HP, MP, Strength, etc. No menu would be able to change or even SEE those variables) Adding


attr_accessor :hp

will allow other classes to read and change the actor's hp instance variable. This makes battle, as well as menu scenes possible. If you DON'T use that, you can't really do anything other than call methods which change the actors variables (think "def changehp").

Ryex

your missing the question...
I know all about instance variables i was merely using then as an example of when a symbolize is used a symbol is any thing with a : if front much like a global is and thing with a $ in front and a instance variable is any thing with a @ in front. but other than attr_ stuff why and how are symbol used? and what are they? (in terms of the questions what is a string? or what is a global variable?)
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

Symbols are a special type of variable. i.e. You can call the method of a class, but you can't access the actual method definition. Using the symbol of the method you actually can access the method object itself and not call the method.

class A
  def method_name
  end
  module_function :method_name
end


Aliasing actually works on the symbols. Aliases can be defined like this as well:

class A
  def method_name
  end
  alias :method_name2 :method_name
end


Symbols are mostly used for reflection (object metadata) I'd say.
I don't think this would work, but it kinda demonstrates what you can use symbols for.

class A
  def method_name(a, b, c)
  end
  print :method_name.argument_number # prints 3 because of a, b and c
end

Check out our game brands:

Daygames
Game Night Games
Chugnar Games

Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Zeriab

No matter where in the program you are :my_symbol is the same as :my_symbol anywhere else. You can call them singleton variables.
You can for example use them to send signals:
case object.do_something
when :success
  p 'It was successful!'
when :failure
  p 'It failed...'
when :error
  p 'It gave an error D:'
else
  raise Exception.new('Signal error')
end


It is quick, dirty and possible the syntax for the exception is wrong, but it should give you another idea of how to use symbols.

*hugs*

Ryex

hmm it seems that symbols are mainly used fore more advanced programing.
so could you uses them to define methods like

def :do_something
  do_stuff
end
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

Check out our game brands:

Daygames
Game Night Games
Chugnar Games

Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

Check out our game brands:

Daygames
Game Night Games
Chugnar Games

Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.