A Perl programmer's understanding of Ruby symbol

Symbol is special data structure in Ruby and used widely in Rails. As a Perl programmer and Ruby/Rails learner, this new concept made me feel confused quite a while. So let’s sort it out now.

What is Symbol in Ruby

A symbol looks like a variable name with a prefixed colon, like :action, :items. It is the most basic Ruby object you can create. It’s just a name and an internal ID. Symbols are useful because a given symbol name refers to the same object throughout a Ruby program.

What’s the difference between Symbols and Strings

Symbols are more efficient than Strings. Everything in Ruby is object so two strings with the same contents are two different objects, but for any given name there is only one Symbol object. This can save both time and memory.

Using Symbols as Hash Keys

Due to the advantage of Symbols, it’s a good idea to use Symbols instead of Strings as hash keys in Ruby. The following 2 examples are using Strings and Symbols as hash keys respectively.

user1 = { "name" => "Jeremy Guan", "email" => "example@example.com" }

user2 = { :name => "Jeremy Guan", :email => "example@example.com" }

References

How to Fix 'Got a packet bigger than max_allowed_packet bytes' MySQL Error

When I tried to import data from a dump file into a new MySQL database, I got “Got a packet bigger than ‘max_allowed_packet’ bytes” error. Apparently ‘max_allowed_packet’ needs to be increased and I don’t have the change permanently. To fix that, just run the following commands in MySQL console:

set global net_buffer_length=9999999; 
set global max_allowed_packet=9999999999;

Or replace the values with some very large numbers work for you. Don’t quit the MySQL console yet and run the importing command on another terminal. The problem should be gone now.

Examples of Perl progress bar

#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
my $max = 10;
# 1. dots
for (1..$max) {
	print ".";
	print " Complete!\n" if ($_ == $max);
	sleep(1);
}
# output: " .......... Complete!"
#
# 2. simple percentage
for(1..$max) {
	my $percent = $_/$max*100;
	print "\r $percent %";
	sleep(1);
}
print "\n";  
# output: " 100 %"
#
$max = 100;
#
# 3. spinning bar
my @progress_symbol = ('-','\\','|','/');
my $n = 0;
for (my $i=1; $i<=$max; $i++){
	print "\r $progress_symbol[$n] $i %";
	$n = ($n>=3) ? 0 : $n+1;
	select(undef, undef, undef, 0.1);
}
print "\n";
# output: " / 100 %"
#
# 4. dynamic progress indicator 
for (my $i=1; $i<=$max; $i++){
	proc_bar($i,$max);
	select(undef, undef, undef, 0.2);
}
print "\n";
sub proc_bar{
	local $| = 1;
	my $i = $_[0] || return 0;
	my $n = $_[1] || return 0;
	print "\r\033[36m[\033[33m".("|" x int(($i/$n)*50)).
		(" " x (50 - int(($i/$n)*50)))."\033[36m]";
	printf("%2.1f%%\033[0m",$i/$n*100);
	local $| = 0;
}
# output: "[||||||||||||||||||||||||||||||||||||||||||||||||||]100.0%"

Convert CSV to SQL insert query

Recently I need to import data from CSV files into databases frequently. This post just records the process of conversion I’m using in Vim.

Assuming the CSV file is:

1, A , a
1, B , b
1, C , c

On each line, clean up all the spaces preceding and following the commas “,”.
:%s/\s*,\s*/,/g

Replace the beginning of each line by “(“.
:%s/^/(

Replace the end of each line by “)”.
:%s/$/),

Add double quotes (“) to text fields.
:%s/\(.*\),\(.*\),\(.*\)/\1,"\2","\3"

Or we can combine last three steps into one command.
:%s/\(.*\),\(.*\),\(.*\)/(\1,"\2","\3"),

At last, complete the INSERT statement and don’t forget to replace the last comma with semicolon “;”.

INSERT INTO table_name VALUES
(1,"A","a"),
(1,"B","b"),
(1,"C","c");
« Previous Page Next Page »