Lessons learned from true, false and nil objects in ruby

Every expression in Ruby evaluates to an object and every object has a boolean value. The boolean values are always the same instance of the TrueClass and FalseClass classes and will therefore always have the same id. Most objects in Ruby will have a boolean value of true. Only two objects have a boolean value of false, these are the false object itself and the nil object.


2.1.0 :027 > true.class
 => TrueClass
2.1.0 :028 > false.class
 => FalseClass


2.1.0 :029 > p = TrueClass.new
NoMethodError: undefined method `new' for TrueClass:Class 
  from (irb):29 
  from /Users/gakshay/.rvm/rubies/ruby-2.1.0/bin/irb:11:in `<main>'
2.1.0 :030 > f = FlaseClass.new
NameError: uninitialized constant FlaseClass 
  from (irb):30 
  from /Users/gakshay/.rvm/rubies/ruby-2.1.0/bin/irb:11:in `<main>'


2.1.0 :052 > true.object_id
 => 20
2.1.0 :053 > false.object_id
 => 0


2.1.0 :031 > if :expresssion
2.1.0 :032?> puts "yeah, it's an object"
2.1.0 :033?> end
yeah, it's an object
 => nil


2.1.0 :034 > if 11
2.1.0 :035?> puts "Yeah, the boolean value of an integer object is true"
2.1.0 :036?> end
Yeah, the boolean value of an integer object is true
 => nil


2.1.0 :037 > if !false
2.1.0 :038?> puts "Yeah, boolean value of false has to be false"
2.1.0 :039?> end
Yeah, boolean value of false has to be false
 => nil


2.1.0 :040 > if nil
2.1.0 :041?> puts "Damn, nil can't be true"
2.1.0 :042?> else
2.1.0 :043 > puts "yeah, boolean value of nil object is false"
2.1.0 :044?> end
yeah, boolean value of nil object is false
 => nil
2.1.0 :045 > nil.class
 => NilClass
2.1.0 :046 > nil.object_id
 => 8


2.1.0 :047 > if def i_am_a_method; end
2.1.0 :048?> puts "can it be true"
2.1.0 :049?> else
2.1.0 :050 > puts "a method definition evaluates to FALSE"
2.1.0 :051?> end
can it be true


To re-iterate :

Every expression in Ruby evaluates to an object and every object has a boolean value. The boolean values are always the same instance of the TrueClass and FalseClass classes and will therefore always have the same id

Calling expire_fragment within rails console

Today i want to expire a fragment cache in haml view:

-cache('taxonomy') do

So I enter in rails c production, but I found Rails.cache.exists?(‘taxonomy’) returned false.

expire_fragment doesn’t exist in ActionController::Caching::Sweeper. It can be found in ActionController::Base. The sweeper class uses method_missing to call the method from a controller instance.

ActionController::Base.new.expire_fragment(‘taxonomy’, options = nil) 

That’s why everything was fine when I used to expire through controllers and nothing happened when everything was called from an observer class or so, I just needed a controller instance and it worked.

def expire_cache_for(deal)
  @controller ||= ActionController::Base.new 

Our life around REPL – Read–Eval–Print Loop

Whenever I see my niece so comfortable with iPhone, Mac, Advance Toys; I usually wonder almost similar feeling our parents must have pondered about us. But what is the biggest disadvantage of being part of GEN X. What? We have best things in the world but the irony is that we don’t enjoy or embrace the inventions/creations done by our forefathers generation. For us TV, Radio, Telephone, Mobile, Desktop was little obvious, now for next generation iPhone, iPad, Smartphone, DSLR, Laptops, Tablets et al are very obvious. Its clearly visible our parents struggle with our generation widgets and I have now started to struggle with next-generation gadgets.

You must be thinking what is it had to do with REPL. During college days, I started coding in C, bit of Java (to clear job interviews incase required), then majorly on PHP, RUBY, PYTHON, JAVASCRIPT, BASH. The moment I started coding on these languages, which are derived from their forefathers C & Lisp, I sensed a sheer bliss while working on them and started promoting them in peer group as soon as I realized the language is always available and it was obvious not to look back at C, C++, Java and many others. We can easily compare it to Gramophone, cassettes, VCR , VCP, Landline but these inventions were the foundation of where we stand today in our life or in programming world.

I was not clear about the term REPL when I started working on these languages and thought of it as a jargon of programming world. Recently, wrote a smallest code on ruby and a REPL was created. This is when I realized the power of REPL and thought to read about their forefathers. Let’s first have a sneak-peek into that smallest piece of ruby repl code.REPL

#!/usr/bin/env ruby
loop {p eval gets }


Now read this piece of code in reverse order, READ (gets), EVAL, PRINT (p), LOOP. Save this file as repl.rb and run as`ruby repl.rb`

─$ ruby repl.rb 1 ↵
"akshay Gupta".upcase

Congratulations, now you understand REPL and underlying fact of  language is always available i.e. no real distinction between read-time, compile-time and run-time. Now, when I start working on any new language, it’s easier to pick up if it’s REPL based and I truly thank Mr. John McCarthy father of Artificial Intelligence, creator of LISP (where lies the roots of REPL) for his immense contribution in the field of computer science, AI, programming languages and so many programming paradigm. If i’m not wrong, he is also the inventor of if-then-else conditional construct and we take these for granted now.

Running code at read-time lets users reprogram; running code at compile-time is the basis of macros; compiling at runtime is the basis of Lisp’s use as an extension language in programs like Emacs; and reading at runtime enables programs to communicate using expressions.

There have been two really clean, consistent models of programming so far: the C model and the Lisp model. As computers have grown more powerful, the new languages being developed have been moving steadily toward the Lisp model. A popular recipe for new programming languages in the past 20 years has been to take the C model of computing and add some parts taken from the Lisp model, like runtime typing and garbage collection.


It’s equally important to look forward and innovate in coming time and respect, embrace the efforts of our remarkable forefathers. To read and practice different languages that support REPL please follow the below links. I primarily depend heavily upon irb and PRY.

In our everyday life we do nothing more than READ (consume knowledge), EVAL ( between right and wrong), PRINT (discuss around), LOOP (day everyday) i.e. REPL 🙂


Rails: Calling Stored Procedure and read OUT variables in Sequel

Very recently stumbled upon a situation, where I need to call a Oracle DB Stored Procedure and also need to read the OUT variables in one of our Ruby on Rails application or in simple words read the output after calling stored procedure in one of our Rails application.

To add to our dismay, it’s not ActiveRecord connection but we were using Sequel library. Sequel do support the Oracle library and it’s very silimar to AR ORM and thanks to this wonderful gem that works out-of-the-box. But it’s not straight forward to call Stored PROC and read the OUT variables and there is next to no documentation to process OUT variables from our stored PROC

DB specific SQL can be executed using “execute” method and almost all the DB adapters provide this method. But question remains the same how to read the OUT variables and I have never called a Stored Procedure ever in my life. It seems an interesting problem to solve and answer may be hidden somewhere in the code but no documentation.

Let’s try to read the OUT variables in different ways taking one step at a time:

1. Create a sample procedure which takes one integer and one string as input and returns integer multiplied by 2 and UPPERCASE of string

create or replace
, name IN VARCHAR2 
, fathers_age OUT NUMBER 
, capital_name OUT VARCHAR2 
) AS 
 fathers_age := age*2;
 capital_name := UPPER(name);


2. How to call using SQL and read OUT variables (in SQLDeveloper)

 var age number
 var naam varchar2
 var papa_age number
 var bada_naam varchar2
 exec :age := 20
 exec :naam := 'akshay'
 exec MYPROC (age => :age, name => :naam, fathers_age => :papa_age, capital_name => :bada_naam)
 print papa_age
 print bada_naam




3. How to call it using ActiveRecord using Ruby OCI8 GEM

cursor = ActiveRecord::Base.connection.raw_connection.parse("BEGIN MYPROC(:age, :name, :fathers_age, :capital_name); END;")
cursor.bind_param(:name, 'akshay')
cursor.bind_param(:age, '20')
cursor.bind_param(:fathers_age, nil, Fixnum)
cursor.bind_param(:capital_name, nil, String, 20)
p cursor[:fathers_age], cursor[:capital_name]


1.8.7 :125 > cursor.exec()
 => 1
1.8.7 :126 > p cursor[:fathers_age], cursor[:capital_name]


4. How to call it using Sequel Database Toolkit.

require 'sequel'
db = Sequel.connect(:adapter=>'oracle', :host=>'localhost', :database=>'db_name', :user=>'test', :password=>'test')
db.execute("begin MYPROC(:age, :name, :fathers_age, :capital_name); end;", {:arguments => [[20, "integer"], ["akshay", "string"], [nil, "integer"], [nil, "string"]]}) { |cursor| [cursor[3], cursor[4]] }


1.8.7 :158 > db.execute("begin MYPROC(:age, :name, :fathers_age, :capital_name); end;", {:arguments => [[20, "integer"], ["akshay", "string"], [nil, "integer"], [nil, "string"]]}) { |cursor| [cursor[3], cursor[4]] }
 => [40, "AKSHAY"]


This is important to note, oracle adapter inside sequel uses the Ruby OCI8 gem but the implementation is not straight forward as in the case of ActiveRecord.

To further understand how the variables are bound and how are we reading out the OUT variables, I recommend to read the code written inside the ORACLE adapter of SEQUEL gem.

Hope this bit of information is useful and many thanks to  for such a great sequel gem. Happy Hacking 🙂



RubyMotion Runtime Guide

Object Model



The object model of RubyMotion is based on Objective-C, the underlying language runtime of the iOS and OS X SDK. Objective-C is an object-oriented flavor of C that has been, like Ruby, heavily influenced by the Smalltalk language.

Sharing a common ancestor, the object models of Ruby and Objective-C are conceptually similar. For instance, both languages have the notion of open classes, single inheritance and single dynamic message dispatch.

In RubyMotion, Ruby classes, methods and objects are Objective-C classes, methods and objects respectively. The converse is also true. Objective-C classes, methods and objects are available in Ruby as if they were native.RubyMotion implements the Ruby object model by using the Objective-C runtime, the library that powers the Objective-C language and indirectly, the iOS SDK APIs.

By sharing the same object model infrastructure, Objective-C and RubyMotion APIs can be interchangeable at no additional performance expense.

via RubyMotion Runtime Guide.