小编mec*_*cov的帖子

将哈希转换为哈希数组

我有哈希,它的所有值都是数组,如下所示:

list = { letter:  ['a', 'b', 'c'],
         number:  ['one', 'two', 'three'],
         fruit:   ['apple', 'pear', 'kiwi'],
         car:     ['vw', 'mb', 'bmw'],
         state:   ['la', 'ny', 'fl'],
         color:   ['red', 'white', 'black'],
         tree:    ['oak', 'pine', 'maple'],
         animal:  ['cat', 'dog', 'rat'],
         clothes: ['tie', 'sock', 'glove'] }
Run Code Online (Sandbox Code Playgroud)

事实上,这个哈希可能有更多的键,值可能更大,但每个值的大小总是相同的(在这种情况下 - 三个).

我想将此哈希转换为哈希数组.

每个哈希都将具有所有原始哈希密钥和相应的值.

最后我想要:

list = [
  { letter: 'a', number: 'one', fruit: 'apple', car: 'vw', state: 'la',
    color: 'red', tree: 'oak', animal: 'cat', clothes: 'tie' },

  { letter: 'b', number: 'two', fruit: 'pear', car: 'mb', state: …
Run Code Online (Sandbox Code Playgroud)

ruby arrays hashmap

2
推荐指数
2
解决办法
501
查看次数

如何对一个值的重复次数求和并将其添加到 Ruby 中键的两个值中?

我试图为目录上的每种扩展类型创建一个具有一个键的哈希值。我想为每个键添加两个值:重复扩展名的次数以及具有该扩展名的所有文件的总大小。

\n

与此类似的东西:

\n

{".md" => {"ext_reps" => 6, "ext_size_sum" => 2350}, ".txt" => {"ext_reps" => 3, "ext_size_sum" => 1300}}

\n

但我\xc2\xb4m坚持这一步:

\n
hash = Hash.new{|hsh,key| hsh[key] = {}}\next_reps = 0\next_size_sum = 0\n\nDir.glob("/home/computer/Desktop/**/*.*").each do |file|\n  hash[File.extname(file)].store "ext_reps", ext_reps\n  hash[File.extname(file)].store "ext_size_sum", ext_size_sum \nend\n\np hash\n
Run Code Online (Sandbox Code Playgroud)\n

得到这个结果:

\n

{".md" => {"ext_reps" => 0, "ext_size_sum" => 0}, ".txt" => {"ext_reps" => 0, "ext_size_sum" => 0}}

\n

我找不到增加ext_reps和的方法ext_siz_sum

\n

谢谢

\n

ruby ruby-on-rails ruby-hash

2
推荐指数
1
解决办法
70
查看次数

如何用正则表达式拆分红宝石字符串?

嗨,我想将此字符串拆分为以下内容。

text = "In the last summer, I visited the U.S. with my friend. It was great experience. I loved an ice cream in the U.S. Welcome to U.S.A. pal!"
Run Code Online (Sandbox Code Playgroud)
In the last summer, I visited the U.S. with my friend.
It was great experience.
I loved an ice cream in the U.S.
Welcome to U.S.A. pal!
Run Code Online (Sandbox Code Playgroud)

显然,我不能申请text.split(".")text.split(". ")。因此,首要原则是,"."除缩写的单词外,字符串将被分割。但是,我不知道如何在Ruby中做到这一点。

似乎使用Regex可能有效,但我不知道如何执行此操作。您能分享您的想法吗?

ruby regex

1
推荐指数
1
解决办法
41
查看次数

Zeitwerk::NameError 由于 Rails 7 中的模块名称

我最近将应用程序从 Rails 6 升级到 7,由于 zeitwerk 命名约定,我面临一个问题。我有一个要自动加载的以下文件:

app/models/dw/hospital.rb

module DW
  class Hospital < DataWarehouse
    def self.columns
      super.reject{|column| column.name == 'tableau_user' }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我尝试通过在文件中添加以下行来自动加载此文件application.rb

config.autoload_once_paths << 'app/models/dw'
Run Code Online (Sandbox Code Playgroud)

但是当我启动服务器时,出现以下错误:

expected file app/models/dw/hospital.rb to define constant Hospital, but didn't (Zeitwerk::NameError)
Run Code Online (Sandbox Code Playgroud)

我不确定为什么会抛出这样的错误,因为该常量已经定义了。我怀疑这是因为我在课程之前定义了模块。如果有人如何解决这个问题,请告诉我。我已经被困在这个问题太久了。

ruby ruby-on-rails zeitwerk ruby-on-rails-7

1
推荐指数
1
解决办法
2628
查看次数

在 Deepin 15.10 上使用 rbenv 安装 Ruby

尝试使用 rbenv(或我尝试过 RVM 和 ruby​​-install 的任何其他安装程序)安装 Ruby 失败并出现此错误

$ rbenv install 2.6.3

> Downloading ruby-2.6.3.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.3.tar.bz2
Installing ruby-2.6.3...
ruby-build: use readline from homebrew

BUILD FAILED (Deepin 15.10 using ruby-build 20190423)

Inspect or clean up the working tree at /tmp/ruby-build.20190507002901.29361
Results logged to /tmp/ruby-build.20190507002901.29361.log

Last 10 log lines:
    from /tmp/ruby-build.20190507002901.29361/ruby-2.6.3/lib/rubygems/package.rb:44:in `<top (required)>'
    from /tmp/ruby-build.20190507002901.29361/ruby-2.6.3/lib/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /tmp/ruby-build.20190507002901.29361/ruby-2.6.3/lib/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /tmp/ruby-build.20190507002901.29361/ruby-2.6.3/lib/rubygems/installer.rb:11:in `<top (required)>'
    from /tmp/ruby-build.20190507002901.29361/ruby-2.6.3/lib/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /tmp/ruby-build.20190507002901.29361/ruby-2.6.3/lib/rubygems/core_ext/kernel_require.rb:54:in `require'
    from ./tool/rbinstall.rb:703:in `<module:RbInstall>'
    from ./tool/rbinstall.rb:639:in `<main>'
uncommon.mk:364: recipe for target 'do-install-all' failed …
Run Code Online (Sandbox Code Playgroud)

ruby rbenv

0
推荐指数
1
解决办法
563
查看次数

RSpec 的 contains_exactly 的哈希值是多少?

我需要验证哈希的内容,我惊讶地发现 RSpeccontain_exactly只适用于数组。理想的期望是:

expect(type.values.values).to contain_exactly(
  ONE: an_object_having_attributes(value: 'uno'),
  TWO: an_object_having_attributes(value: 'dos')
)
Run Code Online (Sandbox Code Playgroud)

基本要求是contain_exactly要求数组包含这些元素,并且哈希等效项必须仅包含指定的确切键/值对。

有很多解决方法都可以

  • include(key: value),但这允许其他键,我需要完全匹配。
  • expect(hash.keys).to contain_exactly(...)但这并不能验证键是否专门链接到值。
  • 无论如何使用contain_exactly(将哈希读取为元组[key, value])并基于子数组进行匹配,例如contain_exactly(a_collection_containing_exactly('ONE', an_object_having_attributes(value: 'uno')), ...)
  • 迭代哈希并根据预期输入将键与值进行匹配aggregate_failures

等等,但我最好奇是否有内置的 RSpec 方法可以做到这一点。

ruby testing rspec ruby-hash

0
推荐指数
1
解决办法
2505
查看次数

如何按字符串排序,Ruby

所以我有@cake.type,它可以是“Primo”、“Multilayered”、“Decorative”、“On Sale”、“Basic”`

我想按该顺序组织蛋糕列表。因此,所有“Primo”蛋糕都位于顶部,接下来是所有“多层”蛋糕,等等。每种类型的蛋糕都应按“created_at”进一步列出(最旧创建的蛋糕在前)。

@cakes = []
@cakes << @store.cakes.where(type: "Primo").order(created_at: :desc) 
@cakes << @store.cakes.where(type: "Multilayered").order(created_at: :desc) 
@cakes << @store.cakes.where(type: "Decorative").order(created_at: :desc) 
@cakes << @store.cakes.where(type: "On Sale").order(created_at: :desc) 
@cakes << @store.cakes.where(type: "Basic", "Basic").order(created_at: :desc) 
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以通过一个查询来完成它?

... 或许:

cake_eater_nom = []

c = @store.cakes
primo_cakes = c.where(type: "Primo").order(created_at: :desc)
cake_eater_nom << primo_cakes
multilayered_cakes = c.where(type: "Multilayered").order(created_at: :desc) 
cake_eater_nom << multilayered_cakes
decorative_cakes = c.where(type: "Decorative").order(created_at: :desc) 
cake_eater_nom << decorative_cakes
on_sale_cakes = c.where(type: "On Sale").order(created_at: :desc)
cake_eater_nom << on_sale_cakes
basic_cakes = c.where(type: "Basic").order(created_at: :desc) …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails sql-order-by rails-activerecord

0
推荐指数
1
解决办法
69
查看次数

哈希数组中的百分比?

我有hash一个哈希数组array

hash = {"BEGIN"=>5, "END"=>10}


array = [
    {:name=>"aaa", :phase=>"END", :quantity=>6},
    {:name=>"bbb", :phase=>"END", :quantity=>4},
    {:name=>"ccc", :phase=>"BEGIN", :quantity=>5}
  ]
Run Code Online (Sandbox Code Playgroud)

如何计算从哈希中获取值的哈希数组的百分比并获得此结果?

[
  {:name=>"aaa", :phase=>"END", :quantity=>60%},
  {:name=>"bbb", :phase=>"END", :quantity=>40%},
  {:name=>"ccc", :phase=>"BEGIN", :quantity=>100%}
]
Run Code Online (Sandbox Code Playgroud)

先感谢您!

ruby arrays percentage ruby-hash

-1
推荐指数
1
解决办法
95
查看次数