1 # global variable 2 $ global_variable = 103 class class14 def print_global () 5 puts "global variable in class1 is # {$ global_variable}" 6 end7 end8 class1obj = class1.new9 class1obj. print_global ()
1 # instance variable 2 Class Customer 3 def initialize (ID, name, ADDR) 4 @ cust_id = ID 5 @ cust_name = Name 6 @ cust_addr = ADDR 7 end 8 def display_detalis 9 puts "Customer ID #{@ cust_id}" 10 puts "customer name #{@ cust_name} "11 puts" customer ADDR # {@ cust_addr} "12 end13 end14 cust1 = customer. new ("1", "John", "wisdom") 15 cust1.display _ detaching
1 # class variable 2 Class Customer 3 @ no_of_mers MERs = 0 4 def initialize (ID, name, ADDR) 5 @ cust_id = ID 6 @ cust_name = Name 7 @ cust_addr = ADDR 8 end 9 def display_details () 10 puts "Customer ID # {@ cust_id}" 11 puts "customer name # {@ cust_name}" 12 puts "customer ADDR # {@ cust_addr}" 13 end14 def total_no_of_mers MERs () 15 @ no_of_mers MERs + = 116 puts "totla Number of MERs: # {@@ no_of_customers}" 17 end18 end19 cust1 = customer. new ("1", "John", "wisdom") 20 cust2 = customer. new ("2", "Poul", "new") 21 22 cust1.total _ no_of_customers23 cust2.total _ no_of_mers MERs
Ruby learning-varaible