Object#try is a solution that I like. However, it uses Object#respond_to?
Ruby Community uses this solution recently in github.
class Object
##
# @person ? @person.name : nil
# vs
# @person.try(:name)
def try(method)
send method if respond_to? method
end
end
But it does not solve the problem of args and blocks.
The right solution for generic try method is :
#By Jagan Reddy
class Object
def try(method, *args, &block)
send(method, *args, &block) if respond_to?(method, true)
end
end
You want Ruby? We can do it! Hire us for Ruby on Rails, CoffeeScript, BackBone.js, MongoDb, _underscore.js,dojo,Extjs,jQuery,Node.js,LAMP, AJAX info {at} rubyassist {dot} com
Wednesday, December 17, 2008
difference between lambda and Proc.new (Closures)
The #lambda is not the same as Proc#new. It’s ok to say the same, but not exactly.
1: First Difference
$ irb irb(main):001:0>
Proc.new{|x,y| }.call 1,2,3 =>
nil
irb(main):002:0>
lambda {|x,y| }.call 1,2,3
ArgumentError: wrong number of arguments (3 for 2) from (irb):2 from (irb):2:in `call' from (irb):2 from :0
irb(main):003:0>
2: Second Difference
def test_ret_procnew
ret = Proc.new { return 'Returned' }
ret.call “This is not reached”
end
# prints 'Returned'
puts test_ret_procnew
While return from lambda acts more conventionally, returning to its caller:
def test_ret_lambda
ret = lambda { return “Returned” }
ret.call “This is printed”
end
# prints “This is printed”
puts test_ret_lambda
1: First Difference
$ irb irb(main):001:0>
Proc.new{|x,y| }.call 1,2,3 =>
nil
irb(main):002:0>
lambda {|x,y| }.call 1,2,3
ArgumentError: wrong number of arguments (3 for 2) from (irb):2 from (irb):2:in `call' from (irb):2 from :0
irb(main):003:0>
2: Second Difference
def test_ret_procnew
ret = Proc.new { return 'Returned' }
ret.call “This is not reached”
end
# prints 'Returned'
puts test_ret_procnew
While return from lambda acts more conventionally, returning to its caller:
def test_ret_lambda
ret = lambda { return “Returned” }
ret.call “This is printed”
end
# prints “This is printed”
puts test_ret_lambda
Friday, December 12, 2008
Sass & Haml AutoCompile option
If you are using Sass & Haml, then you may wanted to compile files for each request =>
Just add the following small chunk of code to your
Just add the following small chunk of code to your
development.rb
file.
Sass::Plugin.options[:always_update] = true
Wednesday, December 10, 2008
Ruby language features
When started learning ruby in 2004, api helped me to understand the features.
Though I used most of rugular expressions and classes, rails gave me enough experience to make use of
many ruby unique features like jayson expalined in the following article.
http://www.rubytips.org/2008/04/07/10-unique-ruby-language-features/
Also look athe Features of Ruby by Michael Neumann
http://www.ntecs.de/old-hp/s-direktnet/ruby_en.html
Though I used most of rugular expressions and classes, rails gave me enough experience to make use of
many ruby unique features like jayson expalined in the following article.
http://www.rubytips.org/2008/04/07/10-unique-ruby-language-features/
Also look athe Features of Ruby by Michael Neumann
http://www.ntecs.de/old-hp/s-direktnet/ruby_en.html
Friday, December 05, 2008
Ruby on Rails Rake Tutorial
In Rails developement, we will be using running "rake" to run your tests or maybe you've used "rake db:migrate" to run your migrations. Many of us know about Make scripts. What if you want to run ruby scrupts inside make then we need rake for that. Did you realize that you can write your own tasks or create your own library of useful Rake files?
Greg of rails envy gave nice article on Rails Rake ..:-)
http://www.railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial
Greg of rails envy gave nice article on Rails Rake ..:-)
http://www.railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial
Subscribe to:
Posts (Atom)