What is the easiest way I can create a 'beep' sound from a Ruby program?

爷,独闯天下 提交于 2019-12-02 17:03:32

Try printing the audible bell character:

print "\a"

For Mac OS X:

system('say "beep"')

Conventional print "\a" didn't always work by some reason for me (MBA, 10.7.4)

Chris Hynes

For windows, use the win32-sound gem - Adding Sound to Your Ruby Apps.

To install:

gem install win32-sound

Then in Ruby:

require 'win32/sound'
include Win32
...
Sound.beep(100, 500)

For non-windows, looks like this could work: How to make beep sounds?

puts 7.chr

The easiest way is puts 7.chr

Here is a customize way

require "Win32API"
Beep = Win32API.new("kernel32", "Beep", ["I", "I"], 'v')
def beep freq, duration
  Beep.call(freq, duration)
end 

beep 600, 400

Try the following:

$stdout.beep
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!