CSC/ECE 517 Spring 2015/ch1a 8 AT: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
m (moved User:Atmughra to CSC/ECE 517 Spring 2015/ch1a 8 AT: Conform to submission requirements)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Introduction ==
 
== '''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.  
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.  
Line 18: Line 22:


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.   
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 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.  
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.
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 ==
== '''Using Keyword Argument for Ruby in Old Versions''' ==


Previously, Ruby (< 2.0.0) provided this mechanism using hashes. While hashes emulate this feature it far from perfect and it has it's own issues and problems lets consider this example.  
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.  


  <nowiki>
  <nowiki>
Line 54: Line 58:




== Using Keyword Argument for Ruby at Newer Versions ==
== '''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.
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 it becomes as the following.
Using our previously illustrated example<ref>http://blog.marc-andre.ca/2013/02/23/ruby-2-by-example/</ref> it becomes as the following.
 


  <nowiki>
  <nowiki>
Line 89: Line 92:
Furthermore using this approach is more convenient in many ways as it produces lesser code, thus affecting performance.
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.
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>.


*All keyword arguments must be located at the end of the argument list, for example:
*All keyword arguments must be located at the end of the argument list<ref>http://chriszetter.com/blog/2012/11/02/keyword-arguments-in-ruby-2-dot-0/</ref>, for example:
  <nowiki>
  <nowiki>
foo(arg1:'It',arg2:'Really',arg3:'Works!', argument )</nowiki>
foo(arg1:'It',arg2:'Really',arg3:'Works!', argument )</nowiki>
is considered illegal. With an only exception which are block arguments[2] as those are located at the end of the argument list.
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.
*Furthermore, keyword arguments behavior is different form default arguments. You have to speciy the name of the argument when calling a method, for example.
Line 111: Line 114:
foo(1,b:2)</nowiki>
foo(1,b:2)</nowiki>


== conclusion ==
== '''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.  
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.  


[1] http://brainspec.com/blog/2012/10/08/keyword-arguments-ruby-2-0/
 
[2] http://dev.af83.com/2013/02/18/ruby-2-0-keyword-arguments.html
 
[3] http://blog.marc-andre.ca/2013/02/23/ruby-2-by-example/
== '''references''' ==
[4] http://chriszetter.com/blog/2012/11/02/keyword-arguments-in-ruby-2-dot-0/
<references/>
[5] http://ruby.about.com/od/advancedruby/qt/opthash.htm
[6] http://ruby.about.com/od/beginningruby/a/blocks.htm

Latest revision as of 21:49, 9 February 2015

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/>