草庐IT

Java继承和对象转换

全部标签

ruby - 如何将字符串格式的毫秒数转换为 HH :MM:SS format in Ruby in under 3 lines of code?

@scores_raw.eachdo|score_raw|#belowiscodeiftimewasbeingsentinmillisecondshh=((score_raw.score.to_i)/100)/3600mm=(hh-hh.to_i)*60ss=(mm-mm.to_i)*60crumbs=[hh,mm,ss]sum=crumbs.first.to_i*3600+crumbs[1].to_i*60+crumbs.last.to_i@scoressum,:hms=>hh.round.to_s+":"+mm.round.to_s+":"+ss.round.to_s}@score

ruby - json 没有将 String 隐式转换为 Integer (TypeError)

玩转ruby​​,我已经:#!/usr/bin/ruby-w#WorldweatheronlineAPIurlformat:http://api.worldweatheronline.com/free/v1/weather.ashx?q={location}&format=json&num_of_days=1&date=today&key={api_key}require'net/http'require'json'@api_key='xxx'@location='city'@url="http://api.worldweatheronline.com/free/v1/weather.

对象的 Ruby 方法查找路径

是否有内置的Ruby方法或众所周知的库可以返回对象的整个方法查找链?Ruby查看一系列令人困惑的类(如thisquestion中所讨论)以查找与消息对应的实例方法,如果没有类响应消息,则调用接收方的method_missing。我将以下代码放在一起,但我确信它遗漏了某些情况或者它是否100%正确。请指出任何缺陷并指导我找到一些更好的代码(如果存在)。defmethod_lookup_chain(obj,result=[obj.singleton_class])ifobj.instance_of?Classreturnadd_modules(result)ifresult.last==B

ruby - 为什么 Object 在 Ruby 中既包含内核又继承它?

在Ruby(1.8.X)中为什么Object既继承了内核又包含了内核?仅仅继承还不够吗?irb(main):006:0>Object.ancestors=>[Object,Kernel]irb(main):005:0>Object.included_modules=>[Kernel]irb(main):011:0>Object.superclass=>nil请注意,在Ruby1.9中情况类似(但更简洁):irb(main):001:0>Object.ancestors=>[Object,Kernel,BasicObject]irb(main):002:0>Object.included

ruby-on-rails - ruby rails 将参数转换为 int 数组

我正在通过get发送数据,我需要将其放入一个int数组中以便在查找中使用。这是我的代码:@found=Array.newparams['candidate'].eachdo|c|@found我的网址是这样的http://localhost:3000/export/candidate?candidate[]=3&candidate[]=4&commit=Export如果它有任何不同,我将它用于此查找@candidate=Candidate.find(:all,:conditions=>["candidates.idIN?",@found])但目前它并没有把它放在一个真正的数组中,因为我得

ruby - 如何将字符串转换为方法调用?

这个问题在这里已经有了答案:Howtocallmethodsdynamicallybasedontheirname?[duplicate](5个答案)关闭8年前。如何使用字符串作为方法调用?"SomeWord".class#=>Stringa="class""SomeWorld".a#=>undefinedmethod'a'"SomeWorld"."#{a}"#=>syntaxerror,unexpectedtSTRING_BEG

ruby-on-rails - ruby 真的是一种完全面向对象的语言吗?

Ruby是完全面向对象的语言。在ruby​​中,一切都是对象,因此属于某个类。例如5属于Objectclass1.9.3p194:001>5.class=>Fixnum1.9.3p194:002>5.class.superclass=>Integer1.9.3p194:003>5.class.superclass.superclass=>Numeric1.9.3p194:005>5.class.superclass.superclass.superclass=>Object1.9.3p194:006>5.class.superclass.superclass.superclass.su

ruby-on-rails - 如何以递归方式将 YAML 文件扁平化为 JSON 对象,其中键是点分隔的字符串?

例如,如果我有YAML文件en:questions:new:'NewQuestion'other:recent:'Recent'old:'Old'这最终会变成一个json对象,例如{'questions.new':'NewQuestion','questions.other.recent':'Recent','questions.other.old':'Old'} 最佳答案 由于问题是关于在Rails应用程序上使用YAML文件进行i18n,因此值得注意i18ngem提供了一个辅助模块I18n::Backend::Flatten完全像

ruby-on-rails - 从 ruby​​ 中的对象中删除不需要的属性

我有一个对象,它具有来自调用的数据库的所有属性让我们说:u=User.find_by_email("email@email.com")你有first_name,last_name,email,phone等如何从对象本身获取除first_name和last_name之外的所有属性,而不是通过修改对模型的调用? 最佳答案 u.attributes.except("first_name","last_name") 关于ruby-on-rails-从ruby​​中的对象中删除不需要的属性,我们在

Ruby 获取继承类

我正在为Rails创建我的第一个插件。我对ruby​​还是很陌生,我想知道是否有可能获得继承类?例如,我正在尝试创建一个插件,在您不使用迁移时允许进行单元测试和功能测试。我要做的是初始化一个名为controller的类变量,以初始化为正在测试的Controller类型。如果我有一个基类ControllerTest:classControllerTest所以我目前坚持的是获取继承类的名称。这可能吗?如果没有,有没有人知道我可以如何着手实现它的另一种方式?提前致谢。 最佳答案 非常简单:使用“继承”回调。来自Class类的RDoc:in