alias_method(p1, p2) public

Makes new_name a new copy of the method old_name. This can be used to retain access to methods that are overridden.

   module Mod
     alias_method :orig_exit, :exit
     def exit(code=0)
       puts "Exiting with code #{code}"
   include Mod


   Exiting with code 99
Show source
Register or log in to add new notes.
May 1, 2015
6 thanks

Very bad documentation

This is terrible documentation. It makes it very hard to understand what the arguments mean.

The signature is

alias_method(p1, p2)

So what do p1 and p2 mean? The description doesn’t refer to them at all, but to new_name and old_name. How are we supposed to know which is which?

And then it gets even worse in the code sample:

alias_method :orig_exit, :exit

From the naming it sounds like the first argument is the original method name.

Documentation is supposed to resolve this kind of confusion, not create it.

April 8, 2010
2 thanks

Bad example

Note that it would be better to avoid the alias_method line in the example and just call super.

October 10, 2012 - (>= v1_9_1_378)
1 thank

Bad Example

@nZifnab it is a bad example because an included module is basically a class.

module Mod
    def exit(code = 0)
        puts "Exiting with code #{code}"

include Mod

exit 99


Exiting with code 99
December 10, 2012
1 thank


Including module in a class does not automatically over-write methods defined with the same name.


module Mod

def exit(code = 0)
  puts "Exiting with code #{code}"


class OriginalClass

include Mod
def exit
  puts "Original message"


OriginalClass.new.exit 99


exit': wrong number of arguments (1 for 0) (ArgumentError)

if you use this construct, the alias_method will work similar to super:

module Mod

alias_method :super_exit, :exit
def self.included base
  base.instance_eval do
    def exit(code = 0)
      puts "Exiting with code #{code}"


March 3, 2011
1 thank

Perfectly applicable

@rkh You would be correct if this code had occurred in a subclass who’s parent method was being overridden.

However, defining the method in this manner is completely removing the old method - as if you had written code like this:

class MyClass
  def do_something
    puts "We're doing some stuff here"

  def do_something
    puts "The old do_something method no longer exists!"

# => "The old do_something method no longer exists!"

Of course this is non-sensical. But the idea is that you have either included a module, or monkey-patched an already existent class, and completely replaced the old method. super(*args) will not work

August 18, 2016
0 thanks

Volt Framework for Ruby

Volt – a new Framework for Ruby where both the server and client sides are written in Ruby via OPAL (a ruby to JavaScript compiler) so developer can write dynamic applications without writing a single JavaScript code. Volt is similar to Meteor but it doesn’t have all the portions of Meteor.

The Basic Setup for Volt Framework

Let us install Volt and create an empty app. Make sure that you have ruby (>2.1.0) and ruby gems installed.

Install Volt Gem :

gem install volt

We can create a new project using the volt gem:

volt new sample_project

Fire up the web server:

bundle exec volt server

We can access the Volt console with:

bundle exec volt console

The Opal Compiler

Volt applications run Ruby on both frontend and backend. So the puts statement in a controller action appears in browser window and not in terminal console. And also writing Ruby code for the front end with Volt is very easy. The opal compiler translates Ruby to JavaScript. Amazing thing about it is that there is no compilation process to follow and no build tools to install. When you run volt server, everything takes place in the background. No refresh or restart is needed when you do changes to code and data.

Calling a JavaScript alert with Ruby

# Calling JavaScript functions in Ruby
module Main
class MainController < Volt::ModelController
# Called from front end when “todos” route loads.
def todos
alerttotes amaze’

Easy Syncing via Reactive Models

Concentrate more on this part when learning volt. Volt::Model acts as hash-like Ruby objects that sync between the front end and back end simultaneously. Usually, updates to the model happens automatically. The concept of “stores” in Volt is used to sync application data in persistent and non-persistent forms. And also a uniform means of syncing data between local storage, MangoDB, cookies, sessions and URL params.

Let’s check how to create real-time chat app of Ruby and HTML:

# Create a persistent data model. This gets stored in MongoDB.
class ChatMessage < Volt::Model

View Code:

<form e-submit=”say”>
<input class=”form-control”
value=”{{ page._input }}” />
{{ _chat_messages.each do |msg| }}
<button e-click=”msg.destroy”>X</button>
{{ msg._text }}
{{ end }}

Full HTTP Endpoint Support

Volt is not only for real-time framework. It also provides workflows for traditional HTTP application development. Checkout an example from GitHub :

# Routes for HTTP endpoint
controller:simple_http’, action:index’

controller:simple_http’, action:show’

controller:simple_http’, action:upload’

# Example controller

class SimpleHttpController < Volt::HttpController
def index
render text:this is just some text’

def show
render text:You had me at “\

def upload
uploaded = params[:file][:tempfile]
File.open(‘tmp/uploaded_file’,wb’) do |f|
render text:Thanks for uploading’
August 24, 2016
0 thanks

Scheduling Recurring Events with Ice Cube Gem

Ice_cube is a ruby library for effectively taking care of repeated events (schedules). The force lies in the ability to indicate multiple rules, and have ice_cube rapidly make sense of whether the schedule falls on a specific date (.occurs_on?), or what times it happens at (.occurrences, .first, .all_occurrences).

How to get ice cube

For install use the below syntax

gem install

if you want to get the code

gem clone git://github.com/seejohnrun/ice_cube

For creating icecube schedule

schedule = IceCube::Schedule.new
if we want to speciy startdate and enddate we have option to specify in the above mentioned schedule

schedule = IceCube::Schedule.new(start = Time.now, :end_time => start + 600)

Daily schedules

After creating schedule we have an option to add recurrence rule for the above mentioned schedule

consider “schedule every day” on above mentioned time

schedule.add_recurrence_rule IceCube::Rule.daily

consider the same schedule with repeat “n” number of days

schedule.add_recurrence_rule IceCube::Rule.daily(repeat_every_n_days)

in place of repeat_every_n_days you have option to specify the number of days For Weekly Monthly & Hourly You can Read It From Here http://goo.gl/XGmXp1

September 8, 2016
0 thanks

How to use Acts_As_Votable Gem

Acts_As_Votable is ruby gem specifically written for Rails/ActiveRecord models and This gem allows any model to be voted on upvote/downvote like/dislike, etc. It allows any model to be voted under arbitrary scopes using this gem we can vote any model. votes do not have to come from a user, they can come from any model (such as a Group or Team) and it provide an easy to write/read syntax.

Gem Installation


Add above line in Gemfile and run bundle install

Supported ruby and rails versions

Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.0
Rails 3.0, 3.1, 3.2, 4.0, 4.1+

This Gem uses vote table to save all voting information. To generate vote migration run below commands

rails generate acts_as_votable:migration
rake db:migrate

To rate any model just use “acts_as_votable” in model


class Article < ActiveRecord::Base

@article = Article.new(:name =>my new article’)

@article.liked_by @user
@article.votes_for.size # => 1

Read More From Here http://goo.gl/emtP8K