Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

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£¬¾ÍûÓÐÎÊÌâÁ


Ïà¹ØÎĵµ£º

Rubyѧϰ±Ê¼ÇÈý——Àà


#Ò»¡¢¶¨ÒåÒ»¸öÀà
class Person
  def initialize(name,age=18)
    @name=name;
    @age=age;
    @motherland="china";
  end
  def talk
    puts "my name is "+@name+" and I am "+@age.to_s
   &nb ......

21±¾¾­µäruby railsµç×ÓÊé


Beginning Ruby from Novice To Perfessional

Head First Rails

Agile Web Development with Rails 3rd Edition(new)

Agile Web Development with Rails 3rd Edition

Agile Web Development with Rails 3rd Editio ......

ruby Á¬½Ó²Ù×÷ sql2005


The following is improved version of the code created by David Mullet, from
http://rubyonwindows.blogspot.com/2007/03/ruby-ado-and-sqlserver.html
require 'win32ole'
class SqlServer
# This class manages database connection and queries
attr_accessor :connection, :data, :fields
attr_wr ......

RubyÉùÒô


ת Adding Sound to Your Ruby Apps
Have you ever thought about including sounds in your Ruby application? Used sparingly, sound may enhance your applications by adding audio cues or a custom touch. You could, for example, play a beep or chime that announces the completion of a lengthy process. Per ......

rubyºÃÏñÓÐÖ¸Õë°¡£¡£¡£¡

b = "123"
a = b
b.gsub!(/2/,"")
puts a
µÃµ½µÄ½á¹ûÊÇ13£¬·¢ÏÖÈç¹ûa = bµÄ»°Ã²ËÆÖ»ÊÇ°ÑbµÄÖ¸Õë¸øÁËa£¬µ«ÊÇÈç¹ûbµÄÖµ·¢ÉúÁ˱仯a²ÅÄÜ´ÓbÖжÀÁ¢³öÀ´¡£
Èç¹û°ÑÉÏÃæµÄ´úÂë¸Ä³É
b = "123"
a = "#{b}"
b.gsub!(/2/,"")
puts a
ÔòµÃµ½µÄ½á¹ûΪ123£¬ÕâÀïÊÇ°ÑbµÄÖµ¸øÁ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ