以下の内容はhttps://tbpgr.hatenablog.com/entry/20141203/1417612930より取得しました。


Ruby | String | try_convert

概要

String.try_convert(obj) -> String | nil

詳細

obj を String に変換を試行。
変換には Object#to_str メソッドが使われる。

変換成功時: 変換後文字列
変換失敗時: nil

サンプルコード
require 'tbpgr_utils'

class Convertable
  def to_str
    "converted"
  end
end

bulk_puts_eval binding, <<-EOS
String.try_convert(1)
String.try_convert(Convertable.new)
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

String.try_convert(1)               # => nil
String.try_convert(Convertable.new) # => "converted"



以上の内容はhttps://tbpgr.hatenablog.com/entry/20141203/1417612930より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14