15, జులై 2025, మంగళవారం

Ruby..............4th B.TECH

 There are different types of variables in Ruby:

  • Local variables
  • Instance variables
  • Class variables
  • Global variables
Each variable in Ruby is declared by using a special character at the start of the variable name which is mentioned in the following table:
SymbolType of Variable
[a-z] or _Local Variable
@Instance Variable
@@Class Variable
$Global Variable

C:\Users\Dell>ruby -v

ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x64-mingw-ucrt]

 

C:\Users\Dell>gem -v

3.6.7

 

C:\Users\Dell>ruby -e "puts'HELLO ruby '"

HELLO ruby

 

C:\Users\Dell>irb

irb(main):001> "hellow world of ruby"

=> "hellow world of ruby"

irb(main):002> puts

irb(main):003> puts "hellow ram a dayianboyina"

 

hellow ram a dayianboyina

=> nil

irb(main):004> 1+2

=> 3

irb(main):005> puts "enter name "

enter name

=> nil

irb(main):006> puts "ramu"

ramu

=> nil

irb(main):007> puts "enter name"

enter name

=> nil


#!/usr/bin/ruby
# Ruby program to illustrate 
# the Class  Variables

class Customer
    
# class variable
 @@no_of_customers = 0
 
 def initialize(id, name, addr)
     
# An instance Variable
 @cust_id = id
 @cust_name = name
 @cust_addr = addr
 end

# displaying result 
 def display_details()
 puts "Customer id #@cust_id"
 puts "Customer name #@cust_name"
 puts "Customer address #@cust_addr"
 end
 
 def total_no_of_customers()
     
# class variable
 @@no_of_customers += 1
 puts "Total number of customers: #@@no_of_customers"
    end
end

# Create Objects
cust1 = Customer.new("1", "ram", "ram's Apartments, hyderabad")
cust2 = Customer.new("2", "dayina", "New Empire road, chirala")

# Call Methods
cust1.display_details()
cust1.total_no_of_customers()
cust2.display_details()
cust2.total_no_of_customers()
custmour id 1, 
custmour name ram
custmour address  ram's Apartments, hyderabad
Customer id 2
Customer name dayina
Customer address New Empire road, chirala

"2", "dayina", "New Empire road, chirala",

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి