The Ruby_Newbie Guide to Symbols

Copyright (C) 2005 by Steve Litt

See the Troubleshooters.Com Bookstore.

Note: All materials in Ruby Revival are provided AS IS. By reading the materials in Ruby Revival you are agreeing to assume all risks involved in the use of the materials, and you are agreeing to absolve the authors, owners, and anyone else involved with Ruby Revival of any responsibility for the outcome of any use of these materials, even in the case of errors and/or omissions in the materials. If you do not agree to this, you must not read these materials.

CONTENTS

Introduction

What do symbols look like?



What do they resemble in other languages?

How are symbols implemented?



What are symbols?

What are symbols not?



What can symbols do for you?

What are the advantages of symbols?



What do symbols look like?

:myname



:'Steve was here and now is gone'

"Steve was here and now is gone"

#!/usr/bin/env ruby

puts :'I love Ruby.'

puts :'I love Ruby.'.to_i

sssss[slitt@mydesk slitt]$ ./test.rb

I love Ruby.

10263

[slitt@mydesk slitt]$



:myname = "steve"



[slitt@mydesk slitt]$ ./test.rb

./test.rb:37: parse error, unexpected '=', expecting $

:myname = "steve"

^

[slitt@mydesk slitt]$



mystring = :steveT



mystring = :steveT.to_s



myint = :steveT.to_i



attr_reader :steveT



What do they resemble in other languages?

How are symbols implemented?

ruby

What are symbols?

A Ruby symbol is a thing that has both a number (integer) representation and a string representation.



contain

:mysymbol.to_i

:mysymbol.to_s

to_s

#!/usr/bin/env ruby



puts :steve

puts :steve.to_s

puts :steve.to_i

puts :steve.class



puts()

[slitt@mydesk slitt]$ ./test.rb

steve

steve

10257

Symbol

[slitt@mydesk slitt]$



to_s

#!/usr/bin/env ruby

:steve = "Big Steve"



[slitt@mydesk slitt]$ ./test.rb

./test.rb:2: parse error, unexpected '=', expecting $

:steve = "Big Steve"

^

[slitt@mydesk slitt]$



#!/usr/bin/env ruby

:steve.to_s = "Big Steve"



[slitt@mydesk slitt]$ ./test.rb

./test.rb:2: undefined method `to_s=' for :steve:Symbol (NoMethodError)

[slitt@mydesk slitt]$



[slitt@mydesk slitt]$ ./test.rb

./test.rb:2: undefined method `to_i=' for :steve:Symbol (NoMethodError)

[slitt@mydesk slitt]$



[slitt@mydesk slitt]$ ./test.rb

./test.rb:2: undefined method `to_i=' for :steve:Symbol (NoMethodError)

[slitt@mydesk slitt]$



to_i

to_a

[#!/usr/bin/env ruby



puts :myvalue.object_id

puts :myvalue.object_id

puts "myvalue".object_id

puts "myvalue".object_id



[slitt@mydesk slitt]$ ./test.rb

2625806

2625806

537872172

537872152

[slitt@mydesk slitt]$



:myvalue

"myvalue"

A Ruby symbol is a thing that has both a number (integer) representation and a string representation.

that has both a number (integer) representation and a string representation. The string representation is much more important and used much more often.



The value of a Ruby symbol's string part is the name of the symbol, minus the leading colon.

A Ruby symbol cannot be changed at runtime.

Multiple uses of the same symbol have the same object ID and are the same object.



What are symbols not?

A Symbol is Not a String

capitalize

center

#!/usr/bin/env ruby

mystring = :steve.capitalize

puts mystring





[slitt@mydesk slitt]$ ./test.rb

./test.rb:2: undefined method `capitalize' for :steve:Symbol (NoMethodError)

[slitt@mydesk slitt]$



#!/usr/bin/env ruby

mystring = :steve.to_s.capitalize

puts mystring



[slitt@mydesk slitt]$ ./test.rb

Steve

[slitt@mydesk slitt]$



A Symbol is not (Just) a Name

attr_reader :length



length()

@length

are

A Symbol is an Object, but So What?

What can symbols do for you?

The string needn't be changed at runtime. The string doesn't need methods of class String.



.to_s

attr_writer :length



attr_writer "length"

length

@length

attr_writer

attr_writer

attr_writer

#!/usr/bin/env ruby



def make_me_a_setter(thename)

eval <<-SETTERDONE

def #{thename}(myarg)

@#{thename} = myarg

end

SETTERDONE

end



class Example

make_me_a_setter :symboll

make_me_a_setter "stringg"



def show_symboll

puts @symboll

end



def show_stringg

puts @stringg

end

end



example = Example.new

example.symboll("ITS A SYMBOL")

example.stringg("ITS A STRING")

example.show_symboll

example.show_stringg



make_me_a_setter

attr_writer

make_me_a_setter

:symboll

"stringg"

example.symboll("ITS A SYMBOL")

[slitt@mydesk slitt]$ ./test.rb

ITS A SYMBOL

ITS A STRING

[slitt@mydesk slitt]$



What are the advantages and disadvantages of symbols?

:mysymbol

:mysymbol

:mysymbol

:mysymbol

Summary

one true way