1 2 3 4 5 6 7 8 9 | class MyTest class << self def test puts "This is a class static method" end end end MyTest.test # works fine |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | irb(main):198:0> class MyTest irb(main):199:1> end => nil irb(main):200:0> y = MyTest.new => #< MyTest:0x2d43fe0> irb(main):201:0> z = class MyTest irb(main):202:1> class << self irb(main):203:2> self irb(main):204:2> end irb(main):205:1> end => #<Class: MyTest > irb(main):206:0> z.class => Class irb(main):207:0> y.class => MyTest |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | irb(main):094:0> class SelfTest3 irb(main):095:1> def foo irb(main):096:2> self.bar() irb(main):097:2> end irb(main):098:1> def bar irb(main):099:2> puts "Testing Self" irb(main):100:2> end irb(main):101:1> end => nil irb(main):102:0> test = SelfTest3.new => #<SelfTest3:0x2d15750> irb(main):103:0> test.foo Testing Self => nil |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | irb(main):110:0> class SelfTest4 irb(main):111:1> def method1 irb(main):112:2> self.method2 irb(main):113:2> end irb(main):114:1> def method3 irb(main):115:2> method2 irb(main):116:2> end irb(main):117:1> private irb(main):118:1> def method2 irb(main):119:2> puts "Inside private method" irb(main):120:2> end irb(main):121:1> end => nil irb(main):122:0> y = SelfTest4.new => #<SelfTest4:0x2c13d80> irb(main):123:0> y.method1 NoMethodError: private method `method2' called for #<SelfTest4:0x2c13d80> from (irb):112:in `method1' irb(main):124:0> y.method3 Inside private method => nil |
1 2 3 4 | irb(main):104:0> self => main irb(main):105:0> self.class => Object |
1 2 3 4 5 6 7 8 9 10 11 | irb(main):135:0> class Test irb(main):136:1> def method_missing(method, *args) irb(main):137:2> puts "Method: #{method} Args: (#{args.join(', ')})" irb(main):138:2> end irb(main):139:1> end => nil irb(main):140:0> t = Test.new => #<Test:0x2c7b850> irb(main):141:0> t.f(23) Method: f Args: (23) => nil |
1 2 3 4 5 6 7 8 9 10 | irb(main):142:0> class Test irb(main):143:1> def method1(s, y) irb(main):144:2> puts "S: #{s} Y: #{y}" irb(main):145:2> end irb(main):146:1> end => nil irb(main):147:0>t = Test.new irb(main):148:0> t.send(:method1, 23, 12) S: 23 Y: 12 => nil |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | irb(main):258:0> class SendTest irb(main):259:1> private irb(main):260:1> def hello irb(main):261:2> puts "Saying Hello privately" irb(main):262:2> end irb(main):263:1> end => nil irb(main):264:0> y = SendTest.new => #< SendTest:0x2cc52c0> irb(main):265:0> y.hello NoMethodError: private method `hello' called for #< SendTest:0x2cc52c0> from (irb):265 irb(main):266:0> y.send(:hello) Saying Hello privately => nil |
1 2 3 4 5 6 7 8 9 | begin f = File.open("ruby.txt") # .. continue file processing rescue ex => Exception # .. handle errors, if any ensure f.close unless f.nil? # always execute the code in ensure block end |
1 2 3 4 5 6 7 | irb(main):185:0> catch :label do irb(main):186:1* puts "This will print" irb(main):187:1> throw :label irb(main):188:1> puts "This will not print" irb(main):189:1> end This will print => nil |
1 2 3 4 5 6 7 8 9 10 | irb(main):190:0> catch :label do irb(main):191:1* catch :label1 do irb(main):192:2* puts "This will print" irb(main):193:2> throw :label irb(main):194:2> puts "This won't print" irb(main):195:2> end irb(main):196:1> puts "Neither will this print" irb(main):197:1> end This will print => nil |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #!/usr/bin/env ruby def func(id, count) i = 0; while (i < count) puts "Thread #{i} Time: #{Time.now}" sleep(1) i = i + 1 end end puts "Started at #{Time.now}" thread1 = Thread.new{func(1, 100)} thread2 = Thread.new{func(2, 100)} thread3 = Thread.new{func(3, 100)} thread4 = Thread.new{func(4, 100)} thread1.join thread2.join thread3.join thread4.join puts "Ending at #{Time.now}" |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |