Ruby 如何使用像字符串一样的行为?红宝石色
我正在阅读ruby中的Ruby 如何使用像字符串一样的行为?红宝石色,ruby,Ruby,我正在阅读ruby中的acts\u like\u string?,但找不到任何关于如何使用的示例。我试过了,但运气不好 puts str.acts_like_string? 这些是在我的gems文件夹中搜索类似于字符串的行为的结果,grep--recursive--context 2类似于字符串的行为$(gem-environment-gemdir): activesupport-4.1.0.rc1/lib/active\u support/core\u ext/string/behavior
acts\u like\u string?
,但找不到任何关于如何使用的示例。我试过了,但运气不好
puts str.acts_like_string?
这些是在我的gems文件夹中搜索类似于字符串的行为的结果,
grep--recursive--context 2类似于字符串的行为$(gem-environment-gemdir)
:
activesupport-4.1.0.rc1/lib/active\u support/core\u ext/string/behavior.rb-class string
activesupport-4.1.0.rc1/lib/active_support/core_ext/string/behavior.rb-#在类似字符串的类上启用更可预测的duck键入。请参见对象的行为?。
activesupport-4.1.0.rc1/lib/active\u support/core\u ext/string/behavior.rb:def的行为与字符串类似?
activesupport-4.1.0.rc1/lib/active\u support/core\u ext/string/behavior.rb-true
activesupport-4.1.0.rc1/lib/active\u support/core\u ext/string/behavior.rb-结束
--
activesupport-4.1.0.rc1/lib/active\u support/multibyte/chars.rb-字符串的别名
activesupport-4.1.0.rc1/lib/active\u support/multibyte/chars.rb-
activesupport-4.1.0.rc1/lib/active\u support/multibyte/chars.rb:delegate:,:=~,:像字符串一样行动?,:to=>:wrapped\u字符串
activesupport-4.1.0.rc1/lib/active\u support/multibyte/chars.rb-
activesupport-4.1.0.rc1/lib/active_support/multibyte/chars.rb-#通过包装_string#创建一个新的chars实例。
--
mail-2.5.4/lib/mail/multibyte/chars.rb-
mail-2.5.4/lib/mail/multibyte/chars.rb-#在类似字符串的类上启用更可预测的duck键入。请参见对象的行为?。
mail-2.5.4/lib/mail/multibyte/chars.rb:def的行为与字符串类似?
mail-2.5.4/lib/mail/multibyte/chars.rb-true
mail-2.5.4/lib/mail/multibyte/chars.rb-结束
如您所见,的行为类似于字符串?
为返回true
。它解决了什么问题
假设您必须检查某个对象是否是字符串
:您将编写类似对象的内容?字符串
;但通过这种方式,您排除了不由String
继承但可以被视为字符串的类,如f.e.ActiveSupport::Multibyte::Chars
,它增强了一些字符串方法,并将每个缺少的方法委托给其@wrapped_String
实例变量
而不是object.is_a?String
您可以使用对象。行为类似于?(:String)
:这将很好地工作,因为ActiveSupport定义了(我的注释):
例如,ActiveRecord在内部使用它:
我相信它会检查对象的行为是否像字符串,这样您就可以执行类似于
str+“将此附加到字符串”的操作,如果str.act\u like\u string?
或者,如果您知道对象的行为像字符串,您可以调用任何其他字符串方法!看看这个-
# Provides acts_like? for every Ruby object, so you can call it
# on everything without worrying about its presence
class Object
def acts_like?(duck)
respond_to? :"acts_like_#{duck}?"
end
end
# Provides acts_like_string? for String and every class which inherits by String
class String
def acts_like_string?
# this actually could be false or nil or whatever,
# since acts_like? checks only the method presence
true
end
end
# Provides acts_like_string? ActiveSupport::Multibyte::Chars, delegating it
# to @wrapped_string instance variable (which in turn defines it if it is
# a String)
class ActiveSupport::Multibyte::Chars
delegate :<=>, :=~, :acts_like_string?, :to => :wrapped_string
end
gem 'activesupport'
require 'active_support/all'
def method_which_works_only_with_a_string_argument(argument)
unless argument.acts_like? :string
raise ArgumentError, 'argument must act like a string'
end
argument.capitalize
end
argument = ActiveSupport::Multibyte::Chars.new 'über'
method_which_works_only_with_a_string_argument argument
# => "Über"
method_which_works_only_with_a_string_argument 123
# => ArgumentError: argument must act like a string
[...]
def quote_bound_value(value, c = connection, column = nil) #:nodoc:
if column
c.quote(value, column)
elsif value.respond_to?(:map) && !value.acts_like?(:string)
if value.respond_to?(:empty?) && value.empty?
c.quote(nil)
else
value.map { |v| c.quote(v) }.join(',')
end
else
c.quote(value)
end
end
[...]