See the Troubleshooters.Com Bookstore.
:mynameAnother way to make a symbol is with a colon followed by a quoted string, which is how you make a symbol whose string representation contains spaces:
:'Steve was here and now is gone'The preceding is also a symbol. Its string representation is:
"Steve was here and now is gone"
#!/usr/bin/env ruby
|
sssss[slitt@mydesk slitt]$ ./test.rb |
:myname = "steve"If you were to try that, you'd get the following error message:
[slitt@mydesk slitt]$ ./test.rb |
mystring = :steveTOr this:
mystring = :steveT.to_sOr this:
myint = :steveT.to_iOr this:
attr_reader :steveTNow you at least know what we're talking about. Naturally, you still have plenty of questions. Read on...
A Ruby symbol is a thing that has both a number
(integer) representation and a string representation. |
#!/usr/bin/env ruby
|
[slitt@mydesk slitt]$ ./test.rb |
#!/usr/bin/env ruby |
[slitt@mydesk slitt]$ ./test.rb |
#!/usr/bin/env ruby |
[slitt@mydesk slitt]$ ./test.rb |
[slitt@mydesk slitt]$ ./test.rb |
[slitt@mydesk slitt]$ ./test.rb |
[#!/usr/bin/env ruby |
[slitt@mydesk slitt]$ ./test.rb |
#!/usr/bin/env ruby |
[slitt@mydesk slitt]$ ./test.rb |
#!/usr/bin/env ruby |
[slitt@mydesk slitt]$ ./test.rb |
attr_reader :lengthYou're naming both a get method (length()) and an instance variable (@length).
attr_writer :lengthOr you can do the avant-garde:
attr_writer "length"Both preceding code statements create a setter method called length, which in turn creates an instance variable called @length. If this seems like magic to you, then keep in mind that the magic is done by attr_writer, not by the symbol. The symbol (or the string equivalent) just functions as a string to tell attr_writer what it should name the method it creates, and what that method should name the instance variable it creates.
#!/usr/bin/env ruby |
[slitt@mydesk slitt]$ ./test.rb |