Difference between revisions of "Ruby"

From RogueBasin
Jump to navigation Jump to search
(Moved Ruby Toolbox to external links as it's not an actual library)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
=Ruby=
{{Programming language| name = Ruby
|company = Yukihiro “Matz” Matsumoto
|influences = Perl, Smalltalk, Eiffel, Ada, and Lisp
|updated = May 9, 2014 (2.1.2)
|status = Stable
|licensing = Ruby License or BSD License
|platforms = [[Linux]], [[Unix]], [[Mac OS X]], [[Windows]] and others.
|site = http://www.ruby-lang.org}}


Ruby is still coming of age in the roguelike field -- there are no roguelike libraries and only a few finished games in the language. That said, Ruby is very powerful, combining an elegant syntax with an incredibly dynamic object system. It's also incredibly fun to code with. But beware! Many dangers lurk within beneath its alluring facade!
== What is Ruby? ==


In terms of speed, Ruby 1.8 measured far below Python in terms of performance. This is mostly due to Python having a bytecode compiler. However, Ruby 1.9 has JIT compilation and <i>out-performs</i> Python. It should be noted however that ruby does have a C API, and extending Ruby in C can result in very fast code (only a bit slower than C itself).
"Ruby is...a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write." It is a pure object orientated language (in the vein of [[Smalltalk]], rather than Simula), but also supports procedural and functional programming.


Ruby is not well documented in relation to other languages, but this is getting better all the time. There are quite a few gotchas (like using <tt>setter=</tt> methods inside of classes).
==Roguelike Specific Details==


==Libraries==
Ruby is still coming of age in the roguelike field -- there are no roguelike libraries and only a few finished games in the language.  That said, Ruby is very powerful, combining an elegant syntax with an incredibly dynamic object system. It's also incredibly fun to code with.


*[http://ippa.se/chingu Chingu], a game framework that extends Gosu with higher level features
===Libraries===
*[http://www.libgosu.org/ Gosu], a 2D game development library
*[[Ncurses]], cross-platform, true Curses support (built-in [[Curses]] support has no colours under Windows and is generally a mess)
*[http://rubygame.org/ Rubygame], multimedia library for entertainment software, scientific data visualisation programs, educational/training tools and interactive motion graphics


* [http://www.libgosu.org/ Gosu], a 2D game development library
* [http://ippa.se/chingu Chingu], a game framework that extends Gosu with higher level features
* [[Ncurses]], cross-platform, true Curses support (built-in [[Curses]] support has no colours under Windows and is generally a mess)


*[http://raa.ruby-lang.org/cat.rhtml?category_major=Library;category_minor=Game Ruby Aplication Archive - Library / Game]
===Interfacing with C===
*[http://ruby-toolbox.com/categories/game_libraries.html Game libraries in The Ruby Toolbox]


As people begin to use Ruby for RL development, code will surface. There are already a few algorithms implemented and put up at RogueBasin.
Ruby MRI, the canonical and most popular Ruby distribution, is implemented in [[C]] and can make use of C libraries and extensions relatively easy. This is great for leveraging libraries such as [[libtcod]], as well as for writing performance and/or memory critical parts of your application.


==Okay, I'm going to use Ruby. What now?==
* [http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html Extending Ruby - The Pragmatic Programmer's Guide] A guide to writing C extensions for Ruby. The overhead of this is minimal, but it does require a modicum of boilerplate.
 
* [http://www.zenspider.com/ZSS/Products/RubyInline/ RubyInline] Is a third party module that allows you to inline C code in pure ruby. The overhead is around 2*.
You'll need to install a copy of Ruby from http://www.ruby-lang.org/ (I recommend the one-click installer) or your package manager (<tt>apt-get install ruby</tt>). Then acquaint yourself with the Ruby tools: <tt>irb</tt>, <tt>ri</tt> and <tt>rdoc</tt>. A good version of the standard library API can be found at http://www.noobkit.com/


===ncurses-ruby===
===ncurses-ruby===
Line 37: Line 42:
===Line of sight===
===Line of sight===


[[Bresenham's Line Algorithm|Here]] is a Bresenham's Line Algorithm implementation in Ruby
[[Bresenham's Line Algorithm|Here]] is a Bresenham's Line Algorithm implementation in Ruby.


===Field of View===
===Field of View===
Line 51: Line 56:


Using this library, it is easy to add methods to your Map class for FOV calculation:
Using this library, it is easy to add methods to your Map class for FOV calculation:
 
<div style="background-color: #EEEEEE; border-style: dotted; padding: 0.3em">
<pre>
<source lang="ruby">
def initialize(map)
def initialize(map)
    ....
  ....
    @visited = BitField.new(@width * @height) # visited by player (drawn in grey)
  @visited = BitField.new(@width * @height) # visited by player (drawn in grey)
    @lit = BitField.new(@width * @height) # seen by player this round (drawn in white)
  @lit = BitField.new(@width * @height) # seen by player this round (drawn in white)
end
end


# Return true if the square is lit (seen by player)
# Return true if the square is lit (seen by player)
def lit?(x, y)
def lit?(x, y)
    @lit[y * @width + x] == 1
  @lit[y * @width + x] == 1
end
end


# Returns true if square has been visited by player
# Returns true if square has been visited by player
def visited?(x, y)
def visited?(x, y)
    @visited[y * @width + x] == 1
  @visited[y * @width + x] == 1
end
end


# Set lit status for square. Also visits the square
# Set lit status for square. Also visits the square
def light(x, y)
def light(x, y)
    idx = y * @width + x
  idx = y * @width + x
    @lit[idx] = @visited[idx] = 1
  @lit[idx] = @visited[idx] = 1
end
end


# Unlight everything (call after doing FOV calc + map draw)
# Unlight everything (call after doing FOV calc + map draw)
def reset_light
def reset_light
    @lit.clear
  @lit.clear
end
end
</pre>
</source>
</div>
 
=== Other Resources ===
 
Over on [http://rubyquiz.com RubyQuiz] there are several quiz solutions which would be useful to anyone wanting to use Ruby for terminal RL games;
 
* [http://rubyquiz.com/quiz5.html Sokaban]. Full game with examples that use Gosu, Curses, OpenGL and plain old terminal. James's 'unix' entry could be useful for a pure ASCII RL jump off point.
* [http://rubyquiz.com/quiz61.html AD&D Dice Roller]. Dennis Ranke's full_parser_roll.rb is very interesting.
* [http://rubyquiz.com/quiz80.html Dungeon Generation].
* [http://rubyquiz.com/quiz31.html Maze Generation]
 
== External links ==
 
* [http://www.ruby-lang.org Official Ruby website]
* [http://ruby-doc.org Ruby API Documentation]
 
* [http://ruby-toolbox.com/categories/game_libraries.html The Ruby Toolbox] Listing of current Ruby game libraries and their popularity.
* [http://www.rubymotion.com RubyMotion], develop OSX, iOS, and Android apps/games using Ruby.
 
[[Category:Programming languages]]
[[Category:Programming languages]]

Latest revision as of 18:20, 5 September 2014

Ruby
Programming Language
Company Yukihiro “Matz” Matsumoto
Influences Perl, Smalltalk, Eiffel, Ada, and Lisp
Updated May 9, 2014 (2.1.2)
Status Stable
Licensing Ruby License or BSD License
Platforms Linux, Unix, Mac OS X, Windows and others.
Official site of Ruby


What is Ruby?

"Ruby is...a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write." It is a pure object orientated language (in the vein of Smalltalk, rather than Simula), but also supports procedural and functional programming.

Roguelike Specific Details

Ruby is still coming of age in the roguelike field -- there are no roguelike libraries and only a few finished games in the language. That said, Ruby is very powerful, combining an elegant syntax with an incredibly dynamic object system. It's also incredibly fun to code with.

Libraries

  • Gosu, a 2D game development library
  • Chingu, a game framework that extends Gosu with higher level features
  • Ncurses, cross-platform, true Curses support (built-in Curses support has no colours under Windows and is generally a mess)

Interfacing with C

Ruby MRI, the canonical and most popular Ruby distribution, is implemented in C and can make use of C libraries and extensions relatively easy. This is great for leveraging libraries such as libtcod, as well as for writing performance and/or memory critical parts of your application.

ncurses-ruby

It is possible to use the built-in curses on linux and other platforms, and interface with the Win32 console via the Win32 API for Windows support. However, this is a relative pain. Instead, install the non-standard ncurses-ruby package!

On Windows:

  • Download ncurses-ruby1.8-0.9.1-i386-mswin32.zip from http://ncurses-ruby.berlios.de/
  • Copy lib/ncurses.rb and ncurses.so into your code directory (don't copy the lib directory itself!)
  • In your script, require 'ncurses'

That's it! You can now use ncurses (see the zdennis's ncurses examples at http://github.com/zdennis/ncurses_examples/tree/master or the C API, which is very similar). At some point an example roguelike using ncurses will be uploaded.

Line of sight

Here is a Bresenham's Line Algorithm implementation in Ruby.

Field of View

A few field of view algorithms have been implemented in Ruby

Bitfields

For certain intensive operations it may be prudent to use a Bitfield. One example is storing which map tiles have been visited by your player (and will be drawn).

Using this library, it is easy to add methods to your Map class for FOV calculation:

def initialize(map)
  ....
  @visited = BitField.new(@width * @height) # visited by player (drawn in grey)
  @lit = BitField.new(@width * @height) # seen by player this round (drawn in white)
end

# Return true if the square is lit (seen by player)
def lit?(x, y)
  @lit[y * @width + x] == 1
end

# Returns true if square has been visited by player
def visited?(x, y)
  @visited[y * @width + x] == 1
end

# Set lit status for square. Also visits the square
def light(x, y)
  idx = y * @width + x
  @lit[idx] = @visited[idx] = 1
end

# Unlight everything (call after doing FOV calc + map draw)
def reset_light
  @lit.clear
end

Other Resources

Over on RubyQuiz there are several quiz solutions which would be useful to anyone wanting to use Ruby for terminal RL games;

External links

  • The Ruby Toolbox Listing of current Ruby game libraries and their popularity.
  • RubyMotion, develop OSX, iOS, and Android apps/games using Ruby.