Tag Archives

18 Articles

A simple Configuration class in Ruby

by Abu Lewis

Just in case you never heard of OpenStruct…

And this is how it can be implemented…

IceCube => Ruby Date Recurrence Library

by Abu Lewis

I love that i found it :). Check it at ice_cube github repo.

This is my first playing around code;

The output would look like this

When to write modules in Ruby!?

by Abu Lewis

To be short and concise, As soon as you have identified a behavior that is or may be shared between different objects, at that time it is a good idea to push that behavior in its own module.

So lets fabricate a use case;

You are given the task to write a little program for a zoo. They need to register the type of food and the time for which the different animals are fed. The zoo has a wide variety of animals, like big cats, fishes, reptiles etc.

So as a good programmer, we do know that all these animals ”eat” (Common behavior). So eating is a good candidate to be separated in a module. The different animals may implemented differently depending on the type, race etc.

So if we define an module and call it EatingBehavior that would look like this;

We can later define a Lion and a Fish class that may look like these;

The both objects are now ”eaters” and if we would define more animals, we should just repeat the same ”including” process….

To test the implementation;

Creating a Ruby service using the dRuby module to be called within a Rails application

To simplify things, lets fabricate a use case scenario;

Imagine that you have to control a password’s strength when the user is registering a new account. The controlling of the password is done using a password list file obtained from https://wiki.skullsecurity.org/index.php?title=Passwords. I’m using the best available one according to the site, which is rockyou.txt.bz2.

To use the list, I have extracted the list and copied it to /usr/share/wordlists/. Once that is done, the shell command ”grep” can easily be used;

The command will check if the password ”hacker” exists or not in the file. And to use the same command in our ruby applications, we can construct a class like this one.

And to make things look like beautiful, We write an dRuby runner class as this one;

To bootstrap and run, we will do so;

Now the server part is up and running. To test our client, we can do so;

The client part can be implemented in your rails application as well!

Connecting IOS socket client to a Ruby socket server using Swift & Ruby’s eventmachine gem.

To demonstrate, I have written a server that has one task. It reads a word and sends it back to the client in a reversed form.

The server code in few lines ;/

The client, which is written as a part of an IOS application in Swift;

The demo application that I wrote is a single view-based IOS-application, with 1 textfield, label and button.

An instance of ReverseClient is created in the ViewController…

And when the button is pressed;

Do not forget to start the server!

Writing a wrapper WebSocket client class in JS

by Abu Lewis

I released this custom WebSocketChatServer gem earlier this month and thought that it may be a good idea to wrap the server’s client in a JS class just to make it easier to communicate with the server.

Here is the JS client:

And the example of usage:

Encrypt text in iPhone & decrypt it in your Rails server

by Abu Lewis

What I did to perform the task is that i used RNCryptor in my IOS application.

And I engaged this gem to do the decryption in Rails.

Parsing an URL with Ruby

by Abu Lewis

Can not be easier than this…

Filter pattern in Rails

by Abu Lewis

Imagine you have an User < ActiveRecord::Base object and you want to apply filtering implementation to it. The filtering will be based on certain criteria like get me all the adult users or all the users that are females or males. That kan be achieved in different ways. One way is using Rails's scoping or by defining class methods. If you like the idea of the design patterns, it turned out that there is a design pattern for this problem and it is called the Filter design pattern. And to demonstrate, We will create an interface class to outline the behaviour of our Filtering criteria. This interface can then be extending with concrete criteria classes containing concrete implementation. This is the domain class I skissed to describe it better: cd

And the code:

By doing so, the User class will look cleaner for me…

To play around with the newly created filters in rails console:

Thank you!

Observer pattern exemplified in Ruby

by Abu Lewis

The observer design pattern is best demonstrated using the broadcaster <-> listener example.

A broadcaster in this example is an object having implementation for attaching listeners to itself. When the broadcaster will broadcast a new message, all the attached listeners are notified.

A listener class will register the broadcaster to itself so that it can access the message that has been broadcasted by the broadcaster. It will also have a method, which will be called by the broadcaster when notifying.

Let’s play around…