ruby安装完之后,打开文本编辑器,在Window系统就是记事本,输入,
print("Hello,Ruby!\n")
点击文件,选择另存为,这里保存在桌面,文件的名字改为helloruby.rb
点击开始运行,输入cmd,打开命令提示符,
然后返回桌面,点击刚才保存的文件,右键属性,
找到位置,点击复制。
返回cmd,输入cd C:\Documents and Settings\Administrator\桌面(这个是我刚才文件保存的路径,具体情况自己分析)
现在输入刚才在记事本写入的代码文件的名称helloruby.rb
看到了么,倒数的第二行,显示了“Hello,Ruby!”,虽然只有一行代码,但是对于Ruby编程是合法的,"Hello,Ruby!\n"就是ruby的字符串。
在Ruby中,有多种方法可以实现方法的动态调用。1.
使用send方法
第一种实现动态方法调用是使用send方法,send方法在Object类中定义,方法的第一个参数是一个符号用来表示所要调用的方法,后面则是所调用方法需要的参数。
“This
is
a
dog1″.send(:length)
=>
14
上面的代码中通过send方法去对一个字符串执行length操作,返回字符串的长度。
class
TestClass
def
hello(*args)
”Hello
”
+
args.join(‘
‘)
end
end
a
=
TestClass.new
puts
a.send
:hello,
“This”,
“is”,
“a”,
“dog!”
执行结果为:
Hello
This
is
a
dog!
2.
使用Method类和UnboundMethod类
另一种实现动态方法调用是使用Object类的method方法,这个方法返回一个Method类的对象。我们可以使用call方法来执行方法调用。
test1
=
“This
is
a
dog1″.method(:length)
test1.call
=>
14
class
Test
def
initialize(var)
@var
=
var
end
def
hello()
”Hello,
@var
=
#{@var}”
end
end
k
=
Test.new(10)
m
=
k.method(:hello)
m.call
#=>
“Hello,
@iv
=
99″
l
=
Test.new(‘Grant’)
m
=
l.method(“hello”)
m.call
#=>
“Hello,
@iv
=
Fred”
可以在使用对象的任何地方使用method对象,当调用call方法时,参数所指明的方法会被执行,这种行为有些像C语言中的函数指针。你也可以把method对象作为一个迭代器使用。
def
square(a)
a*a
end
mObj
=
method(:square)
[1,
2,
3,
4].collect(&mObj)
=>
[1
4
9
16]
Method对象都是和某一特定对象绑定的,也就是说你需要通过某一对象使用Method对象。你也可以通过UnboundMethod类创建对象,然后再把它绑定到某个具体的对象中。如果UnboundMethod对象调用时尚未绑定,则会引发异常。
class
Double
def
get_value
2
*
@side
end
def
initialize(side)
@side
=
side
end
end
a
=
Double.instance_method(:get_value)
#返回一个UnboundMethod对象
s
=
Double.new(50)
b
=
a.bind(s)
puts
b.call
执行结果为:
100
看下面一个更具体的例子:
class
CommandInterpreter
def
do_2()
“This
is
2
”
end
def
do_1()
“This
is
1
”
end
def
do_4()
“This
is
4
”
end
def
do_3()
“This
is
3
”
end
Dispatcher
=
{
?2
=>
instance_method(:do_2),
?1
=>
instance_method(:do_1),
?4
=>
instance_method(:do_4),
?3
=>
instance_method(:do_3)
}
def
interpret(string)
string.each_byte
{|i|
Dispatcher[i].bind(self).call
}
end
end
interpreter
=
CommandInterpreter.new
interpreter.interpret(’1234′)
执行结果为:
This
is
1
This
is
2
This
is
3
This
is
4
3.
使用eval方法
我们还可以使用eval方法实现方法动态调用。eval方法在Kernel模块中定义,有多种变体如class_eval,module_eval,instance_eval等。Eval方法将分析其后的字符串参数并把这个字符串参数作为Ruby代码执行。
str
=
“Hello”
eval
“str
+
‘
World!’”
=>
Hello
World!
sentence
=
%q{“This
is
a
test!”.length}
eval
sentence
=>
15
当我们在使用eval方法时,我们可以通过eval方法的第二个参数指明eval所运行代码的上下文环境,这个参数可以是Binding类对象或Proc类对象。Binding类封装了代码在某一环境运行的上下文,可以供以后使用。
class
BindingTest
def
initialize(n)
@value
=
n
end
def
getBinding
return
binding()
#使用Kernel#binding方法返回一个Binding对象
end
end
obj1
=
BindingTest.new(10)
binding1
=
obj1.getBinding
obj2
=
BindingTest.new(“Binding
Test”)
binding2
=
obj2.getBinding
puts
eval(“@value”,
binding1)
#=>
10
puts
eval(“@value”,
binding2)
#=>
Binding
Test
puts
eval(“@value”)
#=>
nil
可以看到上述代码中,@value在binding1所指明的上下文环境中值为10,在binding2所指明的上下文环境中值为Binding
Test。当eval方法不提供binding参数时,在当前上下文环境中@value并未定义,值为nil。
第一步:安装rvm为什么要安装rvm呢,因为rvm可以让你拥有多个版本的ruby,并且可以在多个版本之间自由切换。安装方式有两种:单用户模式和多用户模式。而
单用户和多用户的区别在于是否有root权限,单用户模式rvm会安装在~/.rvm目录下,而多用户模式则会安装在/usr/local/rvm目录
下。
单用户安装命令:
bash <<(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
多用户安装命令:
sudo bash <<(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
单用户需要设置环境变量,多用户安装重启就行了,确认rvm安装是否成功
在终端输入:
rvm -v
接着会看到rvm的版本信息如下:
rvm 1.19.6 () by Wayne E. Seguin [email protected]>, Michal Papis [email protected]>[https://rvm.io/]
第二步:安装ruby
查看是否安装gcc编译器
在开始安装ruby前请确认你是否安装了gcc,如果你安装了Xcode,请检查是否安装了command line tools,
打开Xcode,在Xcode ->Preference ->Download下,查看“Command Line
tools”这一项是否为“installed”
如下图:
如果不是installed,点击install安装或者下载OSX-GCC-Installer来安装gcc,不然在安装ruby编译的时候会报误(每个人的环境不一样报的错误也会不一样),如下图:
查看所有可以安装的Ruby版本
rvm list known
接着会看到各种版本的Ruby
安装Ruby
rvm install ruby --head
设置Ruby默认版本为1.9.3版本
rvm use 1.9.3 --default
第三步:更换源
查看已有的安装源
gem source
显示如下:
CURRENT SOURCES
http://rubygems.org/
修改更新源
gem update --system
gem uninstall rubygems-update
gem sources -r http://rubygems.org/
gem sources -a http://ruby.taobao.org