jRuby / Rails 如何将一个哈希值与另一个进行比较
我所拥有的是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
String.gsub 可以做到这一点。
String.gsub can do that.
这是使用哈希函数的示例。您可以根据您的需要进行调整:
Here's an example of using the hash functions. You can adapt this to your needs: