我想使用Sensu来监视python脚本,我对如何做到这一点感到困惑。
从Sensu文档来看,这需要。在提供的示例ruby脚本中,检查厨师客户端是否正在运行:
#!/usr/bin/env ruby
# get the current list of processes
processes = `ps aux`
# determine if the chef-client process is running
running = processes.lines.detect do |process|
process.include?('chef-client')
end
我正在使用Ruby1.9。
我有个哈希:
Hash_List={"ruby"=>"fun to learn","the rails"=>"It is a framework"}
我有一根这样的绳子:
test_string="I am learning the ruby by myself and also the rails."
我需要检查test_string是否包含与Hash_List键匹配的单词。如果是的话,用匹配的哈希值替换单词。
我使用了这段代码来检查,但是它返回的是空的:
another_
我最近从用Bash编写脚本转向用Ruby编写,我很好奇在Ruby中是否有一种传统的方式来提示批准?通常在Bash中,我会使用类似于以下内容的内容:
read -p "This may overwrite existing files in your home directory. Are you sure? (y/n) " -n 1
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
#The action
fi
我可以想出上百种方法在Ruby中执行这种类型的检查,但我很好奇是否有一种传统的方法来这样做?
在Ruby (不是Rails)的if条件中,检查变量是否为空的最佳方法是什么?
elsif not variable.to_s.empty?
# do something
end
或
elsif !variable.to_s.empty?
# do something
end
或
elsif variable.to_s.length > 0
# do something
end
我在使用ruby时发现了这一点,但它也适用于C的printf。
如果将ANSI颜色转义代码包含在输出字符串中,则会扰乱对齐。
红宝石:
ruby-1.9.2-head > printf "%20s\n%20s\n", "\033[32mGreen\033[0m", "Green"
Green # 6 spaces to the left of this one
Green # correctly padded to 20 chars
=> nil
C程序中的同一行产生相同的
我正在尝试查看字符串s是否包含正则表达式中的任何符号。下面的正则表达式在上运行良好。
s = "asd#d"
s =~ /[~!@#$%^&*()]+/
但在Ruby 1.9.2中,它给出了这个错误消息:
syntax error, unexpected ']', expecting tCOLON2 or '[' or '.'
s = "asd#d"; s =~ /[~!@#$%^&*()]/
怎么啦?
我有一个长字符串变量,想知道它是否包含两个子字符串中的一个。
例如:
haystack = 'this one is pretty long'
needle1 = 'whatever'
needle2 = 'pretty'
现在,我需要这样的分离,这在Ruby中是行不通的:
if haystack.include? needle1 || haystack.include? needle2
puts "needle found within haystack"
end