jjzjj

after_initialize

全部标签

ruby - ruby 中的字符串文字如何绕过 new/initialize,有没有办法对此进行检测?

今天下午我在玩一个主意,偶然发现了一些我不太明白的东西。基本上我在这个实验中试图实现的是在每次创建字符串时以某种方式知道(供以后使用,例如在某种DSL中)。以下内容适用于通过String.new创建的任何字符串:class::Stringclass例如irb>String.new("foo")initializing'foo'newing'foo'=>"foo"我想不通的是当您使用文字时如何创建String对象。例如,为什么这不经过相同的初始化和设置:irb>"literalstring"=>"literalstring"我意识到当字符串是文字时,编译器会做一些不同的事情,但它不需要初

ruby - 在 object.initialize 中,使用 self 是不是更好。超过 @?

有一种约定,在可能的情况下,通过对象的实例变量来引用对象的属性。PracticalObject-OrientedDesigninRuby说:Alwayswrapinstancevariablesinaccessormethodsinsteadofdirectlyreferringtovariables...这显示了一个例子,我已经释义了:classGearattr_reader:chainring,:cog...defratio#thisisbad#@chainring/@cog.to_f#thisisgoodchainring/cog.to_fend我看到使用实例变量创建新对象的最常

ruby-on-rails - ruby rails : why do i get message for javascript and css after rails s?

railss=>StartedGET"/assets/application.css?body=1"for127.0.0.1at2011-10-1103:37:03-0900Servedasset/application.css-304NotModified(0ms)StartedGET"/assets/home.css?body=1"for127.0.0.1at2011-10-1103:37:03-0900Servedasset/home.css-304NotModified(0ms)StartedGET"/assets/jquery_ujs.js?body=1"for127.0.0

ruby - 为什么 'undefined method ` assert_equal' ' is thrown even after requiring ' test/unit'

我打开irb并输入:require'test/unit'但是当我使用assert_equal方法时,出现以下错误:NoMethodError:undefinedmethod'assert_equal'formain:Object。为什么即使在需要“测试/单元”之后也会发生这种情况? 最佳答案 assert_equal是在Test::Unit::TestCase的子类上定义的,因此仅在该类中可用。您可能会成功地使用includeTest::Unit::TestCase将这些方法加载到当前范围。更有可能的是,您最好将测试写在一个短文件中

ruby-on-rails - rails : Command not Found after successful install

所以我安装了ruby​​、gems和rails-但是每当我输入rails时,我都会收到rails:Commandnotfound.错误。我转储了我的本地gem,我将在下面包含***LOCALGEMS***actionmailer(3.2.7)actionpack(3.2.7)activemodel(3.2.7)activerecord(3.2.7)activeresource(3.2.7)activesupport(3.2.7)arel(3.0.2)bigdecimal(1.1.0)builder(3.0.0)bundler(1.1.5)daemon_controller(1.0.0)

ruby - RSpec 的 before、after 和 around 钩子(Hook)以什么顺序运行?

当我面对someissue我决定检查before和afterHook的执行顺序。这就是我所做的:require"spec_helper"describe"Theorder:"dobefore(:all){puts"before_all"}after(:all){puts"after_all"}before(:each){puts"before_each"}after(:each){puts"after_each"}describe"DESCA"dobefore{puts"A_before"}it"A_it_1"doexpect(1).toeq(1)endit"A_it_2"doexpe

ruby - 时间 :Class after requiring active_support/time_with_zone 的未定义方法 `zone`

我在使用Ruby2.2.1并安装了active_support4.2,所以我想使用Time.zone.parse('2007-02-1015:30:45')如此处所述:http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html但即使在需要active_support和active_support/time_with_zone之后,我觉得Time.zone仍然不起作用。观察:2.2.1:002>require"active_support"=>true2.2.1:003>Time.zoneNoMethodError

ruby-on-rails - Ruby:Class.new 在 Rails 控制台中给出 "Class not initialized"错误

我正在创建一个轻量级应用程序来创建和显示即将发生的事件的信息。我定义了一个Event类,它将args散列作为参数。初始化方法定义如下。classEvent到目前为止,还不错。然后,在Rails控制台中,我定义了一个args散列并尝试创建一个Event实例,但出现以下错误。[4]pry(main)>args={what:'what',theme:'theme'}=>{:what=>"what",:theme=>"theme"}[5]pry(main)>Event.new(args)=>#这看起来很简单,但我很难理解。任何帮助表示赞赏。 最佳答案

ruby-on-rails - rails : Why do I get "warning: already initialized constant JSON::VERSION" when running rake cucumber?

我刚刚设置了一个LinuxMintbox,用于使用rvm进行Rails开发。我继续生成了一个Rails5应用程序,设置了mysql连接,添加了cucumber-railsgem然后尝试运行:rakecucumber出于某种原因,我遇到了:/usr/bin/ruby2.3-Sbundleexeccucumber--profiledefault/usr/lib/ruby/vendor_ruby/json/version.rb:3:warning:alreadyinitializedconstantJSON::VERSION/var/lib/gems/2.3.0/gems/json-1.8.

ruby-on-rails - 全新安装 Rails 并出现 OpenSSL 错误 : "already initialized constant OpenSSL"

我一直在尝试使用RoR。我参加了RubyInstallfest,但遇到了我认为是openssl.bundle的问题。我正在使用RVM,并且正在运行Rails5.0.1和Ruby2.4.0我尝试使用rvmimplode进行完全删除/重新启动,并重新安装了RailsAppsGuide之后的所有内容但我仍然看到相同的错误。我正在运行最新版本的macOSSierra。这是我在my_app中运行$Rake-T时得到的输出。richsmith@Richs-MacBook-Pro:~/workspace/myapp$rake-T/Users/richsmith/.rvm/rubies/ruby-2.