【问题标题】:Equivalent python 'in' on rubyruby 上的等效 python 'in'
【发布时间】:2014-10-18 17:13:12
【问题描述】:

我是 ruby​​ 新手,我想尝试将代码 python 转换为 ruby​​。这将是一个简单的,类似的。

print 'it has' if 'ten' in 'tentacle' else 'None'

【问题讨论】:

    标签: ruby


    【解决方案1】:

    Ruby String 有方法 include? 所以它会是这样的:

    print 'tentacle'.include?('ten') ? 'it has' : 'None'
    

    【讨论】:

      【解决方案2】:

      如果你使用 rails,你可以使用 'in?':

      puts 'ten'.in?('tentacle') ? 'it has' : 'None'
      

      【讨论】:

        猜你喜欢
        • 2014-09-02
        • 2013-09-13
        • 2013-05-16
        • 1970-01-01
        • 2014-10-02
        • 2011-02-08
        • 1970-01-01
        • 1970-01-01
        • 2011-02-18
        相关资源
        最近更新 更多