Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:如何从用户输入动态构建哈希_Ruby - Fatal编程技术网

Ruby:如何从用户输入动态构建哈希

Ruby:如何从用户输入动态构建哈希,ruby,Ruby,我有一个像下面这样的杂烩 h = {a: 1, b: 2, c: 3, d: 4.....z: 26} 现在用户输入1,然后我将获取第一个5,如果用户输入2,那么6到下一个5表示6到11 如何以最佳方式实现这一点,这样您就可以简单地使用切片数组函数来分割内容 alphabets = [*('a'..'z')].collect.with_index{ |key,index| { key => index+1 } } user_input = 1 # Capture use

我有一个像下面这样的杂烩

h = {a: 1, b: 2, c: 3, d: 4.....z: 26}
现在用户输入1,然后我将获取第一个5,如果用户输入2,那么6到下一个5表示6到11


如何以最佳方式实现这一点

,这样您就可以简单地使用切片数组函数来分割内容

   alphabets = [*('a'..'z')].collect.with_index{ |key,index| { key => index+1  }  } 
   user_input = 1 # Capture user input here. 
   part_of_array = alphabets[( (user_input - 1) * 5 )..( (user_input * 5) - 1)]
使用下面的代码转换为简单的一个散列

part_of_array = eval(alphabets[( (user_input - 1) * 5 )..( (user_input * 5) - 1)].to_s.gsub("{","").gsub("}",""))

如果您有任何问题,请告诉我

所以您可以简单地使用slice数组函数来拆分内容

   alphabets = [*('a'..'z')].collect.with_index{ |key,index| { key => index+1  }  } 
   user_input = 1 # Capture user input here. 
   part_of_array = alphabets[( (user_input - 1) * 5 )..( (user_input * 5) - 1)]
使用下面的代码转换为简单的一个散列

part_of_array = eval(alphabets[( (user_input - 1) * 5 )..( (user_input * 5) - 1)].to_s.gsub("{","").gsub("}",""))

如果您有任何问题,请告诉我

我假设这个问题涉及任意散列

代码

三种选择:

#一,

#二,

#三,

此方法使用Ruby的

所有这些方法都使用(返回散列),而不是(返回数组)

示例

h = {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5, :f=>6, :g=>7, :cat=>"meow",  :dog=>"woof",
     :h=>8, :i=>9, :j=>10, :k=>11, :l=>12, :m=>13, :n=>14, :o=>15,
     :p=>16, :q=>17, :r=>18, :s=>19, :t=>20, :u=>21, :v=>22, :w=>23,
     :x=>24, :y=>25, :z=>26} 

select_em(h, 1)
  #=> {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5} 
select_em(h, 2)
  #=> {:f=>6, :g=>7, :cat=>"meow", :dog=>"woof", :h=>8} 
select_em(h, 3)
  #=> {:i=>9, :j=>10, :k=>11, :l=>12, :m=>13} 
select_em(h, 4)
  #=> {:n=>14, :o=>15, :p=>16, :q=>17, :r=>18} 

我假设这个问题涉及任意散列

代码

三种选择:

#一,

#二,

#三,

此方法使用Ruby的

所有这些方法都使用(返回散列),而不是(返回数组)

示例

h = {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5, :f=>6, :g=>7, :cat=>"meow",  :dog=>"woof",
     :h=>8, :i=>9, :j=>10, :k=>11, :l=>12, :m=>13, :n=>14, :o=>15,
     :p=>16, :q=>17, :r=>18, :s=>19, :t=>20, :u=>21, :v=>22, :w=>23,
     :x=>24, :y=>25, :z=>26} 

select_em(h, 1)
  #=> {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5} 
select_em(h, 2)
  #=> {:f=>6, :g=>7, :cat=>"meow", :dog=>"woof", :h=>8} 
select_em(h, 3)
  #=> {:i=>9, :j=>10, :k=>11, :l=>12, :m=>13} 
select_em(h, 4)
  #=> {:n=>14, :o=>15, :p=>16, :q=>17, :r=>18} 

下面是我要做的:

char_ary = ('a'..'z').to_a
start_idx = (input - 1) * 5
subary = char_ary.slice(start_idx, 5)
subary.inject({}) do |h, c|
  h[c.to_sym] = char_ary.index(c) + 1
  h
end

这不需要用所有字母表定义散列。

下面是我要做的:

char_ary = ('a'..'z').to_a
start_idx = (input - 1) * 5
subary = char_ary.slice(start_idx, 5)
subary.inject({}) do |h, c|
  h[c.to_sym] = char_ary.index(c) + 1
  h
end

这不需要用所有字母表定义散列。

此代码将使用独立散列生成数组:
[{“a”=>1},{“b”=>2},{“c”=>3},{“d”=>4},{“e”=>5}]
Simple。eval(alphabets[((user_input-1)*5)((user_input*5)-1)]。to_.gsub(“{”,”).gsub(“},”))我对@Jagdeep的答案的评论也适用于你的答案。这段代码将生成带有独立哈希的数组:
[{a=>1},{b=>2},{c=>3},{d=>4},{e=>5}]
Simple。eval(alphabets[((user_input-1)*5)…((user_input*5)-1)]。对_.gsub(“{,”).gsub(“},”))我对@Jagdeep答案的评论也适用于你的答案。我相信给出的哈希只是一个例子。如果是特定的散列,你不需要做任何切片或选择,直接构造5个元素的散列。我相信给出的散列只是一个例子。如果是那个特定的散列,你不需要做任何切片或选择,只需直接构造5个元素的散列。1)
h
应该是一个有效的Ruby散列(没有
),这样读者就可以剪切和粘贴,而不必构建散列。而且,
会给人留下懒惰的印象。2.如果你是说它是一个任意的散列(我假设是这样),你就不应该有这样一个规则模式(
:a
:b
:c
,…和
1
2
3
,…),因为一些读者会将你的问题解释为指向特定的散列。你的问题不清楚。请定义“最佳方式”的含义。还有,你说的“像这样”是什么意思?总是那样吗?是什么样的模式使散列“像”这个?我恐怕一点也不明白:“现在用户输入1,那么我将先取5,如果用户输入2,那么6到下一个5意味着6到11”。您能否编辑您的问题,以显示不同输入和相应输出的具体示例?您可以通过两种方式改进您的答案。1)
h
应该是一个有效的Ruby散列(没有
),这样读者就可以剪切和粘贴,而不必构建散列。而且,
会给人留下懒惰的印象。2.如果你是说它是一个任意的散列(我假设是这样),你就不应该有这样一个规则模式(
:a
:b
:c
,…和
1
2
3
,…),因为一些读者会将你的问题解释为指向特定的散列。你的问题不清楚。请定义“最佳方式”的含义。还有,你说的“像这样”是什么意思?总是那样吗?是什么样的模式使散列“像”这个?我恐怕一点也不明白:“现在用户输入1,那么我将先取5,如果用户输入2,那么6到下一个5意味着6到11”。请编辑您的问题,以显示不同输入和相应输出的具体示例?
char_ary = ('a'..'z').to_a
start_idx = (input - 1) * 5
subary = char_ary.slice(start_idx, 5)
subary.inject({}) do |h, c|
  h[c.to_sym] = char_ary.index(c) + 1
  h
end