Hexa's Blog

Disable active record (nodatabase uses) in Rails 4

27/01/2015 Ruby on Rails 4

This article shows a quick tweak to unuse database gem, disable active record in Ruby on Rails 4.x

1. For new projects, run the following command to generate new project

rails new YourProject --skip-active-record

2. For existing projects

  • Edit file config/application.rb, remove require 'rails/all' and add:
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
  • Edit file /config/environment/development.rb, remove the following line:
config.active_record.migration_error = :page_load
  • Remove gem named sqlite3 in Gemfile
  • Delete or Comment (recommend) database.yml, db/schema.rb
  • Delete configuration in files in config/environments directory

Ruby - learning

06/01/2015 cheatsheet

#1. Variable in ruby class

  • Local variable: defined inside a method, the usage of local variable in only inside a method. Prefix of local variable is _ or [a-z].
  • Instance variable: instance variable is available accross methods and object, meanwhile, instance variable change from object to object. Prefix of instance variable is @. Instance variable is not shared among its descedants.
  • Class variable: it belongs to a class and is a characteristic of this class. Prefix is @@. Class variable is share among its descedants(childs).
  • Global variable: class varaible is not across class, however, global variable does. The prefix of global variable is $.
  • Constant variable: it’s similar to class variable but it’s constant. Constant varaibles should be in upper case for all letters.

#2. Commenting

  • Using =begin and =end
=begin
This is a multiline comment and con spwan as many lines as you
like. But =begin and =end should come in the first line only.
=end
  • Using #
 # This is a single line comment.

#3. Method

def method_name (arg = default_value,arg2 = default_value2 )
    expression
end

#4. Block

  • Block must be named after a name of a method (or a function), other while, it does not work. Usage: In a method, there might be an chunk of code which is used many time within the method, the point is that, this chunk of code is not worth making a new function or maybe programmers don’t want to make a function on that chunk (they cannot think a name for that).
def function_name
   puts "In a function"
   yield
   puts "Come back to function"
end

function_name{
   puts "In a block"
}
  • It’s also possible to insert parameters into a block. However, if a function has parameters, I don’t know but there is a error and cannot use block with function which have parameters.
def function_name
   yield a, b
end
function_name {
   |a,b|
   puts a+b
}

#5. Class

  • Class structure
 #making a class
class Box
end
  • Initial method
class Box
 #`@` is a declare of instance varaibles
    def initialize(w,h)
	    @width = w
		@height = h
    end
end
  • Getters and setters
class Box
    @@number_of_box = 0
    def initialize(w,h)
	    @width = w
		@height = h
    end
    # These are getters
    def printWidth
	    @width
	end
    def printHeight
	    @height
    end
    #These are setters
	def setWidth=(new_value)
	    @width = new_value
	end
	def setHeight=(new_value)
	    @height = new_value
    end
	#usage of @@class variables
    def printNumberOfBox
	    puts @@number_of_box
	end
end

#6. Access control: Public, Private, Protected

  • Public method: is called by anyone. By default, all methods excepting initialize method are public methods.
  • Private method: only class methods can access private methods
  • Protected method: is called by class method and its subclass method.
private :functionA, :functionB
protected :functionD, :functionD

#7.Inheritance, method overriding, operator overloading

  • ’<’ is used to indicate inheretent from class to class
class Box
end

class AutoBox < Box
end
  • Method overriding: change existing method (parent class) to new method (child class)
class Box
    def print
	    puts "base class"
    end
end

class AutoBox < Box
    def print
	    puts "child class"
    end
end
  • Method overloading: Unlike java, ruby is using dynamic typed language, as a results, it’s impossible to using overriding if depending on variable types. On the other hand, it uses number of parameters to differentiate methods.
def function_name(a,b) #there is no type declaring, compiler does not know
end                    #how to override

def function_name(a,b) #As a consequence, compiler uses number of parameters to
end                    #differentiate two methods

def function_name(a)
end

8. Loop

a. While loop

_counter = 0
while _counter < 5
  puts _counter
  _counter++
end

b. For loop

  # range is [0-5]
for counter in 0..5
  puts counter
end
  # range is [0-5)
for counter in 0...5
  puts counter
end

#9. Symbols a. What is this Symbols in ruby are immutable. Besides this point, it’s a string. It’s able to print put the value of a symbol in term of string or integer

:age #this is a symbol named age
puts :age # print out age's value in string
puts :age.object_id # print out age's object id

b. Implementation Automatic make new method based on the symbols

def make_me_a_setter(thename)
	eval <<-SETTERDONE
	def #{thename}(myarg)
		@#{thename} = myarg
	end
	SETTERDONE
end

class Example
	make_me_a_setter :symboll
	make_me_a_setter "stringg"

	def show_symboll
		puts @symboll
	end

	def show_stringg
		puts @stringg
	end
end

example = Example.new
example.symboll("ITS A SYMBOL")
example.stringg("ITS A STRING")
example.show_symboll
example.show_stringg
  # reference: http://www.troubleshooters.com/codecorn/ruby/symbols.htm

#10. Hash a. Declare a hash

  • Old hash
old_hash = {:var1 => "cat", :var2 => "dog"}
  • New hash
new_hash = {var1 : "cat", var2 : "dog"}

b. Access element

new_hash = {var1 : "cat", var2 : "dog"}
puts new_hash[:var1]
puts new_hash["var1"]

c. Documentation link

Python - learning

07/12/2014 cheatsheet

This is a remember note to learn python, more or less, it’s writen for me -the author to rememeber main issues of python.

1. Scope and Namespace

def scope_test():
    def do_local():
        spam = "local spam"
    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"
    def do_global():
        global spam
        spam = "global spam"
    spam = "test spam"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)
scope_test()
print("In global scope:", spam)

The output is:

After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam

Explaination:

  1. Local variable always is always used inside local scope. In the example, local variable spam has value is local spam, when do_local() invoked, spam only created inside a function, it has no use outside.
  2. Non local variable has been declared and it affect within scope_test and only within scope_test.
  3. Global variable only used in global scope.
  4. In Python, a function can exist inside a function, in the example above, do_local insides scope_test.
  5. Functions make its own scope.

##2. Class

class Dog:

    kind = 'canine'         # class variable shared by all instances
    def __init__(self, name):
        self.name = name    # instance variable unique to each instance
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind                  # shared by all dogs
'canine'
>>> e.kind                  # shared by all dogs
'canine'
>>> d.name                  # unique to d
'Fido'
>>> e.name                  # unique to e
'Buddy'

Notes:

  1. Class variables shared by all instances, kind is class variable.
  2. Instance variables shared by each instance,name is instance variable.
  3. Function can be define outside class.
# Function defined outside the class
def f1(self, x, y):
    return min(x, x+y)

class C:
    f = f1
    def g(self):
        return 'hello world'
    h = g

3. Inheritence

class DerivedClassName(BaseClassName):
    <statement-1>
    .
    .
    .
    <statement-N>

class DerivedClassName(modname.BaseClassName):
    <statement-1>
    .
    .
    .
    <statement-N>

Call baseclass method, base class must be in global scope:

BaseClassName.methodname(self, arguments)

Check instance type:

isinstance(obj, int) #check if `obj` is instance of `int`
issubclass(bool, int) #check if `bool` is subclass of `int`

Git - cheatsheet

05/12/2014 cheatsheet

1. Untrack an already check-in directory or file

git rm -r --cached folder_name

-r : recursive –cached: files or directories are only deleted on the git’s index, not on local storage.

2. See all submodules

cat .gitmodules

3. Add a submodule

git submodule add url_of_submodule

4. Initialize all submodules

git submodule init

5. Reset stage to the last commit

git reset --hard HEAD

6.Clone repository with a particular folder name

git clone git@github.com:whatever folder-name

Run gnome-terminal and open new program

14/10/2014 Linux

This is script which open a new gnome-terminal window and automatically run, execute program on this gnome-terminal.

gnome-terminal  -x zsh -c "echo hello;pwd;whoami;zsh"

The workflow of this script is:

1. Open new gnome-terminal
2. execute command `echo hello`
3. execute command `pwd`
4. execute command `whoami`
5. execute command `zsh`

!!!NOTE: THE END OF EXECUTE SENTENCE SHOULD END WITH zsh

Making new table on an exist database - Android

18/07/2014 Android

When working on my assignment in course named Mobile Application. I had a problem that my program has two tables, both of them belong to a single database.

In my program, I used SQLiteHelper, the program has two diffirential classes which handle database working process.

1. GroupDaoImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public class TaskGroupImpl extends SQLiteOpenHelper implements TaskGroupDao{
    private static TaskGroupImpl instance = null;
    private Context context;
    private ArrayList<TaskGroup> groups;
    //DATABASE information
    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "TaskManager";
    private static final String TABLE_NAME = "task_group";

    //TABLE information
    private static final String GROUP_ID = "ID";
    private static final String GROUP_NAME = "NAME";


    private TaskGroupImpl(Context context){
       super(context, DB_NAME, null, DB_VERSION);
       this.context = context;
    }
    public static TaskGroupImpl getInstance(Context context){
        Log.v("TaskGroupImpl: ", "getInstance() active");
        if (instance == null){
            instance = new TaskGroupImpl(context);
            instance.groups = new ArrayList<TaskGroup>();
        }
        return instance;
    }
    public ArrayList<TaskGroup> getAllGroups(){
        return groups;
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String create_db = "CREATE TABLE " + TABLE_NAME+ "("
                + GROUP_ID + " INTEGER PRIMARY KEY, "
                + GROUP_NAME + " TEXT"  + ")";
        sqLiteDatabase.execSQL(create_db);
        Log.v("TaskGroup: ", "Did make TaskGroup table");
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
        onCreate(sqLiteDatabase);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //super.onDowngrade(db, oldVersion, newVersion);
    }

    @Override
    public int createGroup(TaskGroup taskGroup) {
        try {
            int maxID;
            if(groups.isEmpty() == true){
                maxID = 0;
                taskGroup.setID(maxID);
            } else {
                maxID = groups.get(groups.size() - 1).getID() + 1;
                taskGroup.setID(maxID);
            }
            groups.add(taskGroup);
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values= new ContentValues();
            values.put(GROUP_ID,taskGroup.getID());
            values.put(GROUP_NAME, taskGroup.getName());
            db.insert(TABLE_NAME, null, values);
            db.close();
            return 1;
        } catch (Exception ex){
            Log.v("TaskGroupImpl: ", "cannot create new group" );
            return 0;
        }
    }
    @Override
    public TaskGroup getTaskGroupById(int id) {
        for(int i = 0; i < groups.size(); i++){
            if (groups.get(id).getID() == id){
                return groups.get(id);
            }
        }
        return null;
    }
    @Override
    public int updateTaskGroup(TaskGroup taskGroup) {
        return 0;
    }
    @Override
    public int deleteTaskGroup(TaskGroup taskGroup) {
        try{

            SQLiteDatabase db = this.getWritableDatabase();
            db.delete(TABLE_NAME, GROUP_ID + " = ?", new String[] {String.valueOf(taskGroup.getID())});
            groups.remove(taskGroup);
            return 1;
        }
       catch (Exception ex){
           return 0;
       }
    }
    public void init(){
        groups.removeAll(groups);
        String query = "SELECT * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor  = db.rawQuery(query, null);

        if (cursor.moveToFirst()){
            do{
                TaskGroup group = new TaskGroup();
                group.setID(Integer.parseInt(cursor.getString(0)));
                group.setName(cursor.getString(1));
                groups.add(group);
            } while (cursor.moveToNext());
        }
    }
}

2. TaskDaoImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
public class TaskDaoImpl extends SQLiteOpenHelper implements TaskDao  {
    //singleton
    private static TaskDaoImpl instance = null;
    private Context context;

    private ArrayList<Task> tasks;
    private static final int DATABASE_VERSION = 1;
    private static final  String DATABASE_NAME = "TaskManager";
    private static final String TABLE_TASK = "task";


    //TABLE INFORMATION
    private static final String taskId = "ID";
    private static final String taskTitle= "TITLE";
    private static final String taskNote= "NOTE";
    private static final String taskDueDate = "DATE";
    private static final String taskPriorityLevel = "PRIORITY";
    private static final String taskCollaborators = "COLLABORATOR";
    private static final String taskStatus = "STATUS";
    private static final String taskGroupId = "GROUPID";
    private SQLiteDatabase mydatabase;



    private TaskDaoImpl(Context context){
        super(context,DATABASE_NAME, null,DATABASE_VERSION);
        this.context = context;
    }

    public static TaskDaoImpl getInstance(Context ctx){
        if(instance == null){
            instance = new TaskDaoImpl(ctx);
            instance.tasks = new ArrayList<Task>();
            instance.createTableIfNotExist();
            Log.v("TaskDaoImpl: ", "getInstance() active");

        }
        return instance;
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String create_db = "CREATE TABLE " + TABLE_TASK + "(" +
                taskId + " INTEGER PRIMARY KEY, "+
                taskTitle +" TEXT, "+
                taskNote + " TEXT, "+
                taskDueDate +" INTEGER, "+
                taskPriorityLevel +" INTEGER, "+
                taskCollaborators + " TEXT, "+
                taskStatus + " INTEGER, "+
                taskGroupId + " INTEGER"
                + ")";
        sqLiteDatabase.execSQL(create_db);
        Log.v("TaskDaoImpl", "Did create table " +TABLE_TASK);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
        onCreate(sqLiteDatabase);
    }

    public void createTableIfNotExist(){
        mydatabase = this.getWritableDatabase();
        String create_db = "CREATE TABLE IF NOT EXISTS " + TABLE_TASK + "(" +
                taskId + " INTEGER PRIMARY KEY, "+
                taskTitle +" TEXT, "+
                taskNote + " TEXT, "+
                taskDueDate +" INTEGER, "+
                taskPriorityLevel +" INTEGER, "+
                taskCollaborators + " TEXT, "+
                taskStatus + " INTEGER, "+
                taskGroupId + " INTEGER"
                + ")";
        mydatabase.execSQL(create_db);
    }
    @Override
    public ArrayList<Task> getAllTasks() {
        return tasks;
    }

    @Override
    public int createTask(Task task) {
        int maxID;
        if(tasks.isEmpty() == true){
            maxID = 0;
        } else {
            int id = tasks.get(tasks.size()-1).getId();
            maxID = id+1;
        }
        task.setId(maxID);
        tasks.add(task);
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(taskId, task.getId());
        values.put(taskTitle, task.getTitle());
        values.put(taskNote, task.getNote());
        values.put(taskDueDate, task.getDueDate().getTime()/1000);
        values.put(taskPriorityLevel, task.getPriorityLevel());

        //collaborator from arraylist to String
        StringBuilder stringB = new StringBuilder();
        for (int i = 0; i < task.getCollaborator().size(); i++){
            stringB.append(task.getCollaborator().get(i));
            if (i < task.getCollaborator().size() -1){
                stringB.append(",");
            }
        }
        values.put(taskCollaborators, stringB.toString());
        values.put(taskStatus, task.isCompletionStatus());
        values.put(taskGroupId,task.getGroupID());

        db.insert(TABLE_TASK,null,values);
        db.close();
        return 1;
    }
    @Override
    public Task getTaskByID(int id) {
        for(int i = 0;i < tasks.size(); i++){
            if(tasks.get(i).getId() == id ){
                return tasks.get(i);
            }
        }
        return null;
    }
    public ArrayList<Task> getTaskByGroupID(int id){
        ArrayList<Task> filteredList = new ArrayList<Task>();
        for (int i = 0; i <tasks.size(); i++){
            if(tasks.get(i).getGroupID() == id){
                filteredList.add(tasks.get(i));
            }
        }
        return filteredList;
    }
    public void deleteTaskByGroupID(int groupID){
        for(int x = 0; x < tasks.size();x++){
            if(tasks.get(x).getGroupID() == groupID){
                deleteTask(tasks.get(x));
                x--;
            }
        }
    }
    @Override
    public int updateTask(Task task) {
        for (int i = 0; i< tasks.size();i++){
            if(tasks.get(i).getId() == task.getId()){
                tasks.get(i).setCollaborator(task.getCollaborator());
                tasks.get(i).setCompletionStatus(task.isCompletionStatus());
                tasks.get(i).setDueDate(task.getDueDate());
                tasks.get(i).setPriorityLevel(task.getPriorityLevel());
                tasks.get(i).setTitle(task.getTitle());
                tasks.get(i).setNote(task.getNote());
                return 1;
            }
        }
        return 0;
    }
    @Override
    public int deleteTask(Task task) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();
        db.delete(TABLE_TASK, taskId + " = ?", new String[] {String.valueOf(task.getId())});
        db.setTransactionSuccessful();
        db.endTransaction();
        Log.v("TaskDaoImpl: ", task.getId() + "-"+task.getTitle());
        tasks.remove(task);
        db.close();

        return 0;
    }

    public void init(){
        tasks.removeAll(tasks);
        Log.v("TaskDaoImpl: " , "init method");
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT  * FROM " + TABLE_TASK;
        Cursor cursor = db.rawQuery(query, null);
        if(cursor.moveToFirst()){
            do{
                Task task = new Task();
                task.setId(Integer.parseInt(cursor.getString(0)));
                task.setTitle(cursor.getString(1));
                task.setNote(cursor.getString(2));
                Date datetime = new Date(Long.parseLong(cursor.getString(3)) * 1000);
                Log.v("datetime ", datetime.toString());
                task.setDueDate(datetime);

                task.setPriorityLevel(Integer.parseInt(cursor.getString(4)));
                //get collaborators
                String collaboratorsS = cursor.getString(5);
                ArrayList<String> colaList = new ArrayList<String>(Arrays.asList(collaboratorsS.split(",")));
                task.setCollaborator(colaList);
                //get status
                if(Integer.parseInt(cursor.getString(6)) == 0){
                    task.setCompletionStatus(false);
                } else {
                    task.setCompletionStatus(true);
                }
                //set group id
                task.setGroupID(Integer.parseInt(cursor.getString(7)));
                task.print();
                //add to array list - lists
                tasks.add(task);
            } while(cursor.moveToNext());
        }
    }
}

#ISSULE AND PROBLEM In general, in order to use SQLiteHelper to make new table and database. function named onCreate(SQLiteDatabase db) will be invoked to do that. In my program, TaskGroupImpl instance is initialized before TaskDaoImpl, bug happend here, when I make a new instance of TaskDaoImpl - ofcourse after TaskGroupImpl, table named task would not be created or function named onCreate(SQLiteDatabase db) is not invoked.

#Solution

I make an extra function to make new table manually. This function named createTableIfNotExist(), it belong to TaskDaoImpl class - because it has a new table which I want to make but not create automatically by SQLiteHelper.

When make call a instance of TaskDaoImpl, I also check to make a new table name task. If it is exist, make no new table, if not, make a new table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void createTableIfNotExist(){
        mydatabase = this.getWritableDatabase();
        String create_db = "CREATE TABLE IF NOT EXISTS " + TABLE_TASK + "(" +
                taskId + " INTEGER PRIMARY KEY, "+
                taskTitle +" TEXT, "+
                taskNote + " TEXT, "+
                taskDueDate +" INTEGER, "+
                taskPriorityLevel +" INTEGER, "+
                taskCollaborators + " TEXT, "+
                taskStatus + " INTEGER, "+
                taskGroupId + " INTEGER"
                + ")";
        mydatabase.execSQL(create_db);
    }

In the contructor, I call this function to make a new table.

1
2
3
4
5
6
7
8
9
10
ublic static TaskDaoImpl getInstance(Context ctx){
        if(instance == null){
            instance = new TaskDaoImpl(ctx);
            instance.tasks = new ArrayList<Task>();
            instance.createTableIfNotExist();
            Log.v("TaskDaoImpl: ", "getInstance() active");

        }
        return instance;
    }

How to compile executable jar file from source code

14/07/2014 etc

This thread will illustrate how to make a executable file.

#1.Create file “manifest.txt”

1
2
Main Class: package.to.my.ProgramLaucher
\n - 'this character will not be writen, I mean that you need to end with a new line'

for example, my class is:

1
2
3
4
5
6
package cosc2101.assignment1.controller;
public class ProgramLauncher {
    public static void main(String[] args) {
	     System.out.println("Hello World");
    }
}

then, my manifest file should be

1
2
Main Class: cosc2010.assignment1.controller.ProgramLaucher
next line here(compulsory)

#2. Making file .class This file is a compiled file of file.java. In order to make this file. Use the following command on the terminal.

javac .path/to/file.java

In this case, my output is a file named ProgramLaucher.class #3.Setting up folder for compile In order to compile java source code, you need to add file manifest.txt and compiled file file.class. For example in the case above, after make a new directory, our directory should be

------/
      manifest.txt
	  cosc2101/assignment1/controller/ProgrammerLaucher.class

#4. Make .jar file Within this directory

jar -cvfm ProgramLaucher.jar manifest.txt *.class

'c' refers to creating new archive file, in this case, it is
`ProgramLaucher.jar`
'v' refers to verbose, while processing, all relevant details will be report on
the terminal screen
'f' refers to specified file .jar will be created - creating option is assigned
by `c` but, what and where to create is assigned by `f`
'm' refers to manifest file

#5. Run file .jar Using the following command

java -jar ProgramLaucher.jar

Youtube video downloader for Conkeror

29/03/2014 Conkeror

I am now introducing a simple way to download video from Youtube by conkeror and an extra terminal program name cclive. This program take role to catch the link of youtube video and download it to assigned directory. In addition, you can choose type of video to download, of course, it also depends on the source.However, I am not going to further to that point.

What you need is: cclive, conkeror-spawn-helper Firstly, you need to install cclive. if you are using Fedora, you can install it by this simple command on the terminal.

sudo yum install cclive -y

Then, you open your conkeror file init.js, and append it with the following lines

//download video from youtube using cclive
interactive("download-video-2-music","Download current playing video.",
			function(I){
			  var path = I.buffer.current_uri.path;
			  path = path.substr(9,30);
   			  I.minibuffer.message("Downloading video to folder Music ");
			  shell_command_blind("cclive -d /home/nguyenvinhlinh/Music \"https://www.
			  							  youtube.com/watch?v=\""+ path);
			});

interactive("download-video-2-downloads","Download current playing video.",
			function(I){
			  var path = I.buffer.current_uri.path;
			  path = path.substr(9,30);
			  I.minibuffer.message("Downloading video to folder Downloads");
			  shell_command_blind("cclive -d /home/nguyenvinhlinh/Downloads \"https:
			  							  	 //www.youtube.com/watch?v=\""+ path);
			});

You can active these functions by M-x download-video-2-download, M-x download-video-2-music or assign these function to hot keys, for example:

define_key(default_global_keymap, "C-M", "download-video-2-music" );
define_key(default_global_keymap, "C-D", "download-video-2-downloads" );

Backspace issue with using google document by conkeror

23/03/2014 Conkeror

By default, conkeror users may not use backspace to delete previous characters when they do simple task in google document, perharp also for other google product. The solution is that you have to enable quote mode in conkeror

M-x quote-mode

Using command 'sudo' without password

18/03/2014 Linux

In the daily work, I may have to type sudo your_command and your password many times, it costs your time, in my case, it makes me feel crazy. The solution is that, you will add command which you mostly type into a list, then, when you want to type it with prefix sudo it wont ask you for password.

You have to modify file sudoers in /etc/sudoers. For example, you want to run command such as “yum”, “apachectl” without passwor

#1. Find the path of those commands by using command which in this case, those commands locates in /usr/sbin/apachectl and /usr/bin/yum.

#2. Modify file sudoers

username ALL=(ALL) NOPASSWD = /usr/sbin/apachectl, /usr/bin/yum

Now, you can run those command without password, REMEMBER, You still need prefix sudo

Command Alias, this feature will help you save your time if you want to assign group of command, for example, you have 2,3 user or group, it should be not convenient to type similar commands many times. Now, what you have to do is group them all, then use the command alias instead!

Cmmd_Alias CommandAliasName = /pathToCommand1, /pathToCommand2, /pathToCommand3
username1 ALL=(ALL) NOPASSWD = CommandAliasName
username2 ALL=(ALL) NOPASSWD = CommandAliasName