Skip to content

Instantly share code, notes, and snippets.

@luckydev
luckydev / noname.rb
Created April 9, 2011 17:16
One Step Further with Blocks!
Person = Class.new do
#instance method
def say_hi
puts "Hi, Do you like Ruby?"
end
end
Person.new.say_hi
@luckydev
luckydev / super.rb
Created April 10, 2011 06:57
to demonstrate what super keyword does in ruby
module A
def say_hi(a,b,c)
puts "Hi, from [A] module. Numbers - #{a} #{b} #{c}"
end
end
class B
include A
def say_hi(a,b,c)
@luckydev
luckydev / super.rb
Created April 10, 2011 06:59
explicitly putting parenthesis to forward arguments
module A
def say_hi(a,b,c)
puts "Hi, from [A] module. Numbers - #{a} #{b} #{c}"
end
end
class B
include A
def say_hi(a,b,c)
@luckydev
luckydev / super.rb
Created April 10, 2011 07:00
explicitly forward arguments
module A
def say_hi(a,b,c)
puts "Hi, from [A] module. Numbers - #{a} #{b} #{c}"
end
end
class B
include A
def say_hi(a,b,c)
@luckydev
luckydev / super.rb
Created April 10, 2011 07:03
same as calling just super
module A
def say_hi(a,b,c)
puts "Hi, from [A] module. Numbers - #{a} #{b} #{c}"
end
end
class B
include A
def say_hi(a,b,c)
@luckydev
luckydev / obmodule.rb
Created April 14, 2011 03:37
Objects gets more power over time.
class Person
def say_hi
puts "Hi, this is Ruby!"
end
end
john = Person.new
robin = Person.new
john.say_hi # => Hi, this is Ruby!
@luckydev
luckydev / MyClass.rb
Created April 15, 2011 16:33
for an object -- singleton class
class MyClass
def my_instance_method
puts "this is an instance method"
end
#self here is MyClass
class << self
def count_objects
"counting objects..DONE"
$ bundle install
Fetching source index for http://rubygems.org/
Installing rake (0.9.2)
Installing abstract (1.0.0)
Installing activesupport (3.0.5)
Installing builder (2.1.2)
Installing i18n (0.6.0)
Installing activemodel (3.0.5)
Installing erubis (2.6.6)
Installing rack (1.2.3)
@luckydev
luckydev / output
Created July 18, 2011 06:07
ActiveRecord Finder
ruby-1.9.2-p180 :001 > Account.find(34)
ActiveRecord::RecordNotFound: Couldn't find Account with ID=34
from /Users/lakshman/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/relation/finder_methods.rb:296:in `find_one'
from /Users/lakshman/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-
ruby-1.9.2-p180 :002 > Account.where(:id => 34).first
=> nil
ruby-1.9.2-p180 :003 > Account.where(:id => 34)
=> []
@luckydev
luckydev / a_lambda.rb
Created August 14, 2011 05:38
Proc and Lambda: Difference 1 - Return policy
def my_awesome_method
ret = lambda { return "This is returned from Lambda" }
ret.call
"This line will run"
end
puts my_awesome_method
# => "This is returned from Lambda"
# => "This line will run"