#!/usr/bin/ruby $global_variable = 10 class Class1 def print_global puts "Global variable in Class1 is #$global_variable" end end class Class2 def print_global puts "Global variable in Class2 is #$global_variable" end end class1obj = Class1.new class1obj.print_global class2obj = Class2.new class2obj.print_global
Global variable in Class1 is 10 Global variable in Class2 is 10
#!/usr/bin/ruby
class Customer
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
end
# 创建对象
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
# 调用方法
cust1.display_details()
cust2.display_details()
Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Customer id 2 Customer name Poul Customer address New Empire road, Khandala
#!/usr/bin/ruby
class Customer
@@no_of_customers=0
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
def total_no_of_customers()
@@no_of_customers += 1
puts "Total number of customers: #@@no_of_customers"
end
end
# 创建对象
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
# 调用方法
cust1.total_no_of_customers()
cust2.total_no_of_customers()
Total number of customers: 1 Total number of customers: 2
#!/usr/bin/ruby
class Example
VAR1 = 100
VAR2 = 200
def show
puts "Value of first Constant is #{VAR1}"
puts "Value of second Constant is #{VAR2}"
end
end
# 创建对象
object=Example.new()
object.show
Value of first Constant is 100 Value of second Constant is 200
123 # Fixnum 十进制 1_234 # Fixnum 带有下划线的十进制 -500 # 负的 Fixnum 0377 # 八进制 0xff # 十六进制 0b1011 # 二进制 ?a # 'a' 的字符编码 ?\n # 换行符(0x0a)的编码 12345678901234567890 # Bignum
123.4 # 浮点值 1.0e6 # 科学记数法 4E20 # 不是必需的 4e+20 # 指数前的符号
#!/usr/bin/ruby -w puts 'escape using "\\"'; puts 'That\'s right';
escape using "\" That's right
#!/usr/bin/ruby -w
puts "Multiplication Value : #{24*60*60}";
Multiplication Value : 86400
#!/usr/bin/ruby ary = [ "fred", 10, 3.14, "This is a string", "last element", ] ary.each do |i| puts i end
fred 10 3.14 This is a string last element
#!/usr/bin/ruby
hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }
hsh.each do |key, value|
print key, " is ", value, "\n"
end
green is 240 red is 3840 blue is 15
#!/usr/bin/ruby (10..15).each do |n| print n, ' ' end
10 11 12 13 14 15
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有