CSC/ECE 517 Spring 2015/ch1a 8 AT

From Expertiza_Wiki
(Redirected from User:Atmughra)
Jump to navigation Jump to search

New Keywords Arguments

Introduction

Keyword arguments is feature that lets you specify by name, the parameter that you are going to pass to a method or a function call. Although it considered very powerful and expressive ,the ruby team didn't include it in their tool-chain until ruby 2.0.0 was introduced. One might think why you need such syntactic sugar while clearly you have alternatives like providing parameter with default values.

In the example provided below, passing default parameter in Ruby can be illustrated as the following:

def foo( arg1='Hello', arg2='world', arg3='!' )
	
	puts "#{arg1} #{arg2} #{arg3}" 

end  

foo
foo( 'Trying out' ) 
foo( 'Testing', 'this great' )

One drawback of such approach is that there is no out of order passing for the parameters to the function nor we can omit any of them. Another example of how tedious using the conventional approach, is programming using API libraries<ref>http://dev.af83.com/2013/02/18/ruby-2-0-keyword-arguments.html</ref> that require to pass a lot of arguments, while not needing most of the arguments that we passed just in order to make it work.

Keyword arguments comes into play in a way, that it lets you specify the arguments without being concerned with their order, or number. you only need to pass the arguments that you need. On the other hand the method still maintains it's complexity without trimming its argument list.


Using Keyword Argument for Ruby in Old Versions

Previously, Ruby (< 2.0.0) provided this mechanism using hashes<ref>http://ruby.about.com/od/advancedruby/qt/opthash.htm</ref>. While hashes emulate this feature it far from perfect and it has it's own issues and problems lets consider this example.

def foo(args={})

	defaults={:arg1=>'It',:arg2=>'Really',:arg3 =>'Works!'}.merge(args)

	puts "#{defaults[:arg1]} #{defaults[:arg2]} #{defaults[:arg3]}" 

end 

foo
foo( :arg2 => 'AAA!' )

From the example shown above we can deduce the following, the technique of merging hashes is used as a way to pass the parameters you want with the default parameters. Lets go back to our example and look at the values of defaults hash before passing any parameters to it.

defaults={:arg1=>'It',:arg2=>'Really',:arg3 =>'Works!'

but after the merger of the hash the default parameter list becomes as illustrated below, after our “foo( :arg2 => 'AAA!' )” function call.

defaults={:arg1=>'It',:arg2=>'AAA!',:arg3 =>'Works!}

Hence the merge function replaces the key with new value obtaining a new parameters hash list.

Although this method is considered a good alternative to the real keyword argument approach it creates an over head in terms of using the merge function and adding an extra default parameter list, consequently affecting performance.


Using Keyword Argument for Ruby at Newer Versions

Once Ruby 2.0.0 was released it introduced the formal way of using keyword arguments, without the need to using previous hashing methods.

Using our previously illustrated example<ref>http://blog.marc-andre.ca/2013/02/23/ruby-2-by-example/</ref> it becomes as the following.

def foo(arg1:'It',arg2:'Really',arg3:'Works!')

	puts "#{arg1} #{arg2} #{arg3}" 

end 

foo
foo(arg2:'AAA!' )

The example shows how different the new approach is from the hashing approach now you don't need to specify a parameter hash table, also you get an argument check for the valid parameters you can pass. For example:

#hashing approach( foo function is called with invalid argument)

foo( :arg4 => 'invalid!' )

#=> nothing happens
#proper approach (Ruby 2.0.0

foo( arg4:'invalid!' )

#=>ArgumentError: unknown keyword: arg5

Furthermore using this approach is more convenient in many ways as it produces lesser code, thus affecting performance.

Using this feature in Ruby (>= 2.0.0) some needs to take the following into consideration<ref>http://dev.af83.com/2013/02/18/ruby-2-0-keyword-arguments.html</ref>.

foo(arg1:'It',arg2:'Really',arg3:'Works!', argument )

The previous call is considered illegal. With an only exception which are block arguments<ref>http://ruby.about.com/od/beginningruby/a/blocks.htm</ref> as those are located at the end of the argument list.

  • Furthermore, keyword arguments behavior is different form default arguments. You have to speciy the name of the argument when calling a method, for example.
Def foo(a, b=10)
end

#calling:
foo(1,2)

def foo(a, b:10) end #calling: foo(1,b:2)

conclusion

Eventually keyword arguments have their own trade-offs, on the positive side they provide an easer way for the developer to call on methods. Furthermore, providing better clarity and maintainability of the code. But on the other hand using position arguments provide more expressiveness for presenting methods. Looking at the function with all it arguments can provide explanatory means to understand what it does.


references

<references/>