Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Thursday, October 4, 2012

Tab-Completion and Syntax coloring for irb (Ruby Interpreter)

Wirble is a Ruby Gem that provides Tab-Completion and syntax coloring for the Ruby interpreter, irb. In this article we'll see how to install it and configure it.

irb with Tab-Completion and Syntax coloring
irb with Tab-Completion and Syntax coloring

Install

Open a terminal window and run:

sudo gem install wirble && echo -n "require 'rubygems'\nrequire 'wirble'\nWirble.init\nWirble.colorize" >> ~/.irbrc

Usage

Now that Wirble is installed and irbrc is properly configured, (re)open irb and then you can start using the "TAB" key to auto-complete while typing a Ruby instruction. Only the results will be colorized, not the instructions typed.

References

Wirble: Tab-Completion and Syntax Coloring for irb | Ruby Inside

Thursday, September 27, 2012

[HOW TO] Install gtk-webkit-ruby Gem

WebKit is an open source web browser engine. gtk-webkit-ruby is a Ruby Gem that provides Gtk Webkit bindings for ruby, making it possible to embed a webview (class that has the ability to render a webpage) in a Ruby-GNOME2 application.

In this article we'll see how to install it in any Ubuntu distribution.

Install

Open a terminal window and run:

sudo apt-get install ruby-gnome2-dev -y && sudo apt-get install libwebkitgtk-dev -y && sudo gem install gtk-webkit-ruby -y

Usage

Bellow are the main instructions that should be added to a program in order to render a webpage within a Ruby-GNOME2 application. Please note that this is not a runnable example.

require "gtk2"
require "webkit"

webview = WebKit::WebView.new
webview.show
webview.open(url) # url is a string

Note

While I was trying to install the Gem I encountered some errors that were caused by dependencies not being installed in my system: ruby-gnome2-dev and libwebkitgtk-dev. Below I pasted the error messages for future reference.

### ruby-gnome2-dev dependency error
$ sudo gem install gtk-webkit-ruby
Building native extensions.  This could take a while...
ERROR:  Error installing gtk-webkit-ruby:
 ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.8 extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
 --with-opt-dir
 --without-opt-dir
 --with-opt-include
 --without-opt-include=${opt-dir}/include
 --with-opt-lib
 --without-opt-lib=${opt-dir}/lib
 --with-make-prog
 --without-make-prog
 --srcdir=.
 --curdir
 --ruby=/usr/bin/ruby1.8
extconf.rb:2:in `require': no such file to load -- mkmf-gnome2 (LoadError)
 from extconf.rb:2


Gem files will remain installed in /var/lib/gems/1.8/gems/gtk-webkit-ruby-0.0.3 for inspection.
Results logged to /var/lib/gems/1.8/gems/gtk-webkit-ruby-0.0.3/ext/webkit/gem_make.out

### libwebkitgtk-dev dependency error

$ sudo gem install gtk-webkit-ruby
Building native extensions.  This could take a while...
ERROR:  Error installing gtk-webkit-ruby:
 ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.8 extconf.rb
checking for GCC... yes
checking for rb_define_alloc_func() in ruby.h... yes
checking for rb_block_proc() in ruby.h... yes
checking for new allocation framework... yes
checking for attribute assignment... yes
checking for gtk+-2.0... yes
checking for webkit-1.0... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
 --with-opt-dir
 --without-opt-dir
 --with-opt-include
 --without-opt-include=${opt-dir}/include
 --with-opt-lib
 --without-opt-lib=${opt-dir}/lib
 --with-make-prog
 --without-make-prog
 --srcdir=.
 --curdir
 --ruby=/usr/bin/ruby1.8
 --with-pkg-config
 --without-pkg-config
 --with-override-variables
 --without-override-variables


Gem files will remain installed in /var/lib/gems/1.8/gems/gtk-webkit-ruby-0.0.3 for inspection.
Results logged to /var/lib/gems/1.8/gems/gtk-webkit-ruby-0.0.3/ext/webkit/gem_make.out

References

gtk-webkit-ruby | RubyGems.org

Monday, August 20, 2012

[HOW TO] Install Shoes (Ruby GUI Toolkit) in Xubuntu

Shoes is a cross-platform toolkit for writing graphical apps easily and artfully using Ruby. Unlike most other GUI toolkits, Shoes is designed to be easy and straightforward without losing power.
Shoes (Ruby GUI Toolkit) running on Xubuntu 12.04
Figure 1. Shoes (Ruby GUI Toolkit) running on Xubuntu 12.04

Installing Shoes on Xubuntu (or any other Ubuntu distribution) is not as straight forward as it should be. There's a Shoes package in the Ubuntu repositories, however, it's buggy and using it will result in the following error:

no such file to load -- rubygems

From my experience building Shoes might result in errors during the process.

The easiest and simplest way to install Shoes in Xubuntu were to download Shoes 2 (0.r1134), codenamed “Raisins” and use it to run the Shoes programs. It's a older version, but I'm just getting started with Shoes and for now it'll suffice.

Install

Open a terminal window and run:

sudo wget -O /usr/local/bin/shoes http://goo.gl/3cigS && sudo chmod +x /usr/local/bin/shoes

Usage

Shoes can be accessed typing the following command: shoes [SHOES_PROGRAM].

Running Shoes without arguments will run the interface shown on Figure 1. Then you use the "Open an App" option to run a Shoes program.

If you supply the Shoes program path to the shoes command, the program will be launched right away.

Example

Wanna take it for a spin? Run the following instruction to see it in action:

echo -e 'Shoes.app :width => 300, :height => 200 do\n  button("Click me!") { alert("Xubuntu Geek.") }\nend' > ~/hello_shoes.rb && shoes ~/hello_shoes.rb

This instruction will create a "hello_shoes.rb" file with the content shown below this paragraph. Then it'll the Shoes program. The result is shown on Figure 2.

Shoes.app :width => 300, :height => 200 do
  button("Click me!") { alert("Xubuntu Geek.") }
end
Hello World Shoes application running on Xubuntu
Figure 2. Hello World Shoes application running on Xubuntu

References

shoes gui - Ubuntu Forums
Shoooes!

Friday, August 10, 2012

[HOW TO] Install Ruby in Xubuntu

Ruby is an interpreted, dynamic, open source scripting language for easy object-oriented programming. It a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.

In this article will learn how to install the Ruby interpreter and RubyGems in Xubuntu. Then we'll take a quick look at how to run a Ruby program and how to install a gem package.

Install

Open a terminal window and run:

sudo apt-get install ruby rubygems -y

Run a Ruby script

ruby script.rb

Run the interactive interpreter

The irb is a tool to execute interactively ruby expressions read from stdin. Run the command irb and type the Ruby command. Examples:

$ irb
irb(main):001:0> 2**20
=> 1048576
irb(main):002:0> name = gets
XubuntuGeek
=> "XubuntuGeek\n"
irb(main):003:0> name.chop!
=> "XubuntuGeek"
irb(main):004:0> puts "Hello #{name}" + "!" * 3
Hello XubuntuGeek!!!
=> nil

List remote installable gems

gem query --remote | less

Install remote gem

gem install --remote GEM_NAME # replace GEM_NAME with proper name

Resources

References

Ruby Programming Language