小编Ste*_*n C的帖子

按键后跟值对哈希数组进行分组

假设我有以下数据集

[ 
  {
    :name => "sam",
    :animal => "dog",
    :gender => "male"
  }, {
    :name => "max",
    :animal => "cat",
    :gender => "female"
  }, {
    :name => "joe",
    :animal => "snake",
    :gender => "male"
  }    
]
Run Code Online (Sandbox Code Playgroud)

您将如何将哈希数组分组为:

{
  :name => ["sam", "max", "joe"]
  :animal => ["dog", "cat", "snake"]
  :gender => ["male", "female", "male"]
}
Run Code Online (Sandbox Code Playgroud)

我已经阅读过类似的文章,例如thisGroup array of hashes by key

但是,大多数示例将值作为增量计数返回,我正在寻找实际的单独值。

我的尝试

keys = []
values = []

arr.each do |a|
  a.each do |k, v|
    keys << …
Run Code Online (Sandbox Code Playgroud)

ruby

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

如何在Gmaps4rails中居中标记

看完这个答案后,我仍然无法将标记显示在页面中央.

我的代码如下;

在视图中

<div class="Flexible-container">
<!--Google Maps Div-->
          <div id="map" style='width: 425; height: 425px;' frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></div>
</div>
          <!-- End of Responsive iFrame -->
          <script type="text/javascript">
          handler = Gmaps.build('Google');
          handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
            markers = handler.addMarkers(<%=raw @hash.to_json %>);
            handler.map.centerOn(markers[0]);
            handler.bounds.extendWith(markers);
            handler.fitMapToBounds();
            handler.map.serviceObject.setZoom(16);
          });
          </script>
Run Code Online (Sandbox Code Playgroud)

在控制器中

def edit
    @user = current_user
    @hash = Gmaps4rails.build_markers(@user) do |u, marker|
  marker.lat u.latitude
  marker.lng u.longitude
  marker.infowindow u.name
  end
end
Run Code Online (Sandbox Code Playgroud)

CSS

/* Flexible iFrame */

.Flexible-container {
position: relative;
padding-bottom: …
Run Code Online (Sandbox Code Playgroud)

javascript google-maps-api-3 gmaps4rails ruby-on-rails-4

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

图像顶部的中心离子图标

在阅读悬停图像上的“中心字体真棒”图标及其相应答案后,我仍然无法使用离子图标复制此答案。

我正在使用的代码如下:

<div class="video-thumbnail">
  <img src="image here" />
  <i class="icon ion-play"></i>
</div>
Run Code Online (Sandbox Code Playgroud)

我使用的 CSS 是:

.video-thumbnail {
  position: relative;
  margin: 0 auto;
  display: inline-block;
}

.video-thumbnail img {
  position: absolute;
}

.video-thumbnail i {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}
Run Code Online (Sandbox Code Playgroud)

我已经把它放在彼此之上,但似乎找不到中间点。寻找水平和垂直居中。我不是在寻找任何悬停状态,只是图像上的一个明显图标。

谢谢!

css center ionic-framework ionic

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

按键排序哈希,这是一个字符串

假设我收到一个字符串:

"27,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,12,17,17,41,17,17,17,17,17,17,17,17,17,17,17,17,17,26,26,26,26,26,26,26,26,26,29,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,40,48,28,28,28,28,28,28,28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,34,34,34,34,34,34,36,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,39,40,40,40,40,40,40,40,40,41,41,41,41,41,41,41,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,42,43,43,43,43,43,43,43,43,43,43,43,43,43,44,44,44,44,48,49,29,41,6,30,11,29,29,36,29,29,36,29,43,1,29,29,29,1,41"
Run Code Online (Sandbox Code Playgroud)

我通过调用把它变成一个数组

str.split(',')
Run Code Online (Sandbox Code Playgroud)

然后通过调用将其变为哈希

arr.compact.inject(Hash.new(0)) { |h, e| h[e] += 1 ; h }
Run Code Online (Sandbox Code Playgroud)

我会得到一个看起来像哈希的哈希

{"1"=>2, "6"=>1, "39"=>23, "36"=>23, "34"=>39, "32"=>31, "30"=>18, "3"=>8, "2"=>10, "28"=>36, "29"=>21, "26"=>41, "27"=>48, "49"=>1, "44"=>4, "43"=>14, "42"=>34, "48"=>2, "40"=>9, "41"=>10, "11"=>1, "17"=>15, "12"=>1}
Run Code Online (Sandbox Code Playgroud)

但是,我想按密钥对哈希进行排序.

我试过这里列出的解决方案.

我相信我的问题与他们的键是字符串的事实有关.

我得到的最接近的是使用

Hash[h.sort_by{|k,v| k.to_i}]
Run Code Online (Sandbox Code Playgroud)

ruby sorting hash

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