Welcome to Rainy's blog!

Ruby学习总结六

上一篇 / 下一篇  2008-01-29 17:44:49 / 个人分类:Ruby

Summary Six
1. A symbol looks like a variable name but it's prefixed with a colon.
2. You can think of :id as meaning the name of the variable id, and plain id as meaning the value of the variable.
3. Symbols are useful because a given symbol name refers to the same object throughout a Ruby program.
4. Symbols can be considered constants without values.
5. Symbols are more efficient than strings. Two strings with the same contents are two different objects, but for any
given name there is only one Symbol object. This can save both time and memory.
6. When do we use a string versus a symbol?
o If the contents (the sequence of characters) of the object are important, use a string.
o If the identity of the object is important, use a symbol.
7. A Symbol object is created by prefixing an operator, string, variable, constant, method, class, module name with a colon.
8. If Fred is a constant in one context, a method in another, and a class in a third, the Symbol :Fred will be the same object in all three contexts.
9. Ruby gives you three levels of protection:
o public - methods can be called by everyone. A class's instance methods are public by default.
o protected - methods can be invoked only by objects of the defining class and its subclasses.
o private - methods cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object. The initialize method is always private.
10. Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.
11. Top-level methods are private instance methods of the Kernel module.
12. attr_reader is reader only; attr_writer is writer only and attr_accessor is both reader and writer.
13. Hashes are similar to arrays in that they are indexed collection of object references. However, while you index
arrays with integers, you can index a hash with objects of any types: strings, regular expressions, and so on.
14. When you store a value in a hash, you actually supply two objects - the index (normally called the key) and the value.
15. Hash elements are not ordered, so you cannot easily use a hash as a stack or a queue.
16. nil is returned when an attempt is made to access keys that do not exist in the hash.
17. The Time class contains Ruby's interface to the set of time libraries written in C.
18. Time zero for Ruby is the first second GMT of January 1, 1970.
19. Ruby's DateTime class is superior to Time for astronomical and historical applications, but you can use Time for most everyday programs.

TAG: Ruby

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-30  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 23641
  • 日志数: 33
  • 图片数: 5
  • 书签数: 2
  • 建立时间: 2006-12-05
  • 更新时间: 2008-03-22

RSS订阅

Open Toolbar