If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
[Tutorial] Ruby: Symbols
[Tutorial] Ruby: Symbols
If you thought of a Cheeseburger, that's fine.
If you thought of a McFlurry, that's fine.
If you thought of hot coffee, that's fine.
If you thought of a big company acting worldwide, that's fine.
It's fine, because the logo stands for all of it. It is not just a picture for a specific thing, it's the entirety of the McDonald's cosmos. A famous graphic designer described it like so: "A logo is a flag, a signature, an escutcheon, a street sign. A logo does not sell (directly), it identifies."
And indeed, a logo is the biggest part of "corporate identity". And if you understand, that one logo stands for several different things, as it identifies a whole company, you are already prepared to understand Ruby's symbols.
A symbol is a unique identifier. It is not a variable, it is not a string, although it reminds on a string. Everytime you write in your RubyEdit the following, it creates a new object.
Code: Select all
"Hallo" #first object
"Hallo" #second object
"Hallo" #third object, etcCode: Select all
@greet = "Hallo" #"hallo" is created, let's ID it as object_1
@greet #points to object_1
@greet #points to object_1
@greet #points to object_1, etcCode: Select all
@greet = "moin" #"moin" is created, let's ID it as object_2
@greet #points to object_2
@greet #points to object_2
@greet #points to object_2, etcCode: Select all
@var_a = "moin" #object moin is created and var_a associated with it
@var_b = @var_a #a second variable is associated with object moin
@var_b.upcase! #an exclamation mark tells you the method is changing the object itself
#both, @var_a and @var_b now point to object moin, which reads "MOIN" instead of "moin"Code: Select all
:greet #a symbol is created
:greet = "moin" #syntax error, a symbol is immutable, it is not a variableCode: Select all
Symbol.all_symbols:Class
:"core#set_method_alias"
:[]=
and even the name of the class Symbol, :Symbol
Now you might think, that symbols still are much like strings. Maybe not in how they are handled by Ruby, but in their behavior. And yes, that's not wrong. A symbol is an identifier, and as such needs a descriptive name, which is almost as if it were a string. And you can indeed convert a symbol to a string, and a string to a symbol.
Code: Select all
@moin = :greet.to_s #symbol was converted and is now the string object "greet"
@sym = @moin.to_sym #string converted to symbol object :greetBut of course there are advantages. For once, there's only ever one symbol of the same label, which means a small lookup table where it can be found much quicker. Then they are immutable and therefore instantiating them is faster than instantiating strings. Execution time can be reduced as much as to a third of the strings' time.
The best use case for explaining symbols is Hashes, as they use key - value pairs. In other languages (they don't have symbols), you have to use strings as keys.
Code: Select all
names = {"forename" => "Jessica"}Code: Select all
names = { :forename => "Jessica" } #standard way of creating key-value pair
names = { forename: "Jessica" } #symbol specific way of creating key-value pairIn short, keep the strings for users of your app/plugin. Keep the symbols in your code, use them whenever you need an identifier and save time over strings.
Re: [Tutorial] Ruby: Symbols
Thank you for taking the time to explain this so well.
Re: [Tutorial] Ruby: Symbols
One confusing thing was the mention of 'double colons'. But I didn't see them [or missed it].
I was then looking for [::] that would be 2 colons.
Again, I'll need to re-read.
Always appreciate your sharing of knowledge Tulamide !
Thank-you
- HughBanton
- Posts: 268
- Joined: Sat Apr 12, 2008 3:10 pm
- Location: Evesham, Worcestershire
- Contact:
Re: [Tutorial] Ruby: Symbols
However, if at any point I thought of a MacCheeseburger .. that's NOT so fine
H
Re: [Tutorial] Ruby: Symbols
... with cheese
NO KETCHUP !
Re: [Tutorial] Ruby: Symbols
There are quite a few. For example, when defining a class, you normally go through the painfully boring routine of creating getter and setter methods.HughBanton wrote:Yes thanks indeed for putting this together, much appreciated. I feel compelled to find me an application ...
Code: Select all
class Test
def intialize
@forename = ""
end
##getter
def forename
return @forename
end
##setter
def forename=(obj)
@forename = obj
end
end
names = Test.new
names.forename = "Rex"
names.forenameCode: Select all
class Test
attr_accessor :forename
end
names = Test.new
names.forename = "Rex"
names.forenameCode: Select all
def addsub(*args)
return args[0] + args[1][:add] - args[1][:subtract]
end
addsub(4, add: 3, subtract: 2)EDIT: My bad! In German this sign ":" is called a "Doppelpunkt" (double dot). I mixed German and English here and talked about "double-colon", where it has to be a "colon", of course!
Re: [Tutorial] Ruby: Symbols
Hey, I only speak English ... so I can focus 'messing up' to my full potential.
[yeah ... took Spanish and French in High School ... many eons ago].