【问题标题】:ruby excercise for name.lenth.t_sname.length.to_s 的红宝石练习
【发布时间】:2013-08-30 12:10:46
【问题描述】:

书中的练习说我应该单独询问名称,然后获取名称长度。我被困在第8行?这就是我应该做的事情来获得这个名字吗?

    puts "What\'s your first name?"
    first = gets.chomp
    puts "What\'s your middle name?"
    middle = gets.chomp
    puts "What\'s your last name?"
    last = gets.chomp
    puts "So, your full name is #{first} #{middle} #{last}." 
    name = # {first} #{middle} #{last}
    puts "Did you know that are + #{name.length.t_s} + characters "
    puts "in your + name + "

非常感谢您的帮助!

【问题讨论】:

    标签: ruby variable-length


    【解决方案1】:

    应该是(使用 ruby​​ 字符串嵌入):

    name = "#{first} #{middle} #{last}"
    

    http://www.ruby-doc.org/core-2.0/String.html

    【讨论】:

    • 是的,但我仍然得到这个结果:你知道你的 + name + 中有 + name.length.t_s + 字符吗?
    • 啊,明白了。给你:puts "Did you know that are + #{name.length.to_s} + characters in your #{name}"你的输出中有两个错别字。
    • 它不起作用。它说:<main>': undefined method t_s' for 18:Fixnum (NoMethodError)
    • 你又错过了o。该方法称为to_s,用于“TO STRING”。
    • 非常感谢!我真的很感激!并为我的粗心疏忽感到抱歉!
    猜你喜欢
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多