RubyµÄself
selfÉÏÏÂÎÄ
RubyµÄselfÓкÍJavaµÄthisÏàËÆÖ®´¦£¬µ«ÓÖ´ó²»Ïàͬ¡£JavaµÄ·½·¨¶¼ÊÇÔÚʵÀý·½·¨ÖÐÒýÓã¬ËùÒÔthisÒ»°ã¶¼ÊÇÖ¸Ïòµ±Ç°¶ÔÏóµÄ¡£¶øRubyµÄ´úÂëÖðÐÐÖ´ÐУ¬ËùÒÔÔÚ²»Í¬µÄÉÏÏÂÎÄ(context)self¾ÍÓÐÁ˲»Í¬µÄº¬Ò壬ÏÈÀ´¿´¿´³£¼ûµÄcontext self¶¼´ú±íÄÄЩ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Õâ¸öλÖÃλÓÚtop level context£¬´ú±íObjectµÄĬÈ϶ÔÏómain
p self # => main
p self.class # => Object
@self1 = self
# ÒòΪËùÓÐ×Ô¶¨ÒåÀ඼λÓÚmain contextÖ®ÖУ¬ËùÒÔÕâÊÇObjectµÄʵÀý·½·¨
# ͬʱҲ¿ÉÒÔ˵ÊÇÒ»¸öÈ«¾Ö·½·¨
def a_method
@self2 = self
p self
# => main£¬ÒòΪÓÐÁËʵÀý±äÁ¿@self1ºÍ@self2£¬ËùÒÔ´òÓ¡³öÀ´µÄ²»ÊÇmainÕâ¸ö×Ö·û
# => µ«ÈÔÈ»ÊÇmain¶ÔÏó£¬×¢Ê͵ô4£¬8Ðм´¿É¿´µ½Ð§¹û
p @self1 == @self2 # => true
end
# ÏÂÃæÊÇÒ»¸ö¹ØÓÚÀàÖв»Í¬ÉÏÏÂÎĵÄself
class Person
p self # => Person£¬´ú±íµ±Ç°Àà
def instance_method
p self # => #<Person:0xb7818fdc>£¬´ú±íµ±Ç°ÀàµÄʵÀý
end
def self.class_method
p self # => Person£¬ºÍµÚ16ÐÐÒ»Ñù´ú±íµ±Ç°ÀࣨÕâÊÇÀà·½·¨µÄcontext£©£¬ËüÃÇÊÇÏàµÈµÄ
end
end
m = Person.new
def m.hello
p self # => ´ú±ímÕâ¸öµ¥Àý¶ÔÏó
end
m.hello
ÉÏÃæֻдÁËÔÚÀàÖеÄself£¬ÆäʵÔÚmoduleÒ²ÊÇÒ»ÑùµÄ¡£Í¨¹ýÉÏÃæ´úÂëÄã¿ÉÒÔ·¢ÏÖ£¬selfÒ»Ö±ÒýÓÃ×ÅËüËùÔÚλÖÃÉÏÏÂÎĵÄʵÀý/Àà¡£
selfÏÔʽ/Òþʽ
Äã¿ÉÒÔÏÈÊÔ×ÅÔËÐÐÏÂÃæ´úÂ룬¿´¿´ÓÐʲôÒâÍâ·¢ÉúûÓÐ
1
2
3
4
5
6
7
8
9
10
11
12
13
class Person
attr_accessor :name
def set_name(your_name)
name = your_name
end
end
m = Person.new
p m.name
m.set_name('today')
p m.name # => ²ÂÊÇʲô
Èç¹ûÄã²ÂÊÇtoday¾Í´ó´íÌØ´íÁË£¬´ð°¸ÊÇnil£¬ÎªÊ²Ã´ÊÇnilÄØ£¬ÔÚµÚ5ÐУ¬ÎÒÃ÷Ã÷µ÷ÓõÄÊÇattr_accessorÉú³ÉµÄname=·½·¨¸³ÖµµÄ°¡£¬Äã¿ÉÒÔÔÚÇ°Ãæ¼ÓÉÏselfÊÔÊÔ£¬´úÂëÈçÄãÔ¤ÆÚµÄÒ»ÑùÖ´ÐÐÁË¡£ÔÚÕâÖÖÇé¿öÏÂname = your_name²¢Ã»ÓÐÈ¥µ÷ÓÃattr_accessorÉú³ÉµÄxx=·½·¨£¬¶øÊǽ«nameµ±×÷ÁËÒ»¸ö¾Ö²¿±äÁ¿£¬Èç¹ûÏÔʽµÄÖ¸¶¨self£¬¾ÍûÓÐÎÊÌâÁ
Ïà¹ØÎĵµ£º
¿ÉÓÿ⣺getoptlong.rs£¬ optionparser
¶ÔÓ¦ÀࣺGetoptLong, OptionParser
Ç°ÕßÒѹýʱ£¬½¨ÒéʹÓúóÕߣ¬ÇÒºóÕß±ÈÇ°ÕßÒ×Óá£
ºóÕßÌص㣺
1. ²ÎÊýÃèÊöºÍ´úÂë´¦ÀíдÔÚÒ»Æð
2. Êä³ö²ÎÊý˵Ã÷£¬²»Ðèµ¥¶Àά»¤
3. ¿ÉÑ¡²ÎÊýºÍÃüÁî²ÎÊýÃèÊö¼ò½à
4. ²ÎÊý¿É×Ô¶¯×ª»»ÎªÌض¨µÄÀà
5. ......
1£¬°²×°ruby
ÏÂÔصØÖ·£º http://rubyinstaller.org/download.html
2£¬°²×°rails
ʹÓÃÃüÁîÏÂÔØ£ºÔÚÃüÁîÐÐÔËÐÐgem install rails --remote
2£¬³õ²½Á˽ârubyµÄÎÄÕÂ
http://www.bcbbs.net/html/29671.html
http://www.bcbbs.net/dev/List64.aspx ......
RubyºÍPythonµÄÓï·¨±È½Ï
ÆäʵRubyºÍPython·Ç³£½Ó½ü£¬±È´ó¶àÊý±ðµÄÓïÑÔÒª½Ó½üµÄ¶à£¬ËùÒÔϲ»¶ÓÃɶ¾ÍÓÃɶ£¨´óʵ»°£¬ËäȻҲÊÇ·Ï»°£©¡£Óï·¨ÉϵIJî±ðËäÈ»ÓÐÄÇôһµã£¬´ó²¿·ÖÊÇsyntax sugar£¬ÎÒ¶·µ¨ÉÔ΢Áм¸¸ö£¨pythonÎÒÒ²ÍüµÃ²î²»¶àÁË£¬²»¶ÔµÄ´ó¼Ò¾¡¹ÜÀ´±Þʬ°É£©£¬µ«ÊÇÖ÷Òª²îÒ컹ÊÇÉè¼Æ˼ÏëÉϵģºÁé»î ......