Strings and IO are among the most important aspects of practical programming. This is why Java makes them both so difficult to deal with.


In Gosu, strings can be delimited with either a single or double quote. This allows you to embed the other character without any awkward escaping:

  var strWithDoubleQuote = 'This is a "string"'
  var strWithSingleQuote = "This is a 'string'"
You can, of course, use the + operator to concatenate:
  print( "Hello" + " " + "World" )
You can also use JSP 2.0-style template syntax for including values in strings:
  var world = "World"
  print( "Hello ${world}" )

String Conversions

Strings have a bunch of methods that can be used to convert them to other data types. Here are some examples:

  var bool = "true".toBoolean()
  var i = "42".toInt()
  var d = "1/1/2001".toDate()
These methods are added to java.lang.String via Enhancements, which we will talk about next. Usually they just chain through to some standard Java API that has been hidden away in some obscure location.

Other String Enhancements

In addition to the .to* methods for conversions, a bunch of other useful methods have been added to Strings. Here is a sampling:

Gosu String Templates

Gosu supports string templates as first class citizens in the language. A Gosu String Template is a file that ends in the .gst extension. Here is an example definition, sample.Sample.gst:

  <% params( names : String[] ) %>
  All Names: <% for( name in names ) { %>
    * ${name} 
  <% } %>
The template explicitly declares the names and types of its arguments. You can render the template by calling the render() or renderToString() methods:
  var str = sample.Sample.renderToString( {"Joe", "John", "Josh"} )
Note that the static renderToString() method is statically typed, and takes exactly the parameters specified by the template, so everything is type checked.

There is also a static render() method that takes a writer, as well as the arguments to the template, for more efficient creation of large strings.

File I/O

Here is how you read a File:

  var f = new File( "/tmp/temp.txt")
  var content =
and here is how you write a File:
  var f = new File( "/tmp/temp.txt")
  var content = f.write( "It's so crazy, it just might work..." )
and here is how you read a file a line at a time:
  var f = new File( "/tmp/temp.txt")
  f.eachLine( \ s -> print( s ) )
That funny backslash/arrow thing is a block (or a closure, or a lambda expression.) I'll explain those in the next section, on data structures.

Files have some other useful tricks added to them as well. Play around with them to see.

Interacting With The OS

Sun doesn't want you to interact with the OS, except via standard Java APIs. While standardized APIs are great, there are times when push comes to shove and you just need to exec something.

Gosu provides a class, gw.util.Shell, to make this easy:
  uses gw.util.Shell
  listingOfDir = Shell.exec( "ls" )
Shell has other methods for more elaborate interactions with the OS if necessary.