jRuby / Rails 如何将一个哈希值与另一个进行比较

发布于 2024-12-20 21:26:20 字数 961 浏览 2 评论 0原文

我所拥有的是 2 个哈希值,它是从不专门处理 Rails 的后端转储的。另一种是手动构建的硬编码哈希。有点像已知可能性的索引。

我想做的是将后端吐出的内容与我的散列中的内容进行比较,就像索引一样,这样当我渲染视图时,我可以让显示看起来更有吸引力。

现在我可以用 php 和一个数组做这样的事情

array("monkey" => "Monkey", "server" => "server")
and then do a str_replace("monkey", $var, array)

,好吧,这是一个很糟糕的例子,但是自从我玩 php 以来已经有一段时间了,我的头脑有点生疏了。但这就是

最终结果,当我在其中找到“猴子”时,为了美观,我想将其替换为“猴子”。

编辑/修订/添加

好吧,我意识到我不使用哈希,我实际上使用数组或 JSON 对象。

pretty_service = {"namenode" => "Name Nodes","secondarynamenode" => " Secondary Name Nodes", "datanode" => "Data Nodes", "web" => "Web", "tasktracker" => "Task Trackers", "jobtracker" => "Job Trackers", "oozie" => "Oozie", "single-namenode" => "Single NameNode", "single-databse" => "Single Database" }

我想要的是我正在检查的字符串是否包含任何一个键我想用值替换它。不知道如何通过 Rails 样式编码准确地做到这一点。下面已经有一些不错的概念,但我已经尝试过它们,但它们似乎不适合我。现在知道了这一点,是否有不同的方法来解决这个问题?

What I have is 2 hash's one gets dumped from a backend that deals nothing specifically with rails. The other is a manually built hard-coded hash. Kind of like a index of known possibilities.

What I want to do is compare what the backend spits out vs what I have in my hash thats like an index so when I render my view I can have the display look a bit more appealing.

Now I could do something like this with php and an array

array("monkey" => "Monkey", "server" => "server")
and then do a str_replace("monkey", $var, array)

ok thats a poor example but its been a while since I played with php and I am a bit rusty off the top of my head. But thats the notion

the end result is when I find "monkey" in one I want to replace it with "Monkey" for the view's sake.

edit/revision/addition

Ok realized Im not working with a hash, im actually working with an array or a JSON object..

pretty_service = {"namenode" => "Name Nodes","secondarynamenode" => " Secondary Name Nodes", "datanode" => "Data Nodes", "web" => "Web", "tasktracker" => "Task Trackers", "jobtracker" => "Job Trackers", "oozie" => "Oozie", "single-namenode" => "Single NameNode", "single-databse" => "Single Database" }

What I want to is if my strings I am checking contain any one of the keys I want to replace it with the value. Not sure how exactly to do that with rails styling coding. There is some decent concepts below already but I've tried them and they don't seem to work for me. Knowing this now, is there a differnt approach to working this up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最偏执的依靠 2024-12-27 21:26:20

String.gsub 可以做到这一点。

table = {"monkey"=>"Monkey", "teh"=>"the"}
str = "Don't feed teh monkey"
p str.gsub(Regexp.union(table.keys), table)
#=> "Don't feed the Monkey"

String.gsub can do that.

table = {"monkey"=>"Monkey", "teh"=>"the"}
str = "Don't feed teh monkey"
p str.gsub(Regexp.union(table.keys), table)
#=> "Don't feed the Monkey"
怂人 2024-12-27 21:26:20

这是使用哈希函数的示例。您可以根据您的需要进行调整:

if my_hash.keys.include?("monkey")
   what_i_will_render.gsub!("monkey",my_hash["monkey"])
end

Here's an example of using the hash functions. You can adapt this to your needs:

if my_hash.keys.include?("monkey")
   what_i_will_render.gsub!("monkey",my_hash["monkey"])
end
弥枳 2024-12-27 21:26:20
find = {:a => 'a', :b => 'b', :c => 'c'}
replace = {:a => 'A', :b => 'B'}
replace.merge(find) # {:a => 'A', :b => 'B', :c => 'c'}
find = {:a => 'a', :b => 'b', :c => 'c'}
replace = {:a => 'A', :b => 'B'}
replace.merge(find) # {:a => 'A', :b => 'B', :c => 'c'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文