简单教程
提交运行
代码编辑器:
# !/usr/bin/ruby -w # -*- encoding:utf-8 -*- # filename: main.rb # author: 简单教程(www.twle.cn) # Copyright © 2015-2065 www.twle.cn. All rights reserved. $global_variable = 13 class HelloWorld def print_global puts "全局变量在 HelloWorld 中输出为 #$global_variable" end end class HelloWorld2 def print_global puts "全局变量在 HelloWorld 中输出为 #$global_variable" end end class1obj = HelloWorld.new class1obj.print_global $global_variable = 17 class2obj = HelloWorld2.new class2obj.print_global $global_variable = 19 puts "全局变量为 : #$global_variable"
运行结果: