From Schmid.wiki
Jump to: navigation, search

References

Essential References

Tutorials

Extensions

Ruby Pages

Windows Ruby - Utility Ruby - Object-Oriented Ruby - Ruby Tricks - Ruby and Vim - Rake - Ruby on Rails - Ruby UI - Parse XML in Ruby - Ruby Web Spider

Functional Programming Example

s = lambda { |x| x.gsub!(/-/,'-=-') }
t = '-'
5.times{ s.call(t) }
puts t

Commands

ruby -e 'command;command'                    execute one line of Ruby code
ruby program.rb                              execute Ruby program
ruby -pe 'gsub(/regex/,"replacement")'       like sed
ruby -pi 'gsub(/regex/,"replacement")' file  like 'sed -i' (in-place)
irb                                          interactive ruby shell
ri -c                                        list all Ruby classes known to ri
ri Dir.open                                  get documentation for Dir.open

Why Ruby?

I like ruby, because it's intuitive, concise and very object-oriented.

Just to prove my point, check out the shortest fully functional 3D vector class I have ever written in any language.

class Vector
    attr_reader :x, :y, :z
    def initialize(x, y, z) @x, @y, @z = x, y, z end
    def to_s 
        ("(%+4.2f, %+4.2f, %+4.2f)" % [@x, @y, @z]).gsub(/\+/, " ")
    end

    def +(o) Vector.new(@x + o.x, @y + o.y, @z + o.z) end
    def -(o) Vector.new(@x - o.x, @y - o.y, @z - o.z) end
    def *(o) 
        case o
            # scalar multiplication
        when Numeric then Vector.new(@x * o, @y * o, @z * o) 
            # dot product
        when Vector  then o.x * @x + o.y * @y + o.z * @z
        end
    end
    def ==(o) @x == o.x and @y == o.y and @z == o.z end
end 

It has 'get'-methods for the elements, constructor, string output, addition, subtraction, scalar multiplication, dot product, and equality...

Modules

Ruby has third-party support for unit testing, mySQL, OpenGL, among others.

Stand-alone Ruby Programs

screenshot from electrons.exe - Ruby with OpenGL code packed with RubyScript2Exe

Try RubyScript2Exe. It packs all the necessary libraries into a single executable, and it works both on Linux and Windows.

Check out this Windows executable made with RubyScript2Exe.

* NOTE: When running the program, you must end the simulation by pressing a key, and not by closing the window. There's a bug in the OpenGL for Ruby implementation, it doesn't handle closing of the window, and a 'rubyw.exe' process will keep running, eating all your processing power. Anyways, if you forget, you can always just kill rubyw with ctrl+shift+esc.