gpt4 book ai didi

ruby - 如何使用acts_like_string?在 ruby

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:19 24 4
gpt4 key购买 nike

http://api.rubyonrails.org/classes/String.html#method-i-acts_like_string-3F

我正在研究 ruby​​ 中的 acts_like_string?,但找不到任何有关如何使用的示例。我试过了,但没有成功。

puts str.acts_like_string?

最佳答案

这些是在我的 gems 文件夹中搜索 acts_like_string 的结果,grep --recursive --context 2 acts_like_string $(gem environment gemdir):

activesupport-4.1.0.rc1/lib/active_support/core_ext/string/behavior.rb-class String
activesupport-4.1.0.rc1/lib/active_support/core_ext/string/behavior.rb- # Enable more predictable duck-typing on String-like classes. See <tt>Object#acts_like?</tt>.
activesupport-4.1.0.rc1/lib/active_support/core_ext/string/behavior.rb: def acts_like_string?
activesupport-4.1.0.rc1/lib/active_support/core_ext/string/behavior.rb- true
activesupport-4.1.0.rc1/lib/active_support/core_ext/string/behavior.rb- end
--
activesupport-4.1.0.rc1/lib/active_support/multibyte/chars.rb- alias to_str wrapped_string
activesupport-4.1.0.rc1/lib/active_support/multibyte/chars.rb-
activesupport-4.1.0.rc1/lib/active_support/multibyte/chars.rb: delegate :<=>, :=~, :acts_like_string?, :to => :wrapped_string
activesupport-4.1.0.rc1/lib/active_support/multibyte/chars.rb-
activesupport-4.1.0.rc1/lib/active_support/multibyte/chars.rb- # Creates a new Chars instance by wrapping _string_.
--
mail-2.5.4/lib/mail/multibyte/chars.rb-
mail-2.5.4/lib/mail/multibyte/chars.rb- # Enable more predictable duck-typing on String-like classes. See Object#acts_like?.
mail-2.5.4/lib/mail/multibyte/chars.rb: def acts_like_string?
mail-2.5.4/lib/mail/multibyte/chars.rb- true
mail-2.5.4/lib/mail/multibyte/chars.rb- end

如您所见,acts_like_string?ActiveSupport::Multibyte::Chars 返回 true .它解决了哪个问题?

假设您必须检查某个对象是否是 String:您会编写类似 object.is_a 的代码吗?字符串;但通过这种方式,您可以排除那些不由 String 继承但可以作为字符串计算的类,例如 f.e. ActiveSupport::Multibyte::Chars,它增强了一些字符串方法并将每个缺失的方法委托(delegate)给它的 @wrapped_string 实例变量。

而不是 object.is_a? String 您可以使用 object.acts_like?(:string):这可以正常工作,因为 ActiveSupport 定义了(我的评论):

# 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

例如,ActiveRecord 在内部使用它 values sanitization :

[...]
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
[...]

关于ruby - 如何使用acts_like_string?在 ruby ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22706436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com