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。
需要先安装ruby环境,在windows上下载RubyInstaller下一步下一步即可.
在cmd命令输入'ruby -v'输出版本号验证是否安装成功.
在vscode中运行ruby代码跟命令行执行步骤一样 ctrl+`打开终端输入ruby myfile.rb即可
如果想要在vscode中调试ruby代码 查看此篇文章 debug-ruby-in-vscode
上述情况仅针对于少量文件如果你的代码是一个网站应该考虑何种实现方式更换IDE来解决.
如果有所帮助,敬请采纳.
关于 RubyMotion 我已经写过很多文章了,但如何混用Objective-C与Ruby还从未涉及到。实际上你能在RubyMotion项目中使用Objective-C代码,也可以在传统Objective-C的App中使用Ruby代码。也许你一次听来觉得像黑魔法一样,所以来一起看看下面这些示例。Objective-C in Ruby
很多iOS开发者在现有的Objecive-C代码中存在大量深层的备份日志,这样就为项目手动转换为 RubyMotion 带来了很大的痛苦。然而幸运的是现在仅仅只需要将编译好的Objective-C代码添加到我们的新RubyMotion应用里。
比如我们要在 RubyMotion 应用中使用 Objective-C 形式的 CYAlert 类:
// CYAlert.h
#import <UIKit/UIKit.h>@interface CYAlert : NSObject
+ (void)show@end
// CYAlert.m
#import "CYAlert.h"
@implementation CYAlert
+ (void)show {
UIAlertView *alert = [[UIAlertView alloc] init]
alert.title = @"This is Objective-C"
alert.message = @"Mixing and matching!"
[alert addButtonWithTitle:@"OK"]
[alert show]
[alert release]
}@end
为了在RubyMotion应用中能正常使用,这里需要将CYAlert的两个文件都放置到类似 ./vendor/CYAlert 里面,然后在 Rakefile: 中告诉 RubyMotion 使用vendor_project创建那个文件夹。
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'MixingExample'
# ./vendor/CYAlert contains CYAlert.h and CYAlert.m
app.vendor_project('vendor/CYAlert', :static)
end
那么现在在 AppDelegate 中,我们这样使用 CYAlert:
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
CYAlert.show
true
end
end