InfraRuby Vision delivers solutions for mobile and web applications.
You can try InfraRuby live in your browser!
Try the InfraRuby statically typed Ruby compiler live in your browser. Try our examples or write your own code.
Follow @InfraRuby on Twitter for news and updates.

Wiktionary ✦ Examples

InfraRuby is a compiler and runtime for statically typed Ruby. You can use InfraRuby for your own projects with our free download!

coding style

The InfraRuby compiler supports many coding styles. In particular, you may use either spaces or tabs for indentation and you may omit the return keyword where applicable.

For more information:

This example implements an Android app to display a random entry from wiktionary.org:

NOTE: This example includes Java integration syntax, which is not specific to InfraRuby.

MainActivity

## <>
class MainActivity < JAVA::android.app.Activity
	## android.os.Bundle -> void
	def onCreate(savedInstanceState)
		super(savedInstanceState)
		setContentView(R::LAYOUT::MAIN)
		return
	end

	## -> void
	def onStart
		super()
		t = JAVA::java.lang.Thread.new(-> {
			c = WiktionaryClient.new
			title = c.get_random_article_title
			post_title(title)
			content = c.get_article_content(title)
			body = Wiktionary.render_wikitext(content)
			post_body(body)
		}.j)
		t.start
		return
	end

	## -> android.widget.TextView
	def title_view
		return findViewById(R::ID::TITLE).checkcast("android.widget.TextView")
	end

	## -> android.webkit.WebView
	def body_view
		return findViewById(R::ID::BODY).checkcast("android.webkit.WebView")
	end

	## ..String -> void
	def post_title(title)
		view = title_view
		view.post(-> {
			view.setText(title)
		}.j)
		return
	end

	## ..String -> void
	def post_body(body)
		view = body_view
		view.post(-> {
			view.loadDataWithBaseURL(nil, body, "text/html".j, "UTF-8".j, nil)
		}.j)
		return
	end
end

Wiktionary

## <>
module Wiktionary
	class << self
		## -> ..String
		def uri_for_query
			return "https://en.wiktionary.org/w/api.php?format=json&action=query&list=random".j
		end

		## ..String -> ..String
		def uri_for_article(title)
			s = JAVA::android.net.Uri.encode(title)
			return "https://en.wiktionary.org/w/api.php?format=json&action=query&prop=revisions&rvprop=content&titles=#{s}".j
		end

		## ..String -> ..String
		def render_wikitext(content)
			reader = JAVA::java.io.StringReader.new(content)
			printer = JAVA::org.wikimodel.wem.WikiPrinter.new
			parse_wikitext(reader, printer)
			return printer.toString
		end

		## .io.Reader, org.wikimodel.wem.WikiPrinter -> void
		def parse_wikitext(reader, printer)
			listener = JAVA::org.wikimodel.wem.xhtml.PrintListener.new(printer)
			parser = JAVA::org.wikimodel.wem.mediawiki.MediaWikiParser.new
			parser.parse(reader, listener)
			return
		end
	end
end

WiktionaryClient

## <>
##   @http_client: org.apache.http.client.HttpClient
class WiktionaryClient < JAVA::java.lang.Object
	## -> void
	def initialize
		super()
		@http_client = JAVA::org.apache.http.impl.client.DefaultHttpClient.new
		return
	end

	## ..String -> ..String
	def get(uri)
		request = JAVA::org.apache.http.client.methods.HttpGet.new(uri)
		response = @http_client.execute(request)
		return Util.read(response.getEntity.getContent)
	end

	## ..String -> org.json.JSONObject
	def get_json(uri)
		return JAVA::org.json.JSONObject.new(get(uri))
	end

	## -> ..String
	def get_random_article_title
		json = get_json(Wiktionary.uri_for_query)
		page = json.getJSONObject("query".j).getJSONArray("random".j).getJSONObject(0.i32)
		return page.getString("title".j)
	end

	## ..String -> ..String
	def get_article_content(title)
		json = get_json(Wiktionary.uri_for_article(title))
		pages = json.getJSONObject("query".j).getJSONObject("pages".j)
		id = pages.names.getString(0.i32)
		page = pages.getJSONObject(id)
		return page.getJSONArray("revisions".j).getJSONObject(0.i32).getString("*".j)
	end
end

Util

## <>
module Util
	class << self
		## .io.InputStream, .io.OutputStream -> void
		def copy(is, os)
			m = JAVA::byte[0x1000].new
			while true
				n = is.read(m)
				break if n < 0
				os.write(m, 0.i32, n)
			end
			return
		end

		## .io.InputStream -> ..String
		def read(is)
			os = JAVA::java.io.ByteArrayOutputStream.new
			copy(is, os)
			return os.toString("UTF-8".j)
		end
	end
end
Follow @InfraRuby on Twitter
Copyright © 2011-2017 InfraRuby Vision Limited