草庐IT

first_count

全部标签

ruby - 什么是哈希#count?

当我注意到这个时,我正在玩IRB(Ruby2.5.1):irb(main):020:0>h=>{3=>4,:aaa=>false}irb(main):021:0>h.count=>2然而,这个方法在Rubydocs中不存在。.快速测试表明hsh.count给出与hsh.keys.count相同的结果,并且Hash.ancestors包含可枚举。Hash#count到底是什么? 最佳答案 您似乎已经了解了大部分内容...它是Enumerable#count.从技术上讲,hsh.keys.count正在计算键数,hsh.count正在计

ruby 鞋子 : counting the amount of times a value occurs in an Array

我正在使用Shoes在Ruby中制作Yahtzee游戏当我点击按钮“Two”时,代码应该计算值2出现在数组中。对于出现的值2的每个实例,分数增加2。此代码适用于特定数量的案例,但适用于其他情况,例如@array=[2,1,2,2,3]#数组中有三个2所以分数应该是6,但我的代码却返回4...为什么?button"twos"do@array.each_with_indexdo|value,index|if(@array[index]==2)@score=@score+2@points=@score+2end#ifend#loopend#button 最佳答案

ruby 哈希 : return the first key value under which is not nil

假设我有一个散列hash={a:1,b:false,c:nil}&某处的一系列键:[:c,:b,:a]。在!=nil下返回这样的键值是否有Ruby惯用语?目标[:c,:b,:a].select{|key|hash[key]!=nil}.first#returns:b似乎太长了。 最佳答案 我认为Enumerable#find可能有效:find(ifnone=nil){|obj|block}→objornilfind(ifnone=nil)→an_enumeratorPasseseachentryinenumtoblock.Retur

ruby - 如何区分由 first_or_create 搜索或创建的

我想区分由first_or_create搜索或创建。record=MasterRecord.where(:name=>'test_data').firest_or_create#andiwantdifferentiatesearchedorcreatedlikethis.#butthereisnocreated_record?methodifrecord.created_record?render:status=>200,:json=>record.to_jsonelserender:status=>409,:json=>record.to_jsonend我该怎么做?

sql - Rails 4 "where"-query with "has_many belongs_to"-relationship, search with specific count

这应该是一个简单的查询,但我在正确使用Rails语法时遇到了问题。我正在使用Rails4.1.1和Postgresql(9.3)。我有一个模型用户和模型公司。User有一个公司,Company有很多用户。我试图找到所有拥有5个以上用户的公司。classCompany问题类似于:Findallrecordswhichhaveacountofanassociationgreaterthanzero如果我尝试上述类似的解决方案:Company.joins(:users).group("company.id").having("count(users.id)>5")它给我一个错误:PG::Un

ruby - 有人能帮助我理解 Ruby 手册中的 str.counts 示例吗?

在我看到的所有其他示例中,str.count都非常简单。它只计算字符串中参数的实例。但是Ruby手册中给出的方法的例子似乎难以理解(见下文)。它甚至不使用括号!谁能帮我解释一下?a="helloworld"a.count"lo"»5a.count"lo","o"»2a.count"hello","^l"»4a.count"ej-m"»4 最佳答案 它正在计算occurences的数量你作为参数传入的字母的数量a.count("lo")#5,counts[l,o]helloworld*****#countsall[h,e,o],but

ruby-on-rails - Rails、Count 和 Group

我有一个像这样的表:+--------+-----------+-------+-----------+|house_no|house_alpha|flat_no|street_name|+--------+-----------+-------+-----------+|1|||JamesSt||1|||JamesSt||1|||JamesSt||2|A||JamesSt||2|B||JamesSt||3|A||JamesSt||4||416|JamesSt||4||416|JamesSt|+--------+-----------+-------+-----------+我正在尝试

ruby-on-rails - 在 ActiveRecord 中使用 first_or_create 的两种不同方式的区别

我是ActiveRecord的新手,所以这可能是一个愚蠢的问题:我的方法是这样写的:sample_organizations=['37Signals','FogCreek']sample_organizations.each{|item|Organization.first_or_create(name:item,time_zone:'Central')}它没有用,我希望它能遍历我的组织数组并将它们全部插入到数据库中,但它只插入了第一行。然后有人建议像这样更改它并且这个方法有效,但我想知道是否有人可以解释第一种方法中的错误,以便我可以了解我在做什么/假设错误。所以这个有效:sample

ruby - 鲁弗斯调度程序 : run every x seconds with first run immediately

我正在使用rufus调度程序让一些任务每隔一段时间执行一次。我希望任务在脚本启动时或多或少地立即运行,然后以给定的时间间隔运行。这似乎不受API支持,还是我遗漏了什么?我已将0.1秒指定为第一次运行之前的延迟,如下所示scheduler=Rufus::Scheduler.newscheduler.every'10s',:first_in=>0.1do#dosomeworkend如果:first_in属性设置为0,调度程序会在第一次运行之前等待整整10秒。如果值设置得太低(我想在执行任务时评估过去的值),或者如果我使用Time.now,则会引发以下错误:~/.ruby/gems/rufu

ruby - Array#first 的 block 参数有什么作用?

我的内存力最差,我忘记了Array#find。所以我写了一些代码,使用Array#first来获取第一个符合条件的项目:first_even_num=[1,2,3].first{|x|x%2==0}这行不通,无论条件如何,它都只返回数组的第一个元素。该block根本没有被调用:[1,2,3].first{|x|exit}puts"theprogramdidnotexit"我已经接受了Array#first没有按照想象的那样做,但我很好奇为什么上述示例不会引发错误。查看2.3.1source,唯一记录的参数是一个整数,表示返回结果的数量。我不太了解C,但这是底层Array#first方法