草庐IT

Hive 自定义函数

全部标签

ruby - 为什么在 Ruby 中存在两种访问模块函数的方式?

moduleAdefself.funcputs"func"endend>>A.funcfunc>>A::funcfunc为什么.和::都存在?为什么不仅是.? 最佳答案 作用域解析运算符(::)可以解析常量、实例方法和类方法,因此只要我们在正确的位置查找,我们就可以将该运算符用于基本上任何方法。此外,由于方法“func”被定义为模块A的类方法(通过self.func,类似于“静态”方法)它直接属于模块(即本身是一个对象)所以它可以用点运算符调用,模块作为接收者。请注意,模块A的实例对“func”没有任何可见性,因为它是一个类方法:a

ruby-on-rails - ActiveRecord 自定义类型命名方案

我正在处理一个表,该表已有一个包含自然类型名称的列。例如。已经存在一个名为“provider”的列,其值为“foo”或“bar”。我想使用existing类型名称在此表上使用STI,因为必须添加一个名为“type”的附加列以供ActiveRecord使用似乎很愚蠢。问题是,这些类型名称与ruby​​类不完全匹配。我希望能够设置自定义映射,例如Class1=>foo,Class2=>bar。我尝试了以下方法:#Inthebaseclassset_inheritance_column:provider#InClass1defself.sti_name'foo'end#InClass2def

ruby - Rails 3 db :migrate 的未定义方法 `visitor'

我在Rails3中进行数据库迁移时遇到异常。undefinedmethod`visitor'for#编辑请查看解决方案here.在我的项目中没有出现字符串visitor所以我很困惑。这是完整的转储:$rakedb:migrate--trace**Invokedb:migrate(first_time)**Invokeenvironment(first_time)**Executeenvironment**Invokedb:load_config(first_time)**Invokerails_env(first_time)**Executerails_env**Executedb:l

c - Ruby 的最大函数顺序如何重复?

我一直在看maxmethod在Ruby的Enumerable混合(v2.4.1)。这是一个相当简单的方法,但是当存在重复项时它如何排序项目有点令人困惑。例如:x=[1,2,3,4,5,6,7,8,9]x.max{|a,b|a%2b%2}=>110.times{|y|px.max(y){|a,b|a%2b%2}}[][1][1,7]#whyis7thenextelementafter1?[3,1,5]#whynomore7?[7,3,1,5]#7isnowfirst[9,7,3,1,5][9,7,3,1,5,6][9,7,3,1,5,4,6][9,7,3,1,5,2,4,6][9,7,5

ruby - Chrome 的自定义配置文件

环境:MacOSX10.8.3、Ruby2.0.0p0、selenium-webdriver2.32.1、ChromeDriver26.0.1383.0。我想更改默认浏览器语言。我正在测试站点是否正确检测浏览器语言并以该语言显示页面。我能够将Firefox语言设置为德语:require"selenium-webdriver"profile=Selenium::WebDriver::Firefox::Profile.newprofile["intl.accept_languages"]="de"caps=Selenium::WebDriver::Remote::Capabilities.

ruby - 基于 Ruby 语法的未定义局部变量

在下面的Ruby代码中,#!/usr/bin/envrubyx=truey=xandz=yputs"z:#{z}"它将按预期输出z:true。但在下一个中,我希望它具有相同的行为:#!/usr/bin/envrubyx=truez=yify=xputs"z:#{z}"结果undefinedlocalvariableormethod'y'formain:Object(NameError)这是为什么呢?我知道我正在做一个赋值,并隐式检查赋值以确定是否运行z=y。我还了解到,如果我在x=5行之后添加y声明y=nil,它将按预期通过并运行。但是,期望语言应该首先评估if部分,然后评估其内容,然

ruby - NoMethodError(未定义方法 `[]' 为 nil :NilClass)

我有一个非常奇怪的错误实例:NoMethodError(undefinedmethod`[]'fornil:NilClass):app/controllers/main_controller.rb:150:in`blockinfind_data_label'app/controllers/main_controller.rb:149:in`each'app/controllers/main_controller.rb:149:in`find_data_label'app/controllers/main_controller.rb:125:in`data_string'app/cont

ruby-on-rails - rails 3.2.8 中未定义的 read_inheritable_attribute

升级到rails3.2.8后出现以下错误信息NoMethodError(undefinedmethod`read_inheritable_attribute'forAdminController:Class):谁能解释一下如何解决这个错误?整个轨迹NoMethodError(undefinedmethod`read_inheritable_attribute'forAdminController:Class):vendor/plugins/ssl_requirement/lib/ssl_requirement.rb:45:in`ssl_allowed?'vendor/plugins/s

ruby - 将构造函数参数转换为实例变量

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Idiomaticobjectcreationinruby很多时候我有一个initialize方法,看起来像这样:classFoodefinitializebar,buz,...@bar,@buz,...=bar,buz,...endend有没有办法用一个简单的命令来做到这一点,比如:classFooattr_constructor:bar,:buz,...end其中的符号代表实例变量的名称(具有attr_accessor、attr_reader、attr_writer的精神/风格)?我想知道是否有内置的方式

ruby-on-rails - Nokogiri 和 XPath 中未定义的命名空间前缀

我正在尝试解析YoutubeGdata以查看是否存在具有给定ID的视频。但是没有普通的标签,而是带有命名空间。在链接上http://gdata.youtube.com/feeds/api/videos?q=KgfdlZuVz7I有标签:1有命名空间openSearch:xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'但我不知道如何在Nokogiri和Ruby中处理它。部分代码如下:xmlfeed=Nokogiri::HTML(open("http://gdata.youtube.com/feeds/api/videos