Two ways to load files in Rails

There are two ways that files get loaded in Rails:

It is registered in the autoload process, and you reference a constant that corresponds to the file name. For instance, if you have app/controllers/pages_controller.rb and reference PagesController, app/controllers/pages_controller.rb will automatically be loaded. This happens for a preset list of directories in the load path. This is a feature of Rails, and is not part of the normal Ruby load process.
Files are explicitly required. If a file is required, Ruby looks through the entire list of paths in your load paths, and find the first case where the file you required is in the load path. You can see the entire load path by inspecting $LOAD_PATH (an alias for $:).

Since lib is in your load path, you have two options: either name your files with the same names as the constants, so Rails will automatically pick them up when you reference the constant in question, or explicitly require the module.

I also notice that you might be confused about another thing. ApplicationController is not the root object in the system. Observe:

module MyModule
  def im_awesome
    puts "#{self} is so awesome"
  end
end
class ApplicationController < ActionController::Base
  include MyModule
end

class AnotherClass
end

AnotherClass.new.im_awesome
# NoMethodError: undefined method `im_awesome' for #

You will need to include the module into whatever class you want to use it in.

class AnotherClass
  include MyModule
end

AnotherClass.new.im_awesome
# AnotherClass is so awesome

Of course, in order to be able to include the module in the first place, you’ll need to have it available (using either of the techniques above).

Reference: This note is from a blog/site that I looked for some knowledge.

Advertisement

How to know your machine is 32 or 64 bit?

Here is an easy way to find this, in command line type

$ getconf LONG_BIT

Synopsis:

getconf [-v specification] system_var

the getconf utility shall write to the standard output the value of the variable specified by the system_var operand.

So it prints the value of ‘LONG_BIT’.

Read linux manual for getconf: http://linux.die.net/man/1/getconf

 

Get information about your Rails Environment

To get information about your Rails Environment Rails, Ruby, Rack versions use the following command,

$ rake about

About your application's environment
Ruby version 1.9.2 (i686-linux)
RubyGems version 1.8.10
Rack version 1.3
Rails version 3.1.3
JavaScript Runtime therubyracer (V8)
Active Record version 3.1.3
Action Pack version 3.1.3
Active Resource version 3.1.3
Action Mailer version 3.1.3
Active Support version 3.1.3
Middleware Rack::Cache, ActionDispatch::Static, Rack::Lock, Rack::Runtime, Rack::MethodOverride, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::RemoteIp, Rack::Sendfile, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport, Warden::Manager, OmniAuth::Builder
Application root /home/abhi/my_app
Environment development
Database adapter mysql2
Database schema version 20120704103548

Rails Console ‘sandbox’ to test your rails application data

We know that how useful the rails console is. We can access the database and manupulate the data, but if you wish to test out some code without changing any data of your database, you can use sandbox option rails console –sandbox.

$ rails console --sandbox
Loading development environment in sandbox (Rails 3.1.0)
Any modifications you make will be rolled back on exit
irb(main):001:0>

How is it?

Shell command for killing all the processes together, that we found using grep command

Sometime when we found a lot of process associates with a software or something that may hurt our memory or CPU, and it would be nice to kill that all process and be our system in a fine position. But when there is so many process we can’t kill all the process by hand using the command

 $ kill -9 PID

where ‘PID’ is the process id.

Shell commands will help us to do this. See below

First you create a .sh file file named ‘killAllProcess.sh’.

#!/bin/bash
echo "Usage : ./killAllProcess.sh "
for i in `ps ax | grep $1 | grep -v grep | sed 's/ *//' | sed 's/[^0-9].*//'`
do
  kill -9 $i
done
echo "Killed All the process which has name with $1"

And make this file executable

$ chmod +x killAllProcess.sh 

Then run it

 $ ./killAllProcess.sh 

And see any process with that name is there

$ ps aux | grep PROCESS_NAME

All vanishes..!!

Ruby string method ‘scan’

Here I am writing about the ruby scan method for string.

syntax: scan(pattern) => array

Take a string name, where

irb> name = "Viswanathan Anand"

You scan through the string for words by,

ruby-1.9.2-p290 :014 > name.scan(/\w+/)
 => ["Viswanathan", "Anand"] 

scan for 3 letters and make them an array element,

ruby-1.9.2-p290 :015 > name.scan(/.../)
 => ["Vis", "wan", "ath", "an ", "Ana"] 

scan for 3 letters and group them with an array and make them array element,

ruby-1.9.2-p290 :016 > name.scan(/(...)/)
 => [["Vis"], ["wan"], ["ath"], ["an "], ["Ana"]]

and you can make three two pair letters in an array like

ruby-1.9.2-p290 :017 > name.scan(/(...)(...)/)
 => [["Vis", "wan"], ["ath", "an "]]

You can also use reg exp like,

ruby-1.9.2-p290 :018 > name.scan(/[A-Z][a-z]+/)
 => ["Viswanathan", "Anand"]