Cheat sheet for managing files from Ruby


Bookmark and Share

In this Cheat sheet, you will learn managing files from Ruby.

Files are used for storing the Data for a Long time Period. And the files can contain any type of information means they can Store the text, any Images or Pictures or any data in any Format. It is associated with class IO File includes.

Creating a New File

New files are created in Ruby using the new method of the File class. The new method accepts two arguments, the first being the name of the file to be created and the second being the mode in which the file is to open. Like,

file = File.new(“filename”, “mode”)

Eg:

Supported modes are:

r
Read only access.

r+
Read and write access.

w
Write only access.

w+
Read and write access.

a
Write only access.

a+
Read and write access.

Opening Existing Files

You can open the existing files using the open method.

Eg:

If the file is already opened, we can close it by using the close method.

Eg:

Reading and Writing Files

Once we’ve opened an existing file or created a new file we need to be able to read from and write to that file. We can read/write using different methods.

sysread Method:

You can use the method sysread to read the contents of a file.

Eg:

This statement …

Read More

Get into Sentiment Analysis with Ruby


Bookmark and Share

 

Sometimes we fail to understand other’s emotion. So how it will be when machines try to understand ours? When writing programs we care about the syntax and structures but these concerns are not there in communication between people. To process our language machines have to understand not only what we say, but what we mean. Natural language processing is a fascinating subject to explore. But what makes it complicated?

Human communication isn’t just a group of words. It’s a mix of sentiments which needed to be analyzed to understand what we really mean.

The goal of sentiment analysis is to identify the opinions expressed in a text.

It seems easy, right?

  • I’m happy to watch a new movie
  • I hate war
  • Since happy is a positive word and hate is negative, we know the sentiments in each sentence. This is the most simple situation.

    But what if a text contains two opinion words and one sentiment?

    If we assign +1 to positive word and -1 to negative word,

    • I’m happy and excited to be going to watch a new movie

    The above sentence is positive +2

    • I hate war and the violence it makes

    The text contains two negative words so it is negative -2

    Now, let’s …

    Read More

    Refactoring in Rails


    Bookmark and Share

     

    When the complexity of our Rails application increases, it becomes difficult to test and add new features. As we know, Rails is built on the principle of convention over configuration. Putting too much logic in the controller will eventually violate the single responsibility principle making future changes to the codebase difficult and error-prone. Refactoring the code helps for quality, clarity, and maintainability.

    When should we refactor?

    Tests usually run faster with well-isolated code. Slow running tests indicate the need of more sophisticated design. For example, each class should be concerned about one unique functionality. Also, models and controllers with too many lines of code needed to be refactored to make code DRY and clean. We can use best approaches of Object Oriented Programming to achieve this.

    Consider the following example of a controller where the whole logic for creation and deletion is concentrated.

     

    Moving a big controller’s action to service objects

    To achieve the new design, keep controllers as thin as possible and always call service objects. Controllers are a good place to have the HTTP routing, parameters parsing, authentication, calling the right service, exception catching, response formatting, and returning the right HTTP status code. A service …

    Read More

    Getting started with Angular 4


    Bookmark and Share

    Angular is a most popular platform for building applications with the web. Angular empowers developers to build applications that live on the web, mobile, or the desktop. The AngularCLI is a command line interface tool that can create a project, add files, and perform a variety of ongoing development tasks such as testing, bundling, and deployment.

    As compared to the older versions of Angular, there are many new things added to the list. Not only new features but also some twists are there that enhance old features. Forget Angular 3, Google jumps straight to Angular 4 after Angular 2. Angular4 applications are much faster and less space consuming.  Angular4 is compatible with TypeScript’s newer versions 2.1 and 2.2. Components are the basic building blocks of your application. Every component has an associated template, style sheet, and a class with logic.

    Prerequisites

    1. NodeJS ( 6.x.x or greater )

    2. npm (3.x.x or greater )

    Installation

    1. You have to install AngularCLI globally.

    2. Create a new project.

    3. To serve the application, get into the project repo and run :

    For launching our server and for rebuilding our app …

    Read More

    Rails Generators


    Bookmark and Share

    Rails generators are used to create many files for models, controllers, views, unit tests, migrations and more. It will do some of the manual work for us. So it saves time. They can also be used to set up some basic specs for the application test suite.

    Running rails generate(or rails g) by itself gives a list of available generators:

    The main generators that rails offers are:

    • controller: The Rails controller coordinates the interaction between the user, the views, and the model.
    • helper: Code in helpers is included in the corresponding view. We can move big loops, method calls or other logic into a method in the helper.
    • mailer: It allows for sending and receiving emails from and to your application.
    • migration: Rails Migration allows you to use Ruby to define changes to your database schema.
    • model: It is Ruby class that talk to the database, store and validate data, perform the business logic.
    • scaffold: It refers to the auto-generation of a simple set of a model, views and controller usually for a single table.

    Following is the list of options, which can be used along with generators:

    -h, [–help] # Print generator’s options and usage
    -p, [–pretend] # Run but do not make any changes
    -f, [–force] …

    Read More

    PostgreSQL 9.6 new features


    Bookmark and Share

    POSTGRESQL is an open-source object-relational database system. It is not controlled by any corporation or other private entity. The source code is available free of charge. PostgreSQL supports transactions, subselects, triggers, views, foreign key referential integrity, and sophisticated locking.

    New features in Postgres are:

    • Parallel execution of sequential scans, joins and aggregates.
    • Avoid scanning pages unnecessarily during vacuum freeze operations.
    • Synchronous replication now allows multiple standby servers for increased reliability.
    • Full-text search can now search for phrases (multiple adjacent words).
    • postgres_fdw now supports remote joins, sorts, UPDATEs, and DELETEs.
    • Substantial performance improvements, especially in the area of scalability on multi-CPU-socket servers.

    Parallel execution of sequential scans, joins and aggregates

    PostgreSQL can devise query plans which can leverage multiple CPUs in order to answer queries faster. This feature is known as the parallel query. Mostly, queries that touch a large amount of data but return only a few rows to the user will get benefit by using Parallel Query. It can now execute a full table scan in multiple parallel processes, up to the limits set by the user.

    Avoid scanning pages unnecessarily during vacuum freeze operations

    Freezing …

    Read More

    Web Frameworks in Crystal


    Bookmark and Share

     

    The creator of Ruby, Yukihiro Matsumoto said that the language was made to make programmers happy. Imagine a language as elegant as Ruby and blazingly fast as C. If you already love Ruby, maybe it is time to start considering Crystal lang, the younger sibling of Ruby. It is a compiled language which is syntactically very similar to Ruby and designed for high throughput and performance.

    Features

    • Native WebSocket support
    • Compiles to efficient native code
    • Statically typed => if any errors it will let you know at compile time.
    • Less memory consumption

    Web frameworks in Crystal

    Applications developed using framework are easy to maintain and upgrade at a lower cost. This article lets you get familiar with some of the most popular frameworks of Crystal.

    Install Crystal to get started.

    https://crystal-lang.org/docs/installation/index.html

    Create our Crystal app

    Kemal

    https://github.com/kemalcr/kemal

    To know the true potential of Crystal, let’s familiarise with Kemal, most popular Crystal framework. It’s a Sinatra inspired framework.

    Install Kemal

    In our app,

    Open in an editor and add Kemal as a dependency in the shard.yml file.

    To get dependencies, run

    This will install Kemal.

    This is similar to adding gems in gem file and bundle install in Rails.

    In the file sample-app.cr created in the src directory, substitute the following

    In the …

    Read More

    Event Listeners in VueJS


    Bookmark and Share

    Vue.js is a Javascript framework for user interface. There are many directives in Vuejs. A directive’s job is to reactively apply special behavior to the DOM when the value of its expression changes. You can use the v-on directive to bind event listeners to DOM events.

    We are binding a click event listener to a method named clickMe. Here’s how to define that method in our Vue instance. Here we have a counter variable initialized to zero. Inside the method, we are incrementing the value of the counter. So for each click on the button, the method is invoked. You will get the code here.

    Here in this example, we are using two button and methods. One for incrementing the count and other for decrementing. You can see code here.

    Note :-

    For example,

    This will give same result as before.

    We can see another example. Here we have an input field where we type the url which is bound to the Js using v-model.  We have a button with click event listener which is bound to the method humanizeUrl. We have two urls, url and cleanUrl. url  is what we type in the input field and …

    Read More

    Using mina to deploy a particular commit


    Bookmark and Share

    Mina is our tool of choice when it comes to automating our deployment. It might not be the best tool out there, but it is enough to get us started. Single server or multi server, we love Mina.

    In most of uses cases if something fail in production we just need to rollback to one or two version before to it. But recently we had a case in which we had to revert to version order than the previous 5 version (5 is default no of versions stored). The quick thing that we did at that point is copy of master called temp-master branch and changed master to the version that we wanted and had the new master deployed.

    We didn’t like that solution much, so we decided to look for a cleaner solution. Looking at the source code of the git task inside mina, we found that mina had an option to deploy a particular git commit. The config was set :commit, HASH (Thee cheers to the contributors to mina for having that).

    Now that made our life easier for the future. We just added the following line to our deploy.rb

    set :commit, ENV[‘COMMIT’]

    so now we can …

    Read More

    Two way binding in Vue.js


    Bookmark and Share

    Vue.js is a JavaScript framework getting popular considering its simplicity praised a lot these days. There are a lot of JavaScript frameworks among which React and Angular are popular among web developers. Vue.js is much flexible and less opinionated framework than Angular. It’s similar to React but much simpler. Vue.js supports Two-way binding. That is it keeps your data in sync with your DOM without you having to do anything. Two-way binding in Vue.js is MVVM pattern. That is Model View View Model. Like MVC, Model is the data object and view is what is displayed up. The Model can tie to the View and the View can tie back to Model.

    In one way binding, JS variable is bound to the DOM.

    Here when you change the data in JS, it will also update in the DOM. So you will get the updated message on the page. You can get the basic code here.

    But in two way binding, Js variable can be bound to the DOM and data is also bound from the DOM back to JS.

    Now, if you type anything into your input, your JS variable is updated with the change, which in turn updates your span. You can …

    Read More