Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 

Different ways to invoke method in ruby

Ruby provide with the following ways by which you can invoke the method based on your requirement : s= "ruby method" 1) Call a public instance method directly p s.length #=> 11 2) Other way is to invoke a method dynamically in ...

Ruby Methods

Ruby Methods are similar to functions which begins with lowercase letters. It bundles one or more repeatable statements into single units.Methods are always defined before calling them. Syntax: def method_name [( [arg [= default]]...[, * arg ...

How to create a new gemset & copy an existing gemset into it

We can create a new gemset to install a specific set of gems in it. In this i am copying an existing gemset into a newly created gemset. 1> To list the available gemsets we can type : rvm gemset list gemsets for ruby-2.2.3 (found in /...

How to install Rails in a particular gemset for a particular version of Ruby

1> For this we should first confirm the ruby version we are currently on rvm current (ruby-2.2.3) 2> To view the list of available gemsets before selecting a particular gemset, we can type rvm gemset list gemsets for...

SETTING UP ROR FOR THE FIRST TIME ON A NEW UBUNTU SYSTEM USING RVM

1> We need to install RVM before we can install Ruby, because RVM is a version manager and it will help us to install & manage different versions of Ruby on the same system and easily switch between them. To install RVM we first need to...

each, collect and map in ruby

1> 'each' will loop through each element of the array and evaluate whatever is return inside the block, but it returns the original array without any change. a = [5,10,15,20] => [5, 10, 15, 20] a.each {|t| t+2} => [5, 10, 15, 20...

Associations in Rails

Associations makes common operations simpler and easy in code and removes unnecessary complexities.It ties objects together using foreign keys. Associations creates a connection between two models. here is the code which shows Association exampl...

When to use dup, and when to use clone in Ruby?

Clone copies the exact copy of the original (same address.id) and the dup copy is a new record (address.id = nil). Any changes you will made in the clone copy will be changed the original object, but any changes to the dup copy attributes will re...

Controller in Rails

The logical center of your application, which provides interface between model and view is Controller. It is used to handle people-friendly URLs . Sessions are also managed by Controllers, providing users the facility of an ongoing interaction ...

Managing processes using Foreman gem

Foreman is a gem (aka manager) for managing multiple processes that your rails application is depending upon when running in development environment and also has a option to move it into production environment as well. It is based on a Procfile t...

REST in Rails

REST, is REpresentational State Transfer which provides the appropriate way of heterogeneous application-to-application communication. REST consists of principles that define how Web standards, such as HTTP and URIs should be used Five key pr...

form_for in Rails

It is a Rails helper method that shows forms on the view For example, while Rendering a partial form: <%= form_for([@post, @post.comments.build]) do |f| %> <div class="field"> <%= f.label :commenter %><br /> ...

How to print featured image in wordpress

Hello Reader's! If you learning the wordpress and want to show the contents of a page from admin panel. Then you can use the code below:- First you need to publish the page with it's title, content and a featured image. Now open the page and...

Migration in Ruby on Rails

Migrations comes up with the convenient way to alter your database in a structured and organized pattern. it makes use of Ruby DSL so no need to write SQL statements, allowing your schema to reflect changes independently. Anatomy of Migrat...

Rails Server Setup with passenger and nginx

Rails Server setup with Passenger and nginx Hi friends, Today I am going to tell you how to setup rails server with nginx and passenger. First you need to login to your server using ssh using the command ssh -i server_pem_key_path user@...

Using reject in ruby to delete elements selectively from an array

1> To delete an element from an array, use reject as shown below: arr = ['a','b','c','d','e'] arr.reject! { |i| i.match('d')} Result :=> ["a", "b", "c", "e"] 2> To delete particular keys from the array whose elements are ...

Single Table Inheritance in Rails

STI can be considered where single table can be used to represent multiple models based on different type. The parent model inherits from ActiveRecord::Base. Other models inherit from parent model. Data store and accessed from same table for all ...

How to get date with suffix st,th etc

While getting date in rails we do have a requirement where we need to show suffix attached to date like 2nd,3rd etc. There is no direct rails date function to get the same so in order to get date in the following format Thu, Nov 5th 2015 ...

Hiding all the URL in a text except a particular one

There was a requirement of hiding all the wesite URL in a message except for a particular one. While looking for a solution I came across a particular solution which I think might help other . params[:meal_note] = params[:meal_note].gsub(/...

Using rescue in single line

We can use rescue in single line to return a value if code of that line raise an error/exception, example shown below-: Let say we have a hash: person = {:name => "ABC"} person[:name].downcase # "abc" ...

Ruby Overriding

Ruby is a object oriented programming language , it support to provide a specific implementation to the methods of subclasses that is already defined in superclasses . If there is any method in subclasses is same as the method of superclass then ...

Sorting in Ruby

Using sort method we can sort any string, numbers, even classes either in ascending order or descending order .To sort an array ,you need to compare its element using <, >, <=, >= . return -1 if x < y return 0 if x = y return ...

The initialize method in Ruby

This method is like a constructor of other object oriented programming languages. when you want to initialize some class variables while creating an object, we use this method.. like any other ruby method,it starts with def keyword class abc...

break and next keyword

If i want to break our loop at specific condition then it is same as other languages . In Ruby continue doesn't work , in this there is equivalent keyword i.e next class Number def looping(d) while(d!=50) puts d d = d + 1 ...

Create classes in ruby

Ruby is a pure object oriented language and everything in ruby is an object of some class. In ruby we can create classes and create objects of the classes. In the below example we will create 3 classes Person, Address and Company. An object of...

Uses of class and objects

Use of classes and objects in ruby id different to other languages . And tell how to declare and define function in ruby and how to call the functions . Example : In below example i am creating a program of simple arithmetic program . class A...

.to_a in Ruby

.to_a is used to specify range. It is a method that converts an object into an array Example $, =", " # Array value separator range1 = (1..10).to_a range2 = ('bar'..'bat').to_a puts "#{range1}" puts "#{range2}" Output 1, 2, 3,...

String Methods

Reverse ( ) Method : This method is used to reverse the entire string. Example : var1 = 'mukesh' var2 = 'tomar' var3 = '....hello mukesh' puts var1.reverse puts var2.reverse puts var3.reverse puts var1 ...

Variable Number of Parameters

If you declare a method that takes two parameters, on This method call , you need to pass two parameters along with it. But, Ruby facilitates to declare methods that can work with a variable number of parameters. Example def evon (*t)...

fibonaci series using arrays in Ruby

It is a series of Whole number where we have to find sum of the two preceding numbers. It Begins with 0 and 1, the sequence goes like this 0,1,1, 2, 3, 5, 8, 13, 21, 34, etc. using the formula n = n(-1) + n(-2), where the n(-1) is the the l...

Applying limit and offset to array

In rails we can apply limit and offset to arrays in different ways. 1> Using "slice" method. slice takes 2 arguments, the first one being the "starting_index" & the second one is the "number_of_elements". syntax :- arrayob...

modules in Ruby

For grouping together methods, classes, and constants, we make use of module. Modules give you two major benefits: 1) prevents name clashes. 2) implement the mixin facility. Syntax: module Identifier statement1 statement2 ...

next Statement in Ruby

By using next statement, control will go to next iteration of the loop. Terminates the execution of a block if it will be called in that block. Example for i in 0..8 if i < 3 then next end puts "Value of i is #{i...

How can one install ruby on ubuntu

Choose the version of ruby you want to install for example : 2.2.3 After choosing the right version of ruby first step is to install some dependencies : sudo apt-get update sudo apt-get install git-core curl zlib1g-dev build-essential libs...

Strong Parameters in Rails

Strong Parameters in Rails Strong parameters are used to prevent the action controller parameters that are used in Active Model Mass Assignment. If you want to use them, you need to white-list them. It is basically used as a security for sensiti...

default_url_options in Rails

default_url_options in Rails Rails has a url_for helper, which is used for generating hyperlinks. So many times we want that a certain thing or parameter needs to be passed with each link or request to tell the controller to do certain thing...

Use of Flash in Rails

Use of Flash in Rails In rails, flash is also used as a session management method. Its feature is that it gets cleared after every request. It is basically used when we just want to show a message in the next request or view and don't want t...

JQuery Ajax Post in Rails with example

JQuery Ajax Post in Rails As we all know, ajax is used for saving some objects to server or requesting to server and in response, page should not be refreshed. Rails provides helper remote: true, that can be added in ruby form helpers. After a...

Configuring Unicorn for rails app

Unicorn is an HTTP server for Ruby. It is designed to be used in production environment unlike Webrick which is designed to be used at the time of development on local machine. Because it is very fast, efficient and offers lots of good features l...

Explain Validation helpers

Active Record gives many predefined validation helpers which you can use directly in your class. These helpers provide us rules which can be commonly used as a validation rules. So if the validation fails, a error message is added to object error...

Using Cookies in ROR

Cookies are objects that we use to store some information of the user in the browser like geolocation of user . It is a set of key value pair. All cookies have time of expiry after which they are just deleted from the browser usually at the end o...

Rendering XML and JSON in rails

Rendering xml or json data from the controller method is very easy. We have to just follow this syntax. Example(xml): def index @users = User.all respond_to do |format| format.xml {render :xml => @users} ...

Single Table Inheritance And where to use it in Rails

This particular post will give you a overview of Single Table Inheritance And where to use it in Rails. Single Table Inheritance is, as the name suggested, it is a way to add inheritance to your models. STI lets you to save different models i...

Adding Translations in Rails

Adding Translation to a Rails Application Hi friends, in my previous blogs related to internationalization of rails application, we have configured the i18N api and saw different types of methods to pass the locals, if you have not read thos...

Setting Rails Locales from URL

Rails Internationalization : Setting locals from URL In my previous blog, we looked in Setting rails locale from domain name, it was a good approach of detecting the desired locale from domain or subdomain, but sometimes you are not able to ...

Setting rails locals from Client Requests

Rails Internationalization : Setting locals from Client Requests Hi friends, in my previous blog, we looked into Setting the Locale from the URL Params. There are some more cases where we don't want to set the locales through URLs or domains...

Setting rails locale from domain name in rails for internationalization

Rails Internationalization : Setting locale from Domain Name For translating your application to other language, you can set I18n.default_locale to your locale in application.rb or in initializers as I told in my previous blog Configuring i1...

raw vs. html_safe vs. h to unescape html

Rails has a nice way to protect yourself from CSRF attacks through threatening HTML encoding from anything you write between <%= %>. But there is a caution, you want to render HTML from string so you need to tell rails that it should not es...

Is Ruby pass by reference or by value?

When we have to call a function with parameters there are basically two most generally known and easy to understand approaches for passing of parameters among all programming languages and these are pass-by-value and pass-by-reference. Ruby is pa...

Using session objects in Ruby on Rails

Session objects are used in ROR to store small amount of information which can be used later on. In fact websites following http (a stateless protocol) use session object to keep logged in user informations so that those informations persists b...
1 7 9 10
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: