我有一个哈希数组,我需要根据哈希值之间的一个匹配值来查找和存储匹配项.
a = [{:id => 1, :name => "Jim", :email => "jim@jim.jim"},
{:id => 2, :name => "Paul", :email => "paul@paul.paul"},
{:id => 3, :name => "Tom", :email => "tom@tom.tom"},
{:id => 1, :name => "Jim", :email => "jim@jim.jim"},
{:id => 5, :name => "Tom", :email => "tom@tom.tom"},
{:id => 6, :name => "Jim", :email => "jim@jim.jim"}]
Run Code Online (Sandbox Code Playgroud)
所以我想回来
b = [{:id => 1, :name => "Jim", :email => "jim@jim.jim"},
{:id => 3, :name => "Tom", :email => "tom@tom.tom"},
{:id => …Run Code Online (Sandbox Code Playgroud) ruby 中是否有某种方法可以编辑 Markdown 文件顶部的 YAML Frontmatter,例如 Jekyll 和 Middleman 中使用的那些?
就像是:
def update_yaml
#magic that changes A: 1 to A: 2 in Frontmatter block
end
Run Code Online (Sandbox Code Playgroud)
然后我的降价文件将从
---
A: 1
---
# Title
Words. More words. This is the words part of the file.
Run Code Online (Sandbox Code Playgroud)
到
---
A: 2
---
# Title
Words. More words. This is the words part of the file.
Run Code Online (Sandbox Code Playgroud)
似乎唯一的选择是解析整个文件,然后重写整个文件,只更改所需的部分,但我希望有更好的方法。
我真的尝试过研究这个问题,但我现在已经接近它了,我担心如果不寻求帮助我就找不到解决方案.我正在浏览RubyMonk,其中一个练习让我完全陷入困境.
class Hero
def initialize(*names)
@names = names
end
def full_name
# a hero class allows us to easily combine an arbitrary number of names
end
end
def names
heroes = [Hero.new("Christopher", "Alexander"),
Hero.new("John", "McCarthy"),
Hero.new("Emperor", "Joshua", "Abraham", "Norton")]
# map over heroes using your new powers!
end
Run Code Online (Sandbox Code Playgroud)
您可以在评论中看到代码要求的内容; 获取英雄变量中的名称并将它们组合成一个名称.我已经尝试过测试一些put并且除了"#"或"nil"之外我无法在STDOUT中获得任何内容,所以很明显我没有正确使用它.
目标的要求说不要使用.map或.collect,但我认为你应该这样做,因为如果你不使用.map或.collect它就不符合要求.
想法?
我有一个哈希数组(实际上是CSV行),我需要查找并保留与两个特定键(用户,部分)匹配的所有行.以下是数据示例:
[
{ user: 1, role: "staff", section: 123 },
{ user: 2, role: "staff", section: 456 },
{ user: 3, role: "staff", section: 123 },
{ user: 1, role: "exec", section: 123 },
{ user: 2, role: "exec", section: 456 },
{ user: 3, role: "staff", section: 789 }
]
Run Code Online (Sandbox Code Playgroud)
所以我需要返回的是一个数组,其中只包含相同用户/节组合出现多次的行,如下所示:
[
{ user: 1, role: "staff", section: 123 },
{ user: 1, role: "exec", section: 123 },
{ user: 2, role: "staff", section: 456 },
{ user: 2, …Run Code Online (Sandbox Code Playgroud) 我写了一些代码来获取用户的输入,然后根据我的需要改变它.我需要它以改变和未改变的形式,所以我将输入保存为两个变量.我不明白的是为什么两个变量都在变化.我尝试了一些额外的放置线来确定原因是什么,但我无法弄清楚.代码:
puts "Enter the full directory path of the flv files."
folder = gets.chomp
puts "Folder 1: " + folder
path = folder
path.slice!(0..6)
path.gsub!('\\', '/')
path += '/'
puts "Folder: " + folder
puts "Path: " + path
Run Code Online (Sandbox Code Playgroud)
使用输入:f:\ folder\subfolder\another
输出:
Folder 1: f:\folder\subfolder\another
Folder: folder/subfolder/another
Path: folder/subfolder/another/
Run Code Online (Sandbox Code Playgroud)
我想要的是获取一个目录并保留其他进程的目录,还要将其转换为URL友好格式.想法?