Tag Archives: Ruby

There I was trying to guide a very good friend of mine, Sajin Shakya who was keen to learn a programming language, Ruby On Rails. I suggested him not to jump onto Rails and dig deep into Ruby first. Hence, as he had learnt C programming earlier, I told him to practice developing similar algorithms he’d previously learnt in his academics through Ruby.

So, he created a method which would calculate the factorial of given number as follows:

My first thought before suggesting was if the method definition would work without class definition or not. But to my surprise it was working fine. Then he questioned me why we should be defining the method within the Integer class rather than defining it externally as he’d done. Also, his thought was that it would reduce the line of code as defining class would not be required.

I convinced him with how important the class could be to make our coding life easier but he resisted that in this case it was not required to define the class. Now, our focus was on if the particular change was required to experiment ruby codes in file or not. Hence, the question arose “Is class required while experimenting Ruby codes in file?”.

We benchmarked and noted the difference with the performance but it was not that noticeable. Then, we moved on to figuring out what the difference we could get from these two routes.

The difference noted was that the method were defined within specific class meaning that only that particular class would get the defined method and hence, the error would pop-up immediately if the method implementation was made in a wrong class. And since we’d always have integer to calculate the factorial it seemed legible.

In the first case if a “string” was provided to the method then it would pop-up the error after running through the codes in the method. But in second case it would not bother to run through the codes as it is a “string” and the method is defined for the “integer”. The errors observed can be seen below.

Please feel free to comment your thoughts and help us figure out.

Is class required while experimenting Ruby codes in file? was last modified: February 24th, 2015 by sushil10018

Similarly, Ripple doesn’t provide direct method for adding validation only on create while trying to implement Riak On Rails using Ripple as modeling layer. This is very much necessary for the validation of properties such as password which are to be checked only while being created. Below is the code sample I developed for validation on create manually.Continue reading

Validation on create with Ripple (Riak On Rails) was last modified: August 7th, 2014 by sushil10018

I’m a riak newbie who is trying to implement Riak On Rails using Ripple as modeling layer. But while creating User model, I wanted validation for uniqueness of user. Validation such as presence is available with Ripple but I couldn’t find proper validation method directly provided for the uniqueness. So, below is the sample code of User model I developed for validation.Continue reading

Uniqueness Validation With Ripple (Riak On Rails) was last modified: August 7th, 2014 by sushil10018