Scope modifiers: Difference between revisions
Content added Content deleted
(Added Bracmat example) |
(→{{header|Ruby}}: Added Ruby entry.) |
||
Line 974: | Line 974: | ||
return /*compliments to Jules Verne's Captain Nemo? */</lang> |
return /*compliments to Jules Verne's Captain Nemo? */</lang> |
||
=={{header|Ruby}}== |
|||
===Variables=== |
|||
The scope of a variable is decided by its first character(s). |
|||
$variable : global variable. Visible everywhere. Very seldom used.<br> |
|||
@@variable: class variable. Visible inside a class and it's instances. Very seldom used<br> |
|||
@variable : instance variable. Visible inside a class instance. Commonly used.<br> |
|||
variable : local variable. visible inside whichever comes first: a loop, a proc, a method, a class, a program. |
|||
===Methods=== |
|||
Instance methods may be public, private or protected<br> |
|||
A public method is visible and usable inside and outside an instance.<br> |
|||
A private method is internal to the class, and it can only be invoked from within the class (or subclass).<br> |
|||
A protected method is available within a class and available to instances of the same class.<br> |
|||
By default, methods are public. Use like this: |
|||
<lang ruby> |
|||
<pre> |
|||
class Demo |
|||
#public methods here |
|||
protected |
|||
#protect methods here |
|||
private |
|||
#private methods |
|||
end |
|||
</pre> |
|||
</lang> |
|||
Ruby is an open language. Declaring methods private prevents inadvertend use of methods not meant to be used outside a class. However it is easy to circumvent with metaprogramming methods like instance_eval. |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
===Variables=== |
===Variables=== |