Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: Ryex on December 13, 2009, 11:55:59 pm

Title: what are Symbols and how are they useful?
Post by: Ryex on December 13, 2009, 11:55:59 pm
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
Title: Re: what are Symbols and how are they useful?
Post by: G_G on December 14, 2009, 12:11:30 am
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.
Title: Re: what are Symbols and how are they useful?
Post by: Ryex on December 14, 2009, 12:24:47 am
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?
Title: Re: what are Symbols and how are they useful?
Post by: legacyblade on December 14, 2009, 01:13:39 am
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").
Title: Re: what are Symbols and how are they useful?
Post by: Ryex on December 14, 2009, 01:34:14 am
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?)
Title: Re: what are Symbols and how are they useful?
Post by: Blizzard on December 14, 2009, 08:58:25 am
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

Title: Re: what are Symbols and how are they useful?
Post by: Zeriab on December 16, 2009, 03:36:05 am
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*
Title: Re: what are Symbols and how are they useful?
Post by: Ryex on December 16, 2009, 09:45:58 pm
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
Title: Re: what are Symbols and how are they useful?
Post by: Blizzard on December 17, 2009, 04:17:23 am
Sure.
Title: Re: what are Symbols and how are they useful?
Post by: Ryex on December 17, 2009, 02:20:53 pm
bout there wouldn't be much point right?
Title: Re: what are Symbols and how are they useful?
Post by: Blizzard on December 17, 2009, 02:39:22 pm
It's the same thing.