自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實(shí)例講解Ruby線程局部域變量

開發(fā) 開發(fā)工具
Ruby線程局部域變量痛其他變量一樣都是以$開頭。比如:$!和$@等。一般情況下是用來(lái)處理異常等的操作。下面將會(huì)對(duì)此做詳細(xì)介紹。

我們知道,在Ruby語(yǔ)言中存在一些內(nèi)置變量,這些變量實(shí)現(xiàn)的功能不盡相同。下面就讓我們一起來(lái)看看有關(guān)Ruby線程局部域的一些介紹。#t#

Ruby線程局部域變量之$!

最近發(fā)生的異常的信息.由raise設(shè)定.

  1. def exception   
  2. begin   
  3. raise "exception test."   
  4. ensure   
  5. puts $!   
  6. end   
  7. end   
  8. exception  

 

結(jié)果:

 

  1. simple.rb:58:in `exception': 
    exception test. (RuntimeError)   
  2. from simple.rb:64   
  3. exception test. # $!中的值  

 

 

Ruby線程局部域變量之$@

以數(shù)組形式保存著發(fā)生異常時(shí)的back trace信息. 數(shù)組元素是字符串,它顯示了方法調(diào)用的位置,其形式為
"filename:line"或 "filename:line:in `methodname'" 。在向$@賦值時(shí),$!不能為nil。 

  1. def exception   
  2. begin   
  3. raise "exception test."   
  4. ensure   
  5. puts $@   
  6. puts "$@ size is:#{$@.size}"   
  7. end   
  8. end   
  9. exception  

 

結(jié)果:

  1. simple.rb:58:in `exception': 
    exception test. (RuntimeError)   
  2. from simple.rb:65   
  3. simple.rb:58:in `exception' #$@中的值,
    是一個(gè)數(shù)組,第一個(gè)元素是錯(cuò)誤發(fā)生的行數(shù),
    第二個(gè)是異常的內(nèi)容。下面打印了數(shù)組的長(zhǎng)度   
  4. simple.rb:65   
  5. $@ size:2   

 

責(zé)任編輯:曹凱 來(lái)源: javaeye.com
相關(guān)推薦

2009-12-14 14:07:02

Ruby局部域變量

2009-12-15 10:48:54

Ruby局部變量

2009-12-18 14:35:01

Ruby奇特變量

2009-09-22 17:21:24

線程局部變量

2010-03-15 19:37:00

Java多線程同步

2009-12-14 09:33:04

Ruby安裝

2009-12-15 11:01:31

Ruby數(shù)組

2012-11-12 09:26:06

.NET多線程

2009-12-15 15:28:22

Ruby重載

2009-12-15 14:58:19

Ruby迭代器

2009-12-16 14:24:48

Ruby函數(shù)lambd

2009-12-14 15:30:43

安裝Ruby on R

2009-12-16 15:41:10

Ruby on Rai

2009-12-16 17:37:31

Ruby on Rai

2024-07-09 08:35:09

2010-06-03 18:22:38

Hadoop

2010-09-14 17:20:57

2011-04-02 16:37:26

PAT

2009-12-15 09:56:51

Ruby流程控制

2009-09-17 13:05:38

Linq局部變量類型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)