Over a million developers have joined DZone.

Inputbox For Ruby On Windows

DZone's Guide to

Inputbox For Ruby On Windows

· ·
Free Resource
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})|)

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

#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}| 


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}