Showing posts with label meta-programming. Show all posts
Showing posts with label meta-programming. Show all posts

Thursday, December 22, 2011

Dynamic methods inside ruby classes

by sandipransing 0 comments
Ruby language is dynamic and robust. We can define methods inside ruby classes at runtime.
# bash class A define_method :a do puts "hello" end define_method :greeting do |message| puts message end end A.new.a #=> hello A.new.greeting 'Ram ram' #=> Ram ram
Can you imagine using dynamic methods below 24 lines of code is optimized to just 8 lines
To know more on below code read # Earlier code class ApplicationController < ActionController::Base protect_from_forgery helper_method :current_staff, :current_employee, current_admin def authenticate_staff!(opts={}) current_staff || not_authorized end def current_staff current_user if current_user.is_a? Staff end def authenticate_employee!(opts={}) current_employee || not_authorized end def current_employee current_user if current_user.is_a? Employee end def authenticate_admin!(opts={}) current_admin || not_authorized end def current_admin current_user if current_user.is_a? Admin end end
# New Version using dynamic methods %w(Staff Employee Admin).each do |k| define_method "current_#{k.underscore}" do current_user if current_user.is_a?(k.constantize) end define_method "authenticate_#{k.underscore}!" do |opts={}| send("current_#{k.underscore}") || not_authorized end end
Read More…

Wednesday, March 31, 2010

alias methods in ruby

by sandipransing 0 comments
Alias method in ruby
Ruby classes provides a alias_method that can be used to reuse the existing methods.
Consider a situation where you need different methods which has same code and ONLY they have different names.

In this case alias_method uses suits best choice instead duplicating same code or writing common method that will get used in all methods, as i did before. Example, I have methods search, home, index all are doing same functionality.
Old approach # URL / def index list end # URL /search def search list end #URL /home def home list end private def list # code here end Correct approach in ruby def index # code here end alias_method :home, :index alias_method :search, :index Attributes aliasing in ruby
Same way one can easily rename existing class attribute names using alias_attribute method.
alias_attribute(new_name, old_name) alias_attrinute :username, :login More practical use
while deprecating attributes, methods in gems, plugins, extensions, libraries always use aliases in order to maintain backward compatibility.
Got easy ??
that's where ruby rocks !
Read More…

Tuesday, February 16, 2010

Dynamic creation of variables in ruby

by sandipransing 0 comments
1. To create local variables in ruby dynamically.
Use eval method in ruby

ree-1.8.7-2010.01 > eval("local=4")
 => 4 
ree-1.8.7-2010.01 > p local
4
ree-1.8.7-2010.01 > eval("local_#{1}=4")
 => 4 
ree-1.8.7-2010.01 > puts local_1
4

2. To create & get instance variables dynamically in ruby.
instance_varaiable_set & instance_varaiable_get methods are provided and no need to do it using eval.

ree-1.8.7-2010.01 > (0..3).each do |i|
ree-1.8.7-2010.01 >     instance_variable_set("@instance_#{i}", i*i+1)
ree-1.8.7-2010.01 ?>  end
 => 0..3 
ree-1.8.7-2010.01 > @instance_0
 => 1 
ree-1.8.7-2010.01 > @instance_2
 => 5 

3. Dynamic constant variable creation in ruby

ree-1.8.7-2010.01 > class Example
ree-1.8.7-2010.01 ?>  end
 => nil 
ree-1.8.7-2010.01 > Example.const_set('A',200)
 => 200 
ree-1.8.7-2010.01 > Example::A
 => 200 

Read More…

Eval method in ruby

by sandipransing 0 comments
Eval method in ruby executes string/expression passed as parameter.
Example:
irb > eval("5+3") => 8 irb > eval("a=5") => 5 irb > eval("b||=a") => 5
Its part of ruby meta-programming and not recommended approach unless there is no any alternative to do.
Read More…

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter