Over a million developers have joined DZone.

Inputbox For Ruby On Windows

·
This is a simple inputbox dialog (windows only)

def inputbox( message, title="Message from #{__FILE__}" )
  # returns nil if 'cancel' is clicked
  # returns a (possibly empty) string otherwise
  require 'win32ole' 
  # hammer the arguments to vb-script style
  vb_msg = %Q| "#{message.gsub("\n",'"& vbcrlf &"')}"|
  vb_msg.gsub!( "\t", '"& vbtab &"' )
  vb_msg.gsub!( '&""&','&' )
  vb_title = %Q|"#{title}"|
  # go!
  sc = WIN32OLE.new( "ScriptControl" )
  sc.language = "VBScript"
  sc.eval(%Q|Inputbox(#{vb_msg}, #{vb_title})|)
end

def popup(message)
  require 'win32ole'
  wsh = WIN32OLE.new('WScript.Shell')
  wsh.popup(message, 0, __FILE__)
end

#simple use
res = inputbox "Your input please." 
popup res

#using linebreaks and tabs
str = "a |  does not break it...\n\nOne\n\tTwo tabbed\nThree..."
res = inputbox( str, "demonstration | title")
popup %Q|When asked\n\n"#{str}"\n\nyou answered:\n#{res}| 

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}