草庐IT

php - 在函数 PHP 中实例化类是不好的做法吗

全部标签

ruby - 如何在 Ruby 中获取类实例?

这个问题在这里已经有了答案:HowtofindeachinstanceofaclassinRuby(4个答案)关闭7年前。假设我有一个名为Post的类,它有许多已启动的实例(即Post.new(:name=>'foo'))。有没有办法通过调用某个类来检索该类的所有实例?我正在寻找类似Post.instances.all的东西

ruby-on-rails - Controller 所有 Action 的相同实例变量

我有一个RailsController,其中定义了两个操作:index和show。我在index操作中定义了一个实例变量。代码如下:defindex@some_instance_variable=fooenddefshow#somecodeend如何访问show.html.erb模板中的@some_instance_variable? 最佳答案 您可以使用前置过滤器为多个操作定义实例变量,例如:classFooController[:index,:show]defcommon_content@some_instance_variab

ruby - 如何让 ruby​​ 打印包含传递给函数的参数的完整回溯?

有时回溯足以诊断问题。但有时在不知道传递给函数的内容的情况下,崩溃的原因并不明显。获取传递给导致崩溃的函数的信息将非常有用,特别是在重现不明显的情况下,因为它是由例如网络连接异常、奇怪的用户输入或因为程序依赖于随机化或进程引起的来自外部传感器的数据。假设有以下程序defhandle_changed_input(changed_input)raise'ops'ifchanged_input=~/magic/enddefdo_something_with_user_input(input)input="#{input.strip}c"handle_changed_input(input)e

ruby - Ruby 使用什么哈希函数?

Ruby的哈希函数算法是什么? 最佳答案 标准的Ruby实现使用Murmurhash对于某些类型(整数、字符串)来自string.c:1901:/*MurmurHashdescribedinhttp://murmurhash.googlepages.com/*/staticunsignedinthash(constunsignedchar*data,intlen,unsignedinth)(注意这个函数在SVN主干里好像改名为st_hash)如果您想知道它在哪里使用,请在源代码中搜索rb_memhash。我以前在自己的项目中使用过M

ruby-on-rails - rails 4 - stripe_event 函数

我正在使用带有stripe和stripe_eventgem的Stripe支付服务。到目前为止一切顺利——它们工作得很好。我想使用stripe_eventwebhooks监听器来执行一系列操作。例如,当Stripe发送webhook建议申请新订阅时,我想将该订阅添加到subscriptions表,向新用户发送电子邮件,建议管理员等。在(非常少的)stripe_eventdocs在github上,他们说使用call方法订阅一个对象,并将示例显示为classCustomerCreateddefcall(event)#Eventhandlingendend但是它们没有显示此代码的位置(它将放置

ruby - 从 TestCase 调用 Sinatra 应用程序实例方法

我在Sinatra应用程序中有一个util方法,我想从我的TestCase进行测试.问题是我不知道如何调用它,如果我只使用app.util_method我有错误NameError:undefinedlocalvariableormethod'util_method'for#我的应用.rb:classMyAppmy_app_test.rb:require"my_app.rb"require"test/unit"require"rack/test"classMyAppTest 最佳答案 西纳特拉aliasesthenewmethodto

ruby - 改进仅适用于实例方法吗?

我试图了解Ruby的优化功能,但遇到了一个我不了解的场景。以这个示例代码为例:classTravellerdefwhat_are_youputs"I'maBackpacker"enddefself.preferred_accommodationputs"Hostels"endendmoduleRefinementsmoduleMoneydefwhat_are_youputs"I'macashed-uphedonist!"endmoduleClassMethodsdefpreferred_accommodationputs"ExpensiveHotels"endenddefself.in

ruby - 惯用的 Ruby - 执行一个函数直到它返回一个 nil,将它的值收集到一个列表中

我从这篇文章中窃取了我的标题:Executesafunctionuntilitreturnsanil,collectingitsvaluesintoalist这个问题涉及Lisp,坦率地说,我无法理解。然而,我认为他的问题——翻译成Ruby——正是我自己的问题:What'sthebestwaytocreateaconditionalloopin[Ruby]thatexecutesafunctionuntilitreturnsNILatwhichtimeitcollectsthereturnedvaluesintoalist?我目前笨拙的方法是这样的:deffooret=Array.ne

ruby-on-rails - var_dump 并像 php 一样死去,在 ruby​​ on rails 中(在 ruby​​ on rails 中调试)

这可能是重复的问题。但是我无法显示对象。我是ruby​​的新手,尝试过像var_dump和print_r这样的调试,然后在php中die/p>这是我的代码。@brand_id=Brand.maximum("brand_id")我试过下面的方法1putsYAML::dump(@brand_id)2logger.debug{@brand_id.inspect}请问谁能帮我解决一下吗? 最佳答案 Rails只会将View输出到浏览器。任何其他输出都发送到服务器上的STD_OUT。从View中调试很简单:但是从Controller或模型内部

ruby - 在 ruby​​ 中哪个更快 - 散列查找或带有 case 语句的函数?

在时间紧迫的脚本中,我们有几个地方可以将旧ID转换为字符串。目前,我们在函数内部使用case语句,如下所示:defget_nameidcaseidwhen1"onething"when3"otherthing"else"defaultthing"endend我正在考虑将其替换为哈希查找,如下所示:NAMES={1=>"onething",3=>"otherthing",}NAMES.default="defaultthing"感觉使用NAMES[id]应该比使用get_name(id)更快-但真的是这样吗? 最佳答案 首先,有几点。