Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
51730792
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
51730792
编写于
11月 19, 2008
作者:
P
Pratik Naik
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added Object#try. ( Taken from
http://ozmm.org/posts/try.html
) [Chris Wanstrath]
上级
51a19ae2
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
40 addition
and
0 deletion
+40
-0
activesupport/CHANGELOG
activesupport/CHANGELOG
+2
-0
activesupport/lib/active_support/core_ext/object/misc.rb
activesupport/lib/active_support/core_ext/object/misc.rb
+13
-0
activesupport/test/core_ext/object_and_class_ext_test.rb
activesupport/test/core_ext/object_and_class_ext_test.rb
+25
-0
未找到文件。
activesupport/CHANGELOG
浏览文件 @
51730792
*2.3.0 [Edge]*
* Added Object#try. ( Taken from http://ozmm.org/posts/try.html ) [Chris Wanstrath]
* Added Enumerable#none? to check that none of the elements match the block #1408 [Damian Janowski]
* TimeZone offset tests: use current_period, to ensure TimeZone#utc_offset is up-to-date [Geoff Buesing]
...
...
activesupport/lib/active_support/core_ext/object/misc.rb
浏览文件 @
51730792
...
...
@@ -71,4 +71,17 @@ def with_options(options)
def
acts_like?
(
duck
)
respond_to?
"acts_like_
#{
duck
}
?"
end
# Tries to send the method only if object responds to it. Return +nil+ otherwise.
#
# ==== Example :
#
# # Without try
# @person ? @person.name : nil
#
# With try
# @person.try(:name)
def
try
(
method
)
send
(
method
)
if
respond_to?
(
method
,
true
)
end
end
activesupport/test/core_ext/object_and_class_ext_test.rb
浏览文件 @
51730792
...
...
@@ -247,3 +247,28 @@ def test_instance_exec_nested
[
arg
]
+
instance_exec
(
'bar'
)
{
|
v
|
[
reverse
,
v
]
}
}
end
end
class
ObjectTryTest
<
Test
::
Unit
::
TestCase
def
setup
@string
=
"Hello"
end
def
test_nonexisting_method
method
=
:undefined_method
assert
!
@string
.
respond_to?
(
method
)
assert_nil
@string
.
try
(
method
)
end
def
test_valid_method
assert_equal
5
,
@string
.
try
(
:size
)
end
def
test_valid_private_method
class
<<
@string
private
:size
end
assert_equal
5
,
@string
.
try
(
:size
)
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录