Installing GOSU: Games in Ruby

Gosu is an open source game development library. Ruby and C++ programming languages can be used to write games using the Gosu library. The Passenger guys presented the RubyStein game written in Ruby. It’s an interesting game and has got many famous personalities form Rails community including DHH, ZedShaw and some as well brands like Peepcode.

If you are on Ubuntu, you can try these steps to enjoy this game.

First, install the gosu and dependent libraries…

sudo apt-get install libsdl-mixer1.2
sudo apt-get install libboost-dev
sudo apt-get install libpango1.0-dev
sudo apt-get install libgl1-mesa-dev
sudo gem install gosu

Now, you can download the game and run it as

 ruby wolf3d.rb

and play it using the arrow keys and space bar.

Filed in: ruby

by: Sur

No Comments

Ruby FunDay — First ever full day Ruby Code Burning Station in Delhi, India

Since last one year, there is quite a bit of Ruby and RubyOnRails development happening in India. Still the biggest thing missing here is the official Ruby and Rails meet-ups like RailsConf and RubyConf. Being in Ruby and Rails community since last three years I’ve been attending many events in India but couldn’t find any of them Ruby centric. And in one of the past event I and Manik decided to have an event focusing fully on Ruby and RubyOnRails.

And here we come. We are organizing the first RubyFunDay. Its going to be one of its kind of event in India. There would be sessions on some of the really hot topics…

  • Flex + Rails
  • Internationalization in Rails
  • FxRuby+AR
  • Facebook APP + Rails


If you like to have Fun with Ruby, you are most welcome to attend the event. To attend the event, you would need to register online for the event. Its going to happen in a very decent venue, and we are hearty grateful to Impetus Technology for sponsoring the venue. I am sure you would enjoy a lot in the event.


More good things in the event apart from Ruby and Rails includes the printed Ruby Ts ;) … cool yeah ?

RubyFunDay is going to be a pure Code Burning Station. Every session would be full of live code demonstrations.
If you would like to present a session at this event, register online for it and send me an email with your topic’s details on [surDOTmaxATgmailDOTcom]. The only constraint we have is, it should not be mere presentation, you will have to show the live code in the session.

For registration and more details on the event please visit here

I would really appreciate if you blog/tweet about the event and help spreading a word about it. If you do, please add the tag/category to the post as “rubyfunday”.

If you would be there, and get time, I would love to speak to you. You can recognize me easily, its very simple, I wear Jeans and T-shirts mostly ;)

See you there with more and more Ruby.

Filed in: event, ruby, rubyfunday, rubyonrails

by: Sur

No Comments

New in Ruby 1.9: Threads

The ruby is getting changed and improved a lot with every release. No doubt the existing demerits of rails are soon going to be history.

Currently for my rails applications I am running Ruby1.8… and exploring 1.9 to get ready as we are heading with nice speed in the rails developments. Modrails has already started making waves all around.
Soon I would be publishing a couple of posts on threads, processes and sockets in ruby… here is a bit on how they are going to be different in Ruby1.9.

In Ruby1.9 the threads implementation is going to be really different than what we all know/do in Ruby1.8. The Ruby1.8 uses a single native thread and runs all Ruby threads within that… which certainly protects the Ruby threads to take on much responsibilities and to completely leverage the hardware with multiple processors.

Now, the good news, the Ruby1.9 allocates a separate native thread for every Ruby thread. But at this very moment it is not fully functional… as because of some C libraries, used here, are not thread-safe that it will not allow Ruby1.9 to take control on multiple native threads. Still, great signs of improvements in these directions… It has been mentioned that this restriction will be removed in the upcoming versions of Ruby1.9 and there will be full fledged implementation of multithreading in Ruby with multiple native threads, and the charm of parallel execution… means a lot more scope of improvements for all related libraries, frameworks built on the top of Ruby.

Filed in: ruby, ruby1.9, threads

by: sur

No Comments

New in Ruby 1.9: Block Scoping

Ruby 1.9 provides the block scoping of the local variables. It shadows the outside local variable in the block and allocates a new object_id in the memory to the block variable, which essentially means much more flexibility in using the lambdas without worrying about the name of the block variable. Remember that instance, class and global variables are still the same and can be used outside and inside the block as usual.

ruby <= 1.8

a = 9
p a # => 9
[1, 2, 3].each{|a|}
p a # => 3

ruby 1.9 (shadows the local variable a)

a = 9
p a # => 9
[1, 2, 3].each{|a|}
p a # => 9

Filed in: ruby, ruby1.9

by: sur

No Comments

Ruby interface for OpenSSL

If you have installed ruby using apt-get or if you have not mentioned the configuration option for openssl while installing from source, you can expect openssl will not work with ruby.

Here is the solution for the same…

Step 1

Install gcc and make(if you don’t have already on your box)

  sudo apt-get install gcc make

Step 2

Install the system level OpenSSL and dev libraries

  sudo apt-get install openssl libssl-dev

Step 3

Install the ruby’s openssl interface to the system’s openssl

switch the directory to the ruby’s source code’s openssl

  cd /usr/local/src/ruby1.8.6/ext/openssl

install the ruby’s openssl interface

  sudo ruby extconf.rb
  make
  make install

try requiring the openssl in irb

  require 'openssl'

if returns true, installed successfully!

Filed in: openssl, ruby, ubuntu

by: sur

2 Comments

Defining function inside a function using def

The function(s) defined within a function works fine with any hierarchy only within the main object but gives NoMethodError inside a class.

Lets exemplify it!

Here are the example of both main object and inside a class…

[source:ruby]
def person
def author
p “I am author”
end
def reader
p “I am reader”
end
def publisher
def authorized
p “I am a authorized publisher”
end
end
end

person.author # => I am author
person.reader # => I am reader
person.publisher.authorized # => I am authorized publisher

class Person
def publisher
def authorized
p “I am a authorized publisher”
end
end
end

Person.new.publisher.authorized # => NoMethodError
[/source]

Its an unexpected behavior in the main object. I don’t know whether its a bug or the feature. But as Matz says it will be removed in the future version of ruby, might be in 2.0

Filed in: ruby

by: sur

1 Comment

Rails Hackfest August-07 Results


Hackfest Winners

Filed in: hackfest, rails, ror, ruby, rubyonrails

by: sur

2 Comments

Rails Hackfest August-07

I participated in August Rails Hackfest which was a great experience.
You can see the missing August 2007 from the Post Archives. I didn’t post anything last month as I was busy in submitting rails patches. I submitted a lot of patches out of which I got one patch committed to the rails code under changeset 7362 which was about optimizing the code of ActiveRecord Validation validates_presence_of filed under the ticket 9392. Me and kampers got a collaborated patch accepted under the changeset 7383 which was a tiny patch for improving documentation of Action Controller filed under the ticket 9454. Many of my patches got rejected as well by the more experienced Rails Contributors but still, many patches are there to be analyzed by the Rails Core Team.

As I got my code embedded in the Rails Core, I am proud to be recognized as Rails Core Contributor. Tarmo was one of the most active and really appreciable contributor in August Rails Hackfest. Because of this participation I paid very less attention to the other important things including the clients’ projects as well :D. Moreover in the middle of the month I needed to go out to give Rails Training to a really enthusiastic team of a Bangalore based company. It was lovely experience there, but I got out of touch from the Hackfest for those crucial 5 days. After returning back I tried to keep the pace again. I am desperately waiting for the result of the August Rails Hackfest which might get published in a day or two. The first prize is the Entry Pass to the RailsConfEurope. I am looking for submitting more patches now onwards but not with the same pace as of last month, coz I will not be participating in the Hackfest but keeping the spirit of Rails Core Contributor and the pay back policy of open source. I will also be releasing some rails plugins and probably a ruby gem as well this month.

Have you tried this..
[source:ruby]
Object === Class == Class === Object
[/source]

Filed in: hackfest, rails, ror, ruby, rubyonrails

by: sur

5 Comments

Second delhi.rb meetup - Some Advance Ruby Skills

Hey Everyone,
Vinsol is proudly taking charge to spread Rubyism in delhi and to grow the Ruby & Rails communities here in New Delhi, India. We are organizing delhi.rb meetups around once every month, the meetup is all about ruby and rails as well. The meetup was on 19th July 2007 was our second meetup, first was on 22nd June 2007.

Manik presenting SOLR
Manik presenting SOLR


Me presenting Some Advance Ruby Skills
Me presenting Some Advance Ruby Skills

More photos here.

It was really a nice experience attending the meetup, sharing the ruby/rails thoughts and upcoming features. It helps keeping yourself up-to-date with the latest trends in this technology domain at least in Ruby and Rails(what else m talking except ruby :D). So, there were two presentations in the meetup — first Manik presented Full text search implementation for Rails using SOLR(it was really an interesting presentation, i got SOLR learning for free, thanks Manik :)), second I presented Some Advance Ruby Skills which i am going to share in this post too. Though in the first meetup I presented Caching on RubyOnRails but i haven’t posted here…

Some Advance Ruby Skills

1.) Everything is object

A popular phrase about Ruby, “Everything is Object”. At the root of the ruby it is Object. Everything we define in ruby is object. Even the classes we define are actually object. A class defined with class ClassName; end is actually an object of the class Class.
The Object keeps the record of whatever class or module we define. We can justify it as
[source:ruby]
class Klass
end
Object.constants.include?(”Klass”) # => true
[/source]

2.) module_eval

Use module_eval to define instance and class methods of a class at runtime, when you are outside the class.
example 1
defining an instance method
[source:ruby]
class C
end

C.module_eval do
define_method :wish do
p “hello instance method”
end
end

c = C.new
c.wish # => hello instance method
[/source]
example 2
defining a class method
[source:ruby]
class C
end

C.module_eval do
class << self
define_method :wish do
p "hello class method"
end
end
end

C.wish # => hello class method
[/source]
example 3
another form of using module_eval
when method body is available as a String object
[source:ruby]
class D
class << self
def method_body
ret =<<-EOS
def wish
p "hello, supplied as String object"
end
EOS
end
end

class C
end

c = C.new

c.class.module_eval(D.method_body)

c.wish # => hello, supplied as String object
end
[/source]

3.) alias_method

It is NOT method call delegation but insertion of customized functionalities on a specific method call.
[source:ruby]
class C

def wish
p “hello”
end

end

c = C.new
c.wish # hello

class D

class << self
def keep_some_record
p "I am keeping some records"
end
end

end

# aliasing the wish method

c.class.module_eval do

alias_method :wish_orig, :wish

define_method :wish do
D.keep_some_record
wish_orig
end

end

c.wish # I am keeping some records; hello
[/source]

4.) The Anonymous class

I just presented same a la this post

5.) send

Calling a method when method name is stored as a string object in a variable i.e. you can not see which method to call.
example 1
when method name is simply stored as a String object
[source:ruby]
class C
def wish
p “hello DELHI.rb”
end
end
a = “wish”
c = C.new
c.send(a)
[/source]
example 2
making set method at runtime
[source:ruby]
class C
attr_accessor :name
end

c = C.new

a = “name”

c.send(a + “=”, “SUR MAX”)

p c.send(a)
[/source]
example 3
this is interesting, when attribute name itself is send
[source:ruby]
class C
attr_accessor :send
end

c = C.new

a = “send”

c.__send__(a + “=”, “SUR MAX”)

p c.__send__(a) # => Sur Max
[/source]
well, don’t say “what if attribute name is __send__” :P

6.) The Method class

Methods of the class are objects of the Method class when retrieved with the method method and can be called with the method call.
example 1
anything we define with def-end is an object of the class Method
[source:ruby]
class C
def wish
p “hello”
end
end

c = C.new

m1 = c.method(”wish”)

p m1.class # => Method

m1.call # => hello
[/source]
example 2
method can hold the object’s reference and associated instance variables
[source:ruby]
class C
attr_accessor :name

def initialize(name)
self.name = name.to_s
end

def wish
p “hello ” + name.to_s
end
end

c = C.new(”Sur Max”)

m1 = c.method(”wish”)
m1.call # => hello Sur Max
[/source]
example 3
we are able to let this method object flow throughout the application code and let it available anywhere in the code.
[source:ruby]
class C
attr_accessor :name

def initialize(name)
self.name = name.to_s
end

def wish
p “hello ” + name.to_s
end

def self.supply_wish
c = new(”Sur Max”)
return c.method(”wish”)
end

end

C.supply_wish.call # => hello Sur Max
[/source]

7.) what is “self”

I just presented a la this post

8.) Single Method Delegation - using Forwardable

Allows you to delegate named method calls to other objects.
[source:ruby]
require ‘forwardable’

class C
extend Forwardable

attr_accessor :h

def initialize
@h = {}
end

def_delegator(:@h, :[], :show)
def_delegator(:@h, :[]=, :add)

end

end

c = C.new

c.add(1, “asdf”)

p c.show(1)

p c.h
[/source]
Notice the beauty of ruby here… The methods [], []= of a hash object are usually called as
[source:ruby]
h = {}
h["key"] # this will return the corresponding value
h["key"] = “value” # this will set the “value” corresponding to the “key”
[/source]
BUT in the above delegation code we are calling them as(delegating the method call on them as)
[source:ruby]
h = {}
h.[](”key”) # this will return the corresponding value
h.[]=(”key”, “value”) # this will set the “value” corresponding to the “key”
[/source]

9.) Full class Delegation - using Delegator

Extending an object(instance of Class) with the capabilities of another.
[source:ruby]
require ‘delegate’

class Words < DelegateClass(Array)

def initialize(list = "one two three four")
super(list.split)
end

end

w = Words.new

p w # => ["one", "two", "three", "four"]

p w.length # => 4

[/source]

10.) SimpleDelegator

Write memory optimized code with SimpleDelegator…
[source:ruby]
require ‘delegate’

a = SimpleDelegator.new([10, 20])

old_id = a.__id__

b = a

a.__setobj__(”a new object”) # this is not possible otherwise with the method “replace” which can replace only object of same class on same memory location

new_id = a.__id__

p a # => “a new object”
p b # => “a new object”

p new_id == old_id # => true
[/source]

Filed in: advance_ruby, classes, delegation, delhi.rb, duck typing, metaprogramming, rails, ror, ruby, rubyonrails

by: sur

4 Comments

How to make arrow keys working in irb in Linux/Ubuntu

It was ruining all my mood and concentration when i was trying some sample ruby code in irb and again and again i had to retype the things because the arrow keys were not working there. I searched the problem on net and found that i need to have readline library on my linux and in ruby as well. Now the point is readline should be available on the linux before you compile ruby so as you can give the readline option while compiling ruby. BUT we have ruby already installed !! Now What ?

Now, first you will need to install system based readline libraries as

  sudo apt-get install libreadline5 libreadline5-dev

If you are not able to install linux based readline with the above method the refer this for more details.

Now you will need to compile the ruby based readline interface to let you readline library available in irb.
Do it by this…

  cd /usr/local/src/ruby-1.8.6/ext/readline
  sudo ruby extconf.rb
  sudo make
  sudo make install

thats it, all done!!
Try your tips on arrow keys in irb, it should work now.
enjoy!!

Filed in: feisty fawn, irb, linux, rails, ruby, ubuntu

by: sur

8 Comments