Ruby的數據類型

Ruby Types:String,Number,Symbol,Array,Hash

1.Strings

String Literals:
String含義:
One way to create a String is to use single or double quotes inside a Ruby program to create what is called a string literal
用單引號,或雙引號包括一段ruby程序來創建一個字符串
puts 'Hello world'
puts "Hello world"
Double quotes allow you to embed variables or Ruby code inside of a string literal – this is commonly referred to as interpolation.
雙引號內允許你插入變量或一段代碼,這是很常用的。
def my_method(name)
     puts "Your name is #{name}"
end

Interpolation
Notation
#{expression}
Expression can be almost arbitrary Ruby expression
表達式可以任意的ruby表達式。
If variable that is referenced by #{expression} is not available (has not been assigned), a NameError exception will be raised:
如果與#{expression}表達式相關的變量不存在,將拋出一個NameError異常:
"trying to print #{undefined} variable"    
 NameError: undefined local variable or method `undefined' for main:Object

Escape Sequences
轉義字符
/" – double quote
// – single backslash
/a – bell/alert
/b – backspace
/r – carriage return
/n – newline
/s – space
/t – tab

puts and print
puts automatically prints out a newline after the text
puts將在輸出後自動打印換行
>> puts "Say", "hello"
Say
hello
print function only prints out a newline if you specify one
print函數不會自動打印換行
>> print "Say", "hello", "/n"
Sayhello

% Notation
%w causes breaks in white space to result in an array
%w根據空格來轉換成數組
%w(a b c)
=> ["a", "b", "c"]


2.Hash
Hashes are basically the same as arrays, except that a hash not only contains values, but also keys pointing to those values.
Hashes和數組一樣基礎,但是一個hash不僅有values還有指向values的keys
Each key can occur only once in a hash.
key不可重複
A hash object is created by writing Hash.new or by writing an optional list of comma-separated key => value pairs inside curly braces
一個hash對象可由Hash.new創建,也可以按如下格式創建:
hash_one   = Hash.new
hash_two   = {}                             # shorthand for Hash.new
hash_three = {"a" => 1, "b" => 2, "c" => 3}

Hash and Symbol
Usually Symbols are used for Hash keys (allows for quicker access), so you will see hashes declared like this:
標識符通常用於Hash的keys(可提高調用速度),你將會看到如下聲明:
hash_sym   = { :a => 1, :b => 2, :c => 3}  


3.Symbols
What is Symbol?
A Ruby symbol is the internal representation of a name.
Ruby的標識符就是一個內部的代表名。
It is a class in Ruby language along with
以下是對一個類的引用
:my_value.class      #=> Symbol
You construct the symbol for a name by preceding the name with a colon.
你可以構造一個表示符通過在名字前面加冒號
:my_symbol
Atomic, immutable and unique
標識符是不可變且唯一的
Can't be parsed or modified
不能分拆和修改
All references to a symbol refer to the same object
所有同一個標識符的引用指向同一個對象
:my_value.equal?(:my_value)    #=> true
"my_value".equal?("my_value") #=> false

Symbols vs. Strings
Symbols 和 Strings
Always interchangeable with strings
Symbols和strings是有關聯的
In any place you use a string in your Ruby code, you can use a symbol
在任何用strng的Ruby代碼裏,都能用symbol
Important reasons to use a symbol over a string
用symbol而不用string的原因
If you are repeating same string many times in your Ruby code, let's say 10000 times, it will take 10000 times of memory space of the string while if you are using a symbol, it will take a space for a single symbol
如果你在代碼裏用同一個string很多次,假設10000次,這將比用symbol耗費的內存多10000倍。
Minor reasons to use a symbol over a string
用symbol替代string的次要原因
Symbol is easier to type than string (no quotes to type)
symbol比string好書寫
Symbol stands out in the editor
symbol可被編輯器識別
The different syntax can distinguish keys from values in hash
不同的語法可以區別keys從values在hash
:name => 'Brian'

Where Do Symbols Typically Used?
Symbols are often used as
symbols常用的地方:
Hash keys (:name => 'Brian', :hobby => 'golf')‏
Arguments of a method (:name, :title)‏
Method names (:post_comment)
Symbols are used in Rails pervasively
symbols在Rails裏應用廣泛


本文翻譯自SUN的官方文檔Ruby Basics,該文檔由SUN整理自http://en.wikibooks.org/wiki/Ruby_Programming
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章