草庐IT

what-came-before-websockets

全部标签

ruby - 在 Sinatra 中,你如何制作一个 "before"过滤器来匹配除某些路由之外的所有路由

我有一个RubySinatra应用程序,我有一些代码需要在除少数异常(exception)情况外的所有路由上执行。我该怎么做?如果我想在选定的路由(白名单样式)上执行代码,我会这样做:['/join',"/join/*","/payment/*"].eachdo|path|beforepathdo#somecodeendend我该如何反其道而行之(黑名单样式)?我想匹配除'/join'、'/join/*'和'/payment/*'之外的所有路由 最佳答案 负面前瞻:before/^(?!\/(join|payment))/do#..

ruby-on-rails - 有没有办法使 before_save 成为条件?

我正在尝试在Rails应用程序中进行有条件的before_save,但它似乎不起作用。before_savemethod_call_to_runifself.related_model.some_method_that_returns_t_or_f?如果“some_method_that_returns_t_or_f”返回true,我希望它在保存对象之前运行该方法,否则我只希望它忽略before_save。 最佳答案 你可以使用:ifbefore_savedo_something,:if=>Proc.new{|model|model

ruby - Rspec : expect vs expect with block - what's the difference?

刚刚学习rspec语法,我注意到这段代码有效:context"givenabadlistofplayers"dolet(:bad_players){{}}it"failstocreategivenabadplayerlist"doexpect{Team.new("Random",bad_players)}.toraise_errorendend但是这段代码没有:context"givenabadlistofplayers"dolet(:bad_players){{}}it"failstocreategivenabadplayerlist"doexpect(Team.new("Rando

ruby-on-rails - Rails before_filter 用于 Controller 中的特定操作

defnewbefore_filterdoredirect_to"/"unlesscurrent_admin||current_companyflash[:notice]='Youdonthaveenoughpermissionstobehere'unlesscurrent_admin||current_companyendCODECODECODEenddefeditbefore_filterdoredirect_to"/"unlesscurrent_admin.id=5flash[:notice]='Youdonthaveenoughpermissionstobehere'unles

ruby - Sinatra 与 EventMachine WebSockets 一起使用是否成功?

我已经使用Sinatra一段时间了,我想通过websockets推送数据来为我的网络应用程序添加一些实时功能。我已经成功地单独使用了gem“em-websocket”,但无法编写一个具有sinatra网络服务器和网络套接字服务器的ruby​​文件。我试过旋转运行!或开始!方法在单独的线程中关闭但没有成功。有人用过这个吗?我想将它们放在同一个文件中,这样我就可以在两个服务器之间共享变量。 最佳答案 没试过,但应该不会太难:require'em-websocket'require'sinatra/base'require'thin'EM

ruby-on-rails - rails : I can't call a function in a module in/lib - what am I doing wrong?

我有一个模块保存在/lib中作为test_functions.rb看起来像这样moduleTestFunctionsdefabcputs123endend进入ruby​​脚本/运行程序,我可以看到该模块正在自动加载(良好的配置约定等等......)>>TestFunctions.instance_methods=>["abc"]所以方法是已知的,让我们尝试调用它>>TestFunctions.abcNoMethodError:undefinedmethod`abc'forTestFunctions:Modulefrom(irb):3没有。这个怎么样?>>TestFunctions::a

ruby - class << self vs self.method with Ruby : what's better?

这RubyStyleGuide告诉我们最好使用self.method_name而不是classmethod_name。但是为什么?classTestClass#badclass是否存在性能问题? 最佳答案 class善于将所有类方法放在同一个block中。如果在defself.method中添加方法form则不能保证(除了惯例和一厢情愿的想法)不会有额外的类方法隐藏在文件的后面。defself.method擅长显式声明方法是类方法,而使用class你必须自己去找容器。这些中哪一个对您来说更重要是一个主观决定,并且还取决于诸如有多少其

ruby-on-rails - 是否可以在模块中定义 'before_save' 回调?

是否可以在模块中定义before_save回调?这样的类是这样的:classModelincludeMongoMapper::DocumentincludeMyModuleend和这样的模块:moduleMyModulebefore_save:do_somethingdefdo_something#dowhateverendenddo_something会在保存任何Model对象之前调用吗?我试过这样但是得到了undefinedmethod'before_save'forMyModule:Module。抱歉,如果事情很简单-我是Ruby和Rails的新手。

Ruby 类继承 : What is `<<` (double less than)?

class这是什么为了?我搜索了,但结果只告诉我有关字符串连接的信息... 最佳答案 虽然class是真的是单例类的语法,正如其他人所说,它最常用于在类定义中定义类方法。但是这两种用法是一致的。方法如下。Ruby允许您通过以下方式向任何特定实例添加方法:class这添加了一个方法foo对某个实例,不是对它的类,而是对那个特定实例。(实际上,foo被添加到实例的“单例类”,但这或多或少是一个实现怪癖。)上面的代码执行后,您可以将方法foo发送到某个实例:someinstance.foo=>"Hello."但是您不能将foo发送到同一类

ruby - Rails 4 before_action,将参数传递给调用的方法

我有以下代码:classSupportsController是否可以将字符串传递给方法set_support以应用于所有4种View方法?是否可以为View中的每个方法将不同的字符串传递给方法set_support? 最佳答案 before_actiononly:[:show,:edit,:update,:destroy]doset_support("value")end 关于ruby-Rails4before_action,将参数传递给调用的方法,我们在StackOverflow上找到