草庐IT

first_it

全部标签

ruby - 如何: Ruby Range that doesn't include the first value

使用Ruby中的范围,您可以执行0..5来包含0和5之间的所有数字(包括0和5)。您还可以执行0...5来包含不包括除5以外的相同数字。(1..5)===5=>true(1...5)===5=>false(1...5)===4.999999=>true有没有办法排除第一个数字而不是最后一个数字以获得这样的结果?(1...5)===1=>false(1...5)===1.00000001=>true 最佳答案 不,没有对此类范围的内置支持。如果此行为是必要的,您可能希望推出自己的Range类。

ruby - YAML/ ruby : Get the first item whose <field> is <value>?

我有这个YAML:-company:-id:toyota-fullname:トヨタ自動車株式会社-company:-id:konami-fullname:KonamiCorporation而且我想获取ID为konami的公司的全名。使用Ruby1.9.2,最简单/常用的获取方式是什么?注意:在我的其余代码中,我一直在使用require"yaml",所以我更愿意使用相同的库。 最佳答案 这也行,但不使用迭代:y=YAML.load_file('japanese_companies.yml')result=y.select{|x|x['

ruby : if i declare a variable in a method does another method in the same class know it exists?

如果我有一个名为roll的方法(如在骰子中)并且它有一个名为number的变量。同一个类中的另一个名为stats的方法可以使用其中的那个变量吗?? 最佳答案 你是说这样?classDiedefroll@number=5enddefstatsputs@numberendendd=Die.newd.rolld.stats#prints5 关于ruby:ifideclareavariableinamethoddoesanothermethodinthesameclassknowitexists

ruby-on-rails - 在规范中使用 describe/it 优于 feature/scenario 的优势? (除了语法糖)

Ruby1.9.3、Rails3.1.10、RSpec2.13.0、Capybara2.2.1我正在为Rails3应用程序编写测试——一个供客户(和管理员)配置各种电话设置的GUI。我已经编写了6个左右的规范文件,之前还编写了很多其他文件(我将其用作模板)。以下是规范文件的快照。#spec/features/admin/administrators_spec.rbrequire'spec_helper'includeAdministratorHelperincludeHelpersfeature"ExerciseAdministratorspage"include_context"sh

ruby-on-rails - rails : get the first n active record objects of a model

我需要一种方法来获取模型的前n项。Item.first(n)、Item.all[1..n]会这样做,只是它们返回的是数组,而不是对象。如何将其作为ActiveRecord对象获取?irb(main):135:0>Player.where(game_id:1).class=>Player::ActiveRecord_Relation#Okirb(main):136:0>Game.first.players.class=>Player::ActiveRecord_Associations_CollectionProxy#Okirb(main):137:0>Player.where(game

ruby 可枚举 : first truthy value of a block

在ruby​​中我们可以这样做:stuff_in_trash.detect(&:eatable?)=>:pack_of_peanutsstuff_in_trash.detect(&:drinkable?)=>nil但是,如果我们对block第一次为真时的值感兴趣,而不是block为其取真值的第一个项目感兴趣怎么办?也就是转换如下代码:deftry_to_make_artwork_from(enumerable)enumerable.eachdo|item|result=make_artwork_fromitemreturnresultifresultendnilend类似于:deftr

ruby-on-rails - rails : How can my app tell if it is running in MRI or JRuby?

在previousquestion中,我询问了如何告诉我的Gemfile是采用与JRuby相关的gem还是与MRI相关的gem。我得到的答案是在Gemfile中执行以下操作:platforms:jrubydogem"activerecord-jdbcsqlite3-adapter"endplatforms:mridogem"sqlite3"end显然,Bundler中的platforms()方法知道如何判断我运行的是MRI还是JRuby。如果我正在运行JRuby或MRI,是否有其他方法可以在我的程序中判断? 最佳答案 你能像这样区分

Ruby 枚举 : Taken first n where block returns true

我想取前“n”个通过该block的条目a=1..100_000_000#Basicallyalongarray#Thisiteratesoverthewholearray--nogoodb=a.select{|x|x.expensive_operation?}.take(n)一旦我得到n个“昂贵”条件为真的条目,我想缩短迭代。你有什么建议?take_while并保持计数n?#Thisisthecodeihave;whichithinkcanbewrittenbetter,buthow?a=1..100_000_000#Basicallyalongarrayn=20i=0b=a.take

ruby-on-rails - 使用 rake db :migrate does not change it 迁移数据

我正在为rails和db:migrate苦苦挣扎。我有一个使用此代码的迁移classSetDefaultInstallmentsForLicenses运行迁移后有这个输出==SetDefaultInstallmentsForLicenses:migrating==============================Modifyinglicense3withcodeLEADERAftersavetrue5Aftersavef==SetDefaultInstallmentsForLicenses:migrated(0.0037s)=====================可以清楚的看到

ruby - Textmate + Ruby : ruby: warning: -K is specified; it is for 1. 8 兼容性,可能导致奇怪的行为

我正在使用textmate,当我尝试使用快捷方式复制一行时(ctrl+shift+d)它给我一个错误ruby:warning:-Kisspecified;它是为了1.8兼容性,可能会导致奇怪的行为。这很奇怪,因为它几天前才开始这样做。正如另一个stackoverflow答案所建议的那样,我将我的textmate指向1.8,这工作了几个月,但突然之间它不再工作了。我该如何解决这个问题? 最佳答案 TextMate1.xunderMavericks:IfyouwishtouseTextMate1.xonMavericksyouwilln