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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值