You can use alias:
#This is your current method:
def foo(i, css_class = ‘’, html_id = ‘’, css_style = ‘’)
puts “i = #{i.inspect}”
puts “css_class = #{css_class.inspect}”
puts “html_id = #{html_id.inspect}”
puts “css_style = #{css_style.inspect}”
end
alias old_foo foo #Alias your current method
#Now do some preparations:
hash = Hash.new do |this_hash, non_existent_key|
non_existent_key.to_s
end
ALTERNATE_PARAM_NAMES = hash.merge({
:class => ‘css_class’,
:id => ‘html_id’,
:style => ‘css_style’,
})
PARAM_ORDER = [‘css_class’, ‘html_id’, ‘css_style’]
#Redefine your method:
def foo(text, hash)
hash_with_normalized_key_names = {}
hash.each do |key, val|
normalized_key = ALTERNATE_PARAM_NAMES[key]
hash_with_normalized_key_names[normalized_key] = val
end
ordered_param_values = PARAM_ORDER.map do |param|
hash_with_normalized_key_names[param]
end
old_foo(text, *ordered_param_values) #Call the old method
end
#Here is a test to see if it works:
foo(‘Hello world’,
:css_style => ‘border: 3px solid green’,
:id => ‘test1’,
:css_class => ‘pentagon’,
)
–output:–
i = “Hello world”
css_class = “pentagon”
html_id = “test1”
css_style = “border: 3px solid green”
In ruby 2.1, you can make it simpler if you make a slight change to your
definition of foo:
#Use the new style default params:
def foo(i, css_class: ‘’, html_id: ‘’, css_style: ‘’)
puts “i = #{i.inspect}”
puts “css_class = #{css_class.inspect}”
puts “html_id = #{html_id.inspect}”
puts “css_style = #{css_style.inspect}”
end
alias old_foo foo
hash = Hash.new do |this_hash, non_existent_key|
non_existent_key.to_sym #CHANGE HERE
end
ALTERNATE_PARAM_NAMES = hash.merge({
:class => :css_class,
:id => :html_id,
:style => :css_style,
})
def foo(text, hash)
hash_with_normalized_key_names = {}
hash.each do |key, val|
normalized_key = ALTERNATE_PARAM_NAMES[key]
hash_with_normalized_key_names[normalized_key] = val
end
old_foo(text, hash_with_normalized_key_names)
end
foo(‘Hello world’,
‘css_style’ => ‘border: 3px solid green’, #Can use string keys
:id => ‘test1’,
:css_class => ‘pentagon’,
)