我to_s在一个方法中调用:
$ def my_function(num)
$ number = num.to_s.split(//)
$ puts number
$ end
$ my_function(233)
2
3
3
# => nil
Run Code Online (Sandbox Code Playgroud)
它看起来像在函数中,因为输出没有创建数组nil.为什么to_s.split(//)在方法内部调用时不会创建字符串数组?
另外,为什么输出puts number似乎只是每个数字在它自己的行?我是否需要在函数中显式创建数组,然后显式地将拆分数推入其中?
这似乎没有影响任何东西,它只是在我的终端烦恼 - 我经常收到以下警告(有时我收到多个,呼叫不同的线路,有时后面的路径/gem/变化,但除此之外,这是输出:
/Users/alecwilson/.rvm/gems/ruby-2.2.1/gems/fog-1.23.0/lib/fog/rackspace/mock_data.rb:42: warning: duplicated key at line 80 ignored: "name"
捆绑和运行时最常见rake test.有关如何解决它的任何想法?我一般都非常谨慎地编辑我的.rvm目录中的文件,因为我之前已经把它搞砸了,但有时仍然会警告我的PATH设置不正确(但只是偶尔).如果有人能告诉我导致这种情况的原因,我将非常感激.
我正在尝试在Rails应用程序中编写一个小功能,该应用程序使用随机词gem生成随机名词,然后将其复数.我第一次访问开发中的页面时能够让它工作,但是我想让脚本在每次加载页面时再次运行.现在,后续页面加载(直到我反弹服务器)给我FiberError in WelcomeController#randomwords,fiber called across threads.我试图自己解决这个问题,但我对编程很陌生,并不太了解Fibers的工作原理.我尝试使用Queue,但无法弄清楚如何让它工作,再次因为我不完全理解这个类.我该如何解决这个具体问题?
资料来源:welcome_helper.rb
def random
noun = RandomWord.nouns.next.split('_').sample.pluralize
if noun.include? "_"
noun = noun.split("_").join.pluralize
else
noun.pluralize!
end
return noun
end
Run Code Online (Sandbox Code Playgroud) 似乎无法为此找到合适的答案.我正在使用Rails教程的第10章第10.1.2节,似乎无法使邮件预览工作.我发现处理错误的所有答案都与本教程的不同部分有关,我假设我正在制作的错误正在盯着我.我已经完成了将教程中的代码复制/粘贴到相关文件中,到目前为止还没有看到我输入的内容和教程中的内容之间的区别.到目前为止,建议是user从函数定义中添加或删除参数,但这还没有解决问题.触发错误的url是http:// localhost:3000/rails/mailers/user_mailer/account_activation.http:// localhost:3000/rails/mailers/user_mailer /没有问题,http:// localhost:3000/rails/mailers/user_mailer/password_reset(我还没有进行任何自定义)也没有问题.我错过了什么?
这是错误:
NoMethodError in Rails::MailersController#preview
undefined method `activation_token=' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
摘录来源:
def account_activation
user = User.first
user.activation_token = User.new_token # highlighted line
UserMailer.account_activation(user)
end
Run Code Online (Sandbox Code Playgroud)
据我所知,这里涉及的文件是:
user_mailer.rb:
class UserMailer < ApplicationMailer
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.user_mailer.account_activation.subject
#
def account_activation(user)
@user = user
mail to: user.email, subject: "Account activation"
end
def password_reset
@greeting = "Hi"
mail …Run Code Online (Sandbox Code Playgroud)