将用户输入存储到哈希RUBY中

时间:2015-07-08 20:00:49

标签: ruby hash

我试图将所有用户的输入存储到哈希中,然后遍历哈希并显示结果。 输入:名字,姓氏,年龄,访问的城市(用户将输入多个城市,直到他们输入"退出"。

这是我到目前为止所得到的......除了能够在输入多个城市时将城市分成多个值时,一切看起来都不错。

result = ""
print "Enter first name "
first = gets.chomp
print "Enter last name "
last = gets.chomp
print "Enter age "
age = gets.chomp

while TRUE
print "Enter city"
city = gets.chomp
if city == "exit"
  break
end
result = result + " " + city
end

user_data = { first: first, last: last, age: age, city: result}

puts "#{user_data}"

1 个答案:

答案 0 :(得分:1)

最好为此目的使用数组,如下所示:

cities.join(' ')

之后,您可以通过连接数组的元素来进行字符串表示,例如

   /* SET Custom SQUARE CALL TO ACTION Button */

#ctasquare-button-container {
  text-align: center !important;
}

#ctasquare-button {
  text-align: center !important;
  border: solid #000000 2px;
  border-radius: 2px;
  -moz-border-radius: 2px;
  -webkit-box-shadow: 0 0px 0 0 #b13f21;
  -moz-box-shadow: 0 0px 0 0 #b13f21;
  box-shadow: 0 0px 0 0 #b13f21;
  position: absolute;
  margin: 0 0 0 -115px;
    top:30;
  -webkit-transition: .1s background-color linear;
  -moz-transition: .1s background-color linear;
  -o-transition: .1s background-color linear;
  transition: .1s background-color linear;
  padding: 21px 35px;
  color: #fff;
  border-color: #fff;
  font-family: Futura-Pt;
  font-size: 15px;
  text-transform: uppercase;
  letter-spacing: 2px;
  font-weight: 500;
  font-style: normal;
  cursor: pointer;
}

#ctasquare-button:hover {
  background-color: #fff;
  border-radius: 2px;
  -webkit-box-shadow: 0 0px 0 0 #b13f21;
  -moz-box-shadow: 0 0px 0 0 #b13f21;
  box-shadow: 0 0px 0 0 #b13f21;
  position: absolute;
  -webkit-transition: .1s background-color linear;
  -moz-transition: .1s background-color linear;
  -o-transition: .1s background-color linear;
  transition: .1s background-color linear;
  color: black;
}
/* END Custom SQAURE CALL TO ACTION Button */
/* overlay text on image properties */

.sidebyside {
  position: relative;
  width:100%;
  text-align:center;
  /* for IE 6 */
}

.sidebyside img{
  width: 100%;
  max-height: 100%;
  height: auto;
}

textoverlay {
  position: absolute;
  text-align: center;
  right: 0;
  left:0;
  bottom:0;
  top:0;
  padding: 0px 0px 0px 0px;
  margin: auto;
}

h2.TitleBlock {
  font-color: #fff;
  font-size: 62px;
  text-align: center;
  position: relative;
  font-weight: normal;
  margin: 30px 40px;

}
/* END overlay text on image properties */