Friday, May 25, 2012

Ruby 1.9.3 - defined?

defined?
It's a ruby method for checking passed expression defined or not

Some examples

1.9.3p0 :025 > message = "hello" => "hello" 1.9.3p0 :031 > defined? message => "local-variable" 1.9.3p0 :032 > if defined? message 1.9.3p0 :033?> puts "message defined" 1.9.3p0 :034?> end message defined
1.9.3p0 :050 > defined? $_ => "global-variable"

1.9.3p0 :052 > defined? lastname => nil

1.9.3p0 :053 > books = [] => [] 1.9.3p0 :054 > defined? books => "local-variable"

1.9.3p0 :055 > name = "" => "" 1.9.3p0 :056 > defined? name => "local-variable" 1.9.3p0 :057 > author = "Steve" => "Steve" 1.9.3p0 :058 > defined? author => "local-variable" 1.9.3p0 :059 > object = String.new => "" 1.9.3p0 :060 > defined? object => "local-variable"

I hope you understand defined? method from these examples.

No comments:

Post a Comment