Currying: A Ruby approach

Hi, This is Shubham Mishra from India,  this is the part of Ruby on Rails exploration journey.  In this post we will discuss about the ruby's one of the underworld approach: Currying (not 🥘 this one 😅).

I thing it would be better to understand it first by diagrammatically. Let understand curry in ruby in a snap. Lets suppose we want to sum up 3 numbers [1,2,3],

Normal Ruby approach:
Normal Sum in RUby
Normal Sum in RUBY


Curried Sum:
Curried ruby approach
Curry in Ruby

Simple, isn't it!,😉😉, Ok if you are not in a hurry then you should now go through the Code level reading to understand it even better.


Practice it : Curry in Ruby, Play here
See the OutPut:
#<Method: main.sum>
Normal Sum: 6
Curried Sum 👇:
#<Proc:0x000055c305dbb0f0 (lambda)>
#<Proc:0x000055c305dbace0 (lambda)>
6

Now, understand each part:

  • The sum() method is the normal ruby code, where (*) is splat operator, which takes more than one arguments.
  • Then we have method(:sum), which is the reference for the Sum method, and by calling curry(n), we are mentioning that how many arguments are permitted till we will get the result. It means, we call the curry approach. The curry method takes a an argument (basically the number of arguments the curried approach should be given) and makes it in order that till we pass sufficient arguments to the curry itemin place of resolving the technique, it returns another proc item so we are able to maintain passing more arguments to it. 😳😳🛠🛠
  • Then for 'n' number of values in the each loop, we are calling call(x), with the current value, it will return a new Proc if not reached to the last argument, once reached last argument it will return the result.
  • That's it my dear, I know its not easy to grab it at first read hence I would suggest you to copy the above code and have a play with those, You can check if Curry in Ruby, Play here, 💻💻 link is working, or else copy above code and play with it!, 😎



Ok great, we are done with this post,  

Now if you like my way of talk, lets explore more blogs written by me and my inner knowledge,

Get to know answers for common search on Google : A blog for posts which can help you for daily life problems, such as where to get free images, Topic suggestion for the blog.

Computer Science algorithms and other knowledge share : A blog for posts such as best search algorithm, Top interview questions for diffrent technologies, knowledge share for some frameworks or programming languages for the interview or in general terms.

My ideas to solve real world problems : A blog where me shared and presented my ideas to solve a real world problems, this will be interesting for me.

Future of computer science technology discussed : A blog where me discussed about the future of computer science and new technologies which will change our way for looking to solve problems.

Ruby on Rails Web development Blog : As the name suggest, it is the blog for sharing few knowledge about RoR web development framework.


Liked my blogs, wanna to connect:

LinkedIn   GitHub  |  HackerEarth   ResearchGate  |  Twitter  |  Facebook  |  StackOverflow

Thanks for reading this post, Have a good day :)


Comments

  1. ROR framework is the most powerful and feature-rich framework for developing applications and websites. Hire Dedicated ROR Developers to witness flawless solutions.

    ReplyDelete

Post a Comment

Popular posts from this blog

Rails Postgresql Query timeout set and skip during migration

Rails Sync Postgresql and Elasticsearch Database of a Model

Ruby on Rails configure CORS setting with rack cors gem