尝试使用 ruby 迭代数组,结果惨败,
My Array
people = [{first_name: "Gary", job_title: "car enthusiast", salary: "14000" },
{first_name: "Claire", job_title: "developer", salary: "15000"},
{first_name: "Clem", job_title: "developer", salary: "12000"}]
Run Code Online (Sandbox Code Playgroud)
如何迭代上面的哈希以仅输出工资值???
我尝试使用:
people.each do |i,j,k|
puts "#{i}"
end
Run Code Online (Sandbox Code Playgroud)
结果如下,不是我想要的,
{:first_name=>"Gary", :job_title=>"car enthusiast", :salary=>"14000"}
{:first_name=>"Claire", :job_title=>"developer", :salary=>"15000"}
{:first_name=>"Clem", :job_title=>"developer", :salary=>"12000"}
Run Code Online (Sandbox Code Playgroud)
有没有办法迭代这个数组并仅列出工资值而不列出其余值?
这是我第一次涉足计算机编程.我选择学习Ruby,我很享受它.但是,我有点困惑的是为什么答案在这段代码中输出不正确.
def addition_function
puts "Which numbers would you like to add?"
@n1 = gets.chomp
@n2 = gets.chomp
@n1 + @n2 == @answer
puts "The sum is... #{@answer}"
end
def subtraction_function
puts "Which numbers would you like to subtract?"
@n1 = gets.chomp.to_i
@n2 = gets.chomp.to_i
@n1 - @n2 == @answer
puts "The answer is... #{@answer}"
end
def multiplication_function
puts "Which numbers would you like to multiply?"
@n1 = gets.chomp
@n2 = gets.chomp
@n1 * @n2 == @answer
puts "The answer is... …Run Code Online (Sandbox Code Playgroud) 在http://learnrubythehardway.org/book/ex35.html的第7行,有一条评论说明有一个错误.
当这个程序运行并且您选择一个包含的数字时,既不是"0"也不是"1",它会说它不是数字.如何让它检测所有数字?
这是代码:
def gold_room
puts "This room is full of gold. How much do you take?"
print "> "
choice = $stdin.gets.chomp
# this line has a bug, so fix it
if choice.include?("0") || choice.include?("1")
how_much = choice.to_i
else
dead("Man, learn to type a number.")
end
if how_much < 50
puts "Nice, you're not greedy, you win!"
exit(0)
else
dead("You greedy bastard!")
end
end
Run Code Online (Sandbox Code Playgroud) 我有一个字符串:"C:/This/is/a/windows/path/file20110312name20120221.csv".
我想提取所有日期(例如[0-9]{8}).
通常,如何从较大的字符串中提取与模式匹配的子字符串?