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

前端 未结 5 774
甜味超标
甜味超标 2021-01-31 07:53

I\'m making a small ruby command line script and I wanted to know what the simplest way to have the program emit a beep is.

相关标签:
5条回答
  • 2021-01-31 08:26

    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
    
    0 讨论(0)
  • 2021-01-31 08:28

    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
    
    0 讨论(0)
  • 2021-01-31 08:30

    Try the following:

    $stdout.beep
    
    0 讨论(0)
  • 2021-01-31 08:33

    For Mac OS X:

    system('say "beep"')
    

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

    0 讨论(0)
  • 2021-01-31 08:34

    Try printing the audible bell character:

    print "\a"
    
    0 讨论(0)
提交回复
热议问题