当前位置:编程学习 > 网站相关 >>

Ruby on Rails 入门之:(16) Ruby中的定义操作、取消定义操作

判断某个表达式或者变量是否意境定义,可以使用define?语言。如果表达式已经定义会返回一个保存表达式类型的字符串,如果表达式没有定义,那么会返回false

 

 

[ruby]
#encoding:gbk  
class Test 
    def Play 
        puts "Play method"; 
        puts defined?"a>3"; 
    end 
end 
 
a = Test.new 
puts defined?a.Play; 
a.Play; 
 
puts defined?a.Show(); 

#encoding:gbk
class Test
 def Play
  puts "Play method";
  puts defined?"a>3";
 end
end

a = Test.new
puts defined?a.Play;
a.Play;

puts defined?a.Show();
上面输出的结果为:

 

 

[html]
watkins@watkins:~/temp/workspace/ruby$ ruby define.rb  
method 
Play method 
expression 
nil 
watkins@watkins:~/temp/workspace/ruby$  

watkins@watkins:~/temp/workspace/ruby$ ruby define.rb
method
Play method
expression
nil
watkins@watkins:~/temp/workspace/ruby$

释放已经定义的表达式或者变量需要使用undef

 

 

[ruby]


#encoding:gbk  
class Test 
    def Play 
        puts "Play method"; 
        puts defined?"a>3"; 
    end 
end 
 
a = Test.new 
puts defined?a.Play; 
a.Play; 
 
puts defined?a.Show(); 
 
puts "************************"; 
 
undef Play; 
puts defined?a.Play; 

#encoding:gbk
class Test
 def Play
  puts "Play method";
  puts defined?"a>3";
 end
end

a = Test.new
puts defined?a.Play;
a.Play;

puts defined?a.Show();

puts "************************";

undef Play;
puts defined?a.Play;


输出结果为:


[html] view plaincopyprint?

Play method 
expression 
nil 
************************ 
define.rb:17: undefined method `Play' for class `Object' (NameError) 
watkins@watkins:~/temp/workspace/ruby$  

watkins@watkins:~/temp/workspace/ruby$ ruby define.rb
method
Play method
expression
nil
************************
define.rb:17: undefined method `Play' for class `Object' (NameError)
watkins@watkins:~/temp/workspace/ruby$


 

补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,