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。
map 和 select 可以说是ruby枚举方法中最重要也最有用的两个方法,如果你已经学习并了解Array和Hash,你一定会觉得他们非常漂亮和灵活,这只是ruby这个美女一半的风情而已......每个人都有自己习惯的添加、删除、访问数据的方法,也都有实现#each迭代的风格各异的版本,但是,你知道吗?使得Array和Hash在Ruby中真正强大的原因,是因为它们可以使用各种枚举(Enumerable)的方法,比如map和select。三个PHP调用系统命令函数的区别与联系我们在执行linux系统的shell命令时,会用到PHP调用系统命令函数来实现。那么在这些函数中,主要包括了system(),exec(),passthru()这三个经常用于外部命令调用的函数。
虽然这三个命令都能执行linux系统的shell命令,但是其实他们是有区别的:
system() 输出并返回最后一行shell结果。
exec() 不输出结果,返回最后一行shell结果,所有结果可以保存到一个返回的数组里面。
passthru() 只调用命令,把命令的运行结果原样地直接输出到标准输出设备上。
相同点:都可以获得命令执行的状态码
在PHP中调用外部命令,可以用如下三种方法来实现:
用PHP提供的专门函数
PHP提供共了3个专门的执行外部命令的PHP调用系统命令函数:system(),exec(),passthru()。
system()
原型:string system (string command [, int return_var])
system()函数很其它语言中的差不多,这个PHP调用系统命令函数执行给定的命令,输出和返回结果。第二个参数是可选的,用来得到命令执行后的状态码。
例子:
system("/usr/local/bin/webalizer/webalizer")
exec()
原型:string exec(string command [, string array [, int return_var]])
exec()函数与system()这个PHP调用系统命令函数类似,也执行给定的命令,但不输出结果,而是返回结果的最后一行。虽然它只返回命令结果的最后一行,但用第二个参数array 可以得到完整的结果,方法是把结果逐行追加到array的结尾处。所以如果array不是空的,在调用之前最好用unset()最它清掉。只有指定了第二个参数时,才可以用第三个参数,用来取得命令执行的状态码。
例子:
exec("/bin/ls -l")
exec("/bin/ls -l", $res)
exec("/bin/ls -l", $res, $rc)
passthru()
原型:void passthru (string command [, int return_var])
passthru ()只调用命令,这个PHP调用系统命令函数不返回任何结果,但把命令的运行结果原样地直接输出到标准输出设备上。所以passthru()函数经常用来调用象pbmplus (Unix下的一个处理图片的工具,输出二进制的原始图片的流)这样的程序。同样它也可以得到命令执行的状态码。
例子:
header("Content-type: image/gif")
passthru("./ppmtogif hunte.ppm")
六种用ruby调用执行shell命令的方法
碰到需要调用操作系统shell命令的时候,Ruby为我们提供了六种完成任务的方法:
1.Exec方法:
Kernel#exec方法通过调用指定的命令取代当前进程:
例子:
$ irb
>>exec 'echo "hello $HOSTNAME"'
hello nate.local
$
值得注意的是,exec方法用echo命令来取代了irb进程从而退出了irb。主要的缺点是,你无法从你的ruby脚本里知道这个命令是成功还是失败。
2.System方法。
Kernel#system方法操作命令同上, 但是它是运行一个子shell来避免覆盖当前进程。如果命令执行成功则返回true,否则返回false。
$ irb
>>system 'echo "hello $HOSTNAME"'
hello nate.local
=>true
>>system 'false'
=>false
>>puts $?
256
=>nil
>>
3.反引号(Backticks,Esc键下面那个键)
$ irb
>>today = `date`
=>"Mon Mar 12 18:15:35 PDT 2007n"
>>$?
=>#<Process::Status: pid=25827,exited(0)>
>>$?.to_i
=>0
这种方法是最普遍的用法了。它也是运行在一个子shell中。
4.IO#popen
$ irb
>>IO.popen("date") { |f| puts f.gets }
Mon Mar 12 18:58:56 PDT 2007
=>nil
5.open3#popen3
$ irb
>>stdin, stdout, stderr = Open3.popen3('dc')
=>[#<IO:0x6e5474>, #<IO:0x6e5438>, #<IO:0x6e53d4>]
>>stdin.puts(5)
=>nil
>>stdin.puts(10)
=>nil
>>stdin.puts("+")
=>nil
>>stdin.puts("p")
=>nil
>>stdout.gets
=>"15n"
6.Open4#popen4
$ irb
>>require "open4"
=>true
>>pid, stdin, stdout, stderr = Open4::popen4 "false"
=>[26327, #<IO:0x6dff24>, #<IO:0x6dfee8>, #<IO:0x6dfe84>]
>>$?
=>nil
>>pid
=>26327
>>ignored, status = Process::waitpid2 pid
=>[26327, #<Process::Status: pid=26327,exited(1)>]
>>status.to_i
=>256