Sunday, August 24, 2008

Locale in linux/unix

(1) Get locale information
Use command locale.

locale    //get current locale environment for each locale category defined by the LC_* environment variables.
locale -a //output names of all available locales
locale -m //Write names of available charmaps.
locale -k LC_CTYPE //Write names and values of selected keywords (In this case, it is "LC_CTYPE").

command: localedef
"The  localedef  program  reads  the  indicated  charmap  and input files, compiles them to a form usable by the locale(7) functions in the C library, and places the six output files in the outputpath directory."

When a program looks up locale dependent values, it does this according to the following environment variables, in priority order:

  1. LANGUAGE
  2. LC_ALL
  3. LC_xxx, according to selected locale category: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES, ...
  4. LANG

Variables whose value is set but is empty are ignored in this lookup.
Set locale environment variable (E.g. LC_ALL, LANG...):
ll_cc.encoding  E.g. de_DE.UTF-8, zh_CN.UTF-8
ll_cc@variant  E.g. de_DE@euro, sr_RS@latin
"There is also a special locale, called ‘C’. When it is used, it disables all localization: in this locale, all programs standardized by POSIX use English messages and an unspecified character encoding (often US-ASCII, but sometimes also ISO-8859-1 or UTF-8, depending on the operating system)."
http://www.gnu.org/software/gettext/manual/gettext.html#Setting-the-POSIX-Locale

C lib:
char *setlocale(int category, const char *locale): set the current locale.

Ruby notes-basic(1)

Semicolons and newline characters are interpreted as ending of a statement. +, = or \ can be use as line continuation characters.
Comments
Single-line comments: Comments extend from # to the end of a line.
Multi-line comments:

=begin
comments go here
=end
Note: '=begin' and '=end' must appear at the beginning of a line.
Integer literals: 123, 0123(octal), 0xf34(hex), 0b1123(binary), ?d(char code for 'd')
Float-point literals: 23.45, 3e4, 4E10, 5e+39
String literals:
double quotations: allow substitution and escape character sequences.
single quotations: don't allow substitution and escape character sequences except \\ and \'.
Adjacent strings are concatenated automatically. "abc""def" => "abcdef"
Command execution: `command`. The syntax is similar to the counterpart of bash. It allows substituion and escape character sequences. The output generated by execution of the command is converted to a string.
here documents:

<<EOF
content goes here
EOF
The string is in double quotations.
<<"EOF"
content goes here
EOF
<<'EOF'
content goes here
EOF
The string is in single quotations.
<<`EOF`
command goes here
EOF
The string is in back quotes.
<<-EOF
command goes here
EOF
The delimiter ('EOF' in this example) needs not be at the beginning of a line.

Symbols:
"A symbol is an object corresponding to an identifier or variable"
:name    #symbol for 'name'
:$name   #symbol for variable 'name'
Array
[], [1,2,3], [1,[2,3]]
%w(str1 str2 str3) => ["str1", "str2", "str3"]
Dictionary(Map, or Hash):
{key => value}
Regular expression:
/pattern/
/pattern/options

Alternatives:
%!string! and %Q!string! are equivalent to "string".
%q!string! is equivalent to 'string'
%x!command! is equivalent to `command`.
%r!pattern!

Variable

Type Description Example Note
global variables can be accessed globally by and program. $var Uninitialized global variables are set to nil.
instance variables that belong to an object. @var
class variables that belong to a class. @@var Class variables must be initialized before they are accessed in methods.
contants Constants as you know in other languages Var must begin with an upper case letter.May not be defined in methods.
local Local variables. var must begin with a lowercase letter or _

Variable substitutions: #{varname}. E.g. #{filename}, #{@conf}, #{$stdin} 
Variable assignments
var = value
var1, var2, ..., *varn = expr1, expr2, ..., *exprn
Operators:

expr?expr1:expr2
    The same as the tertiary operator in C/C++.
defined? varname
    #return a description about the variable/method or nil.

Control statements

Name Syntax Alternatives
if if cond [then]
    code
[elsif cond [then]
    code]
[else
    code]
end
code if cond
unless unless cond [then]
    code
[else
    code]
end
code unless cond
case case expr
[when expr [,expr ...] [then]
    code ]...
[else
    code]
end
 
while while cond [do]
    code
end
(1)code while cond
(2)begin code end while cond.
Note: code is executed once before cond is evaluated.
until until cond [do]
    code
end
(1) code until cond
(2) begin code end until cond
Note: code is executed once before cond is evaluated.
for for var[, var2 ...] in expr [do]
    code
end
expr do |var [,var...]|
    code
end
break Break from look  
next like continue in C++  
redo re-execute the loop body once without evaluation of conditional expression.  
retry re-execute a call to a method  
begin begin
    code
[rescue [exception_class [, excep_cls...]] [=>var] [then]
    code ]...
[else
    code ]
[ensure
    code ]
end
Code in ensure clause is always executed.
Code in else clause is executed only when no exceptions are raised.
Code in rescue clause is executed when exception is caught.
rescue code rescue expr Evaluate the expr only if an exception is caught.
raise raise exception_class, message
raise exception_object
raise message
raise
Raise an exception.
To call raise in a rescue clause will re-raise the exception.
BEGIN BEGIN{
    code
}
Code to be executed before the program is run.
END END{
    code
}
Code to be executed when the interpreter quits.