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
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.
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 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").
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?)
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
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*
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
Sure.
bout there wouldn't be much point right?
It's the same thing.