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

幾種Ruby self應(yīng)用方法介紹

開發(fā) 開發(fā)工具
Ruby self對于一個Ruby語言初學(xué)者來說還是比較生疏的。我們希望大家可以通過這篇文章幫助大家提高對Ruby語言的了解程度。

Ruby語言通常被人們理解為是一種解釋型腳本語言。在對Ruby語言的學(xué)習(xí)中,我們需要不斷的從編碼實踐中去總結(jié)經(jīng)驗,幫助我們提高編程能力。#t#

Ruby self在不同的環(huán)境中有不同的含義,這點和java的this不同,原因是java實際上只有一種環(huán)境--在class的實例方法定義中使用,代表訪問這個方法參數(shù)自動傳進的那個對象。

而由于ruby作為一個完全純凈的面向?qū)ο笳Z言,任何東東都是對象,方法是對象,類也是對象...,所以Ruby self就會有很多環(huán)境,區(qū)分不同環(huán)境的self含義才能更好的理解程序的含義。

一、Top Level Context

puts self

打印出main,這個代表Object的默認(rèn)對象main.

二、在class或module的定義中:

在class和module的定義中,self代表這個class或這module對象: 

  1. class S   
  2. puts 'Just started class S'   
  3. puts self   
  4. module M   
  5. puts 'Nested module S::M'   
  6. puts self   
  7. end   
  8. puts 'Back in the 
    outer level of S'   
  9. puts self   
  10. end  

輸出結(jié)果: 

  1. >ruby self1.rb   
  2. Just started class S   
  3. Nested module S::M   
  4. S::M   
  5. Back in the outer level of S   
  6. >Exit code: 0  

三、在實例的方法定義中:

這點和java的this代表的東東一樣:程序自動傳遞的調(diào)用這個方法的對象

 

  1. class S   
  2. def m   
  3. puts 'Class S method m:'   
  4. puts self   
  5. end   
  6. end   
  7. s = S.new   
  8. s.m  

運行結(jié)果:

  1. >ruby self2.rb   
  2. Class S method m:   
  3. #<S:0x2835908>   
  4. >Exit code: 0  

四、在單例方法或者類方法中:

單例Ruby self方法是針對一個對象添加的方法,只有這個對象擁有和訪問這個方法,這時候self是擁有這個方法的對象:

  1. # self3.rb   
  2. obj = Object.new   
  3. def obj.show   
  4. print 'I am an object: '   
  5. puts "here's self inside a 
    singleton method of mine:"   
  6. puts self   
  7. end   
  8. obj.show   
  9. print 'And inspecting obj 
    from outside, '   
  10. puts "to be sure it's the
     same object:"   
  11. puts obj  

運行結(jié)果: 

 

  1. ruby self3.rb   
  2. I am an object: here's self 
    inside a singleton method of mine:   
  3. #<Object:0x2835688>   
  4. And inspecting obj from outside,
     to be sure it's the same object:   
  5. #<Object:0x2835688>   
  6. >Exit code: 0  

在類方法中Ruby self代表這個類對象: 

  1. # self4.rb   
  2. class S   
  3. def S.x   
  4. puts "Class method 
    of class S"   
  5. puts self   
  6. end   
  7. end   
  8. S.x  

運行結(jié)果:

  1. >ruby self4.rb   
  2. Class method of class S   
  3. >Exit code: 0  

從上面的例子我們可以看出不管是Ruby self還是java的this都表示在當(dāng)前的環(huán)境下你可以訪問的當(dāng)前的或者默認(rèn)的對象。

責(zé)任編輯:曹凱 來源: jb51.net
相關(guān)推薦

2009-12-17 11:14:50

Ruby on Rai

2009-12-16 15:14:43

Ruby on Rai

2009-12-17 17:13:23

Ruby for Ec

2009-12-15 15:19:30

Ruby訪問控制

2009-12-14 16:26:40

Ruby復(fù)制文件

2009-12-16 13:48:06

Ruby Web開發(fā)框

2009-12-16 15:04:26

Ruby實現(xiàn)strea

2009-12-15 17:04:56

Ruby使用HTTP協(xié)

2009-12-16 14:04:04

Ruby對象初始化

2009-12-15 14:46:04

Ruby類常量

2009-12-24 16:11:07

WPF圖像處理

2009-12-14 13:56:12

Ruby特點

2009-12-10 17:02:50

PHP站點性能

2021-07-07 05:53:23

PythonPython 語法加密源代碼

2011-06-16 10:48:33

session

2009-12-17 17:46:26

Ruby編寫問題

2009-12-22 17:30:47

WCF Address

2009-12-23 18:06:25

WPF模板

2009-12-14 13:06:08

Ruby數(shù)字類型

2009-12-14 18:30:59

Ruby DSL特點
點贊
收藏

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