DEV Community

pooyaalamdari
pooyaalamdari

Posted on

global variable

instance variable

@var = "I am instance var"
class C
    def examine_var
        puts @var
    end
end

c = C.new 
c.examine_var
Enter fullscreen mode Exit fullscreen mode

global variable

$gvar = "I am a global var"
class C
    def examin_global
        puts $gvar
    end
end

c = C.new 
c.examin_global
Enter fullscreen mode Exit fullscreen mode
class Person 
    def whole_name
        n = $first_name + " "
        n << "#{middle_name} " if $middle_name
        n << $last_name
    end
end

david = Person.new 

$first_name = "Pooya"
$last_name = "Ghobadi"

puts david.whole_name
Enter fullscreen mode Exit fullscreen mode

Top comments (0)