ruby的变量和常量 ruby的变量和常量含有对象的引用。变量本身没有内在的类型。变量的类型仅仅由变量引用的对象所能响应的消息决定。 ruby中常量也是对对象的引用。常量在第一次被赋值的时候创建,和别的语言不同,ruby允许你改变常量的值。[code]OUTER_CONST = 99 class Const def get_const p "-ssssssssssssssssss---" CONST end p "------------------" CONST = OUTER_CONST + 1 end p Const.new.get_const p Con...
ruby为我们提供了一些全局变量,我们在程序中可以随时调用,熟悉这些变量可以在我们编写程序时为我们提供很多的便利。 全局变量名称 说明 $@ The location of the latest error. (最近一次出错的位置,不知道这里的出错指的什么?Exception?还是error?) $_(特殊) The string last read by gets. (gets从用户输入所获取的最后一个字符串) $. ...
由于平时主要使用Java语言进行开发,对Java比较熟悉,因此我决定学习ruby就从ruby对象相关的知识入手,以便能尽快上手。今天首先了解一下ruby的类中都有哪些类型的变量和常量,以及他们与Java的不同。 ruby中共有5种变量:local variable, instance variable, class variable, global variable以及constant。这几种变量的写法都有规定的格式,local variable以小写字母或者下划线开头,intance variable以@开头,clas...
ruby的变量有局部变量、全局变量、实例变量、类变量、常量。 (一)局部变量 局部变量有局部作用域限制(例如一个block内),大家编程时,经常这样写: [quote]irb(main):001:0> i=123 => 123 irb(main):002:0> s="hi" => "hi"[/quote] 这里i和s就是局部变量。 ruby的变量是动态变量,某个变量在前一刻是数字型,在后一刻可以是字符型: [quote]irb(main):003:0> x=321 => 321 irb(main):004:0> x="hello" => "hello"[/quote...
PHP 和 ruby 的基本常量变量、类的简单书写 PHP 变量、常量 变量:$var = "1000"; 常量:define("ROOT","/tmp"); 类常量:const aaa = 10; 类变量:同样还是$xxx; 全局变量: global $xxx; $GLOBALS['xxxx'] ruby 变量、常量 变量:aa = 10 或者 _a = 10 小写字母或者下划线开头 常量:Ma = 10 实例变量:@hello = 10 类变量:@@t = 20 全局变量:$aa = 10 PHP基本的类实例化 Php代码[code]
RT!
#!/usr/bin/ruby
#
class Dog
def set_aname(aName)
@name=aName
end
def get_name
return @name
end
end
adog=Dog.new
adog.set_aname("dahuang")
puts adog.name
#puts adog.get_name
#END
报错
5.rb:14: undefined method `name' for #