Total Pageviews

Friday, October 25, 2013

Ruby notes from Javascript coder

  • Symbols begins with :
  • function call do not need (), just mention it.
  • If you want to get the function object instead of the result of calling it, use method(:function_name), obj.method(:method_name) or class.instance_method(:method_name).
  • no curly braces, use `end` key word.
  • string enclosed by "" can be perform fancy variable substitute; with '', no substitute.
  • substitute: #{variable_name}, #{variable_name.method}
  • Inside class, @prop means `this.prop`
  • To define class properties: attr_accessor, attr_reader, atter_writer
  • Result of the last express in a method definition will be the return value.
  • Can define constants by using upper case letter as variable name. It is still mutable, but the interpreter will complain.
  • By convention, methods end with ? are predicates, they all return a boolean value.
  • By convention, methods end with ! would perform some mutation to the object.
    • list.sort => return a new list
    • list.sort! => sort the list in place
  • Variable naming and their scope:
    • begin with $ => global
    • begin with @ => instance variable
    • begin with @@ => class variable
    • begin with [a-z_] => local
    • begin with [A-Z] => constant
  • Special global variables: $@, $_, $., ...,
  • Function arguments:
    • variable length of arguments, (*args), then args will be a Enumerable
    • default value def method(a=1, b)
  • Operate on collection (Enumerable class):, mapping from Javascript Array methods:
    • filter <=> select
    • reduce <=> reduce, inject
    • map <=> map, collect
    • every <=> all?
    • some <=> any?
    • flat_map == collect_concat
  • Lambda expression:
    • function(x){return x*2;} => {|x| x*2}, do |x| x*2 end
  • Ranges:
    • (1..10) , double dot, includes 10
    • (1...10), triple dot, does not include 10
  • JSON like hash syntax works on later version of ruby. But the original syntax looks like:
    • my_hash = { :a => 1, "b" => 2, 3 => 3 }
  • Array decomposition can be used to pull out values from a array and assign them to variables:
    • a, (b, *c), *d = 1, [2, 3, 4], 5, 6
  • Support modifier form for if, unless, while, until
    • a += 1 if
    • a += 1 while a < 10
    • a += 1 until a > 10
    • begin a+= 1 end while a < 10
  • Ruby use `next` key word instead of `continue`
  • Regex:
    • create syntax => same as Javascript:  /[0-9a-z]/i
    • re.test( 'some string' ) <=> re === 'some string'

Tuesday, June 11, 2013

Download all pdf links from ASCE search page

- Search the articles by key word
- Get a web page that list all the pdf links
- Inspect the pdf link element, looks like this:
    <a class="ref nowrap" target="_blank" title="Opens new window" href="/doi/pdf/10.1061/%28ASCE%290733-9488%282001%29127%3A3%28118%29">PDF (1569 KB)</a>
- Open browser console, use jQuery select the pdf links based on its class/text/or other features
    pdfLinks = [];
    $('.ref:contains("PDF")').each( function() {  pdfLinks.push( $(this).attr( 'href' ); ) } ) ;
    pdfLinks.join( '\n' )
- Copy the output text from browser console, and save it to a text file list.txt.
- Use wget to get all the links:
    cat list.txt | xargs -I{} wget{}

Thursday, January 24, 2013

Install WordPress on linux server

Here are the instructions for setting up your own WordPress server. In this example, the server runs CentOS 6.3. Before you get started, switch to root user first, since most of the instructions below require a root privilege.
  1. Install related softwares: yum install httpd mysql-server mysql php php-mysql
  2. Start httpd and mysql: service start httpd service start mysqld Now open http://localhost, you should see a test page that indicates the server is working.
  3. Set up mysql server: Use mysqladmin utility to do the job: mysqladmin -u root -p password 123456 Then a prompt 'Enter password:' should appear, just hit enter since the default root user has no password. After this step, the mysql server has been setup. There is a user 'root' with the password 123456 (of course, you can change it).
  4. Install database manage tool phpMyAdmin: This tool provide a web gui interface to manage the mysql database.
    • Download the latest version of phpMyAdmin and put into the document root of Apache server(usually is /var/www/html): cd /var/www/html wget '!md5!3f77e1a608939224a7dce97caf860f46'
    • Uncompress the file to phpmyadmin folder and remove the downloaded file: tar xvf phpMyAdmin-3.5.5-all-languages.tar.bz2 mv phpMyAdmin-3.5.5-all-languages phpmyadmin rm -rf phpMyAdmin-3.5.5-all-languages.tar.bz2
    • Now open http://localhost/phpmyadmin in the browser, you should see a login interface. Login with username root and password 123456 (configured in step (3))
  5. Create a new user and database for wordpress: In phpMyAdmin page:
    • Login phpMyAdmin as root.
    • In phpMyAdmin, click 'Users' tab on home page.
    • Click 'Add User'
    • In 'Add User' form, fill in the user information.
    • User name: choose 'User text field', fill in 'wordpress'
    • Host: choose 'Local'
    • Hit generate password, copy the generated password to somewhere else for future use. In this example, the password is jcqu9LH5C82h238c
    • Under 'Database for user' tab, choose second option: 'Create database with same name and grant all privileges'
    • Hit 'Add User', now the user 'wordpress' and database 'wordpress' is added to the mysql database.
  6. Install wordpress:
    1. Download and uncompress the lastest version of wordpress into document root:cd /var/www/html wget tar xfv latest.tar.gz wordpress rm -rf latest.tar.gz Now a folder name 'wordpress' is placed under the document root.
    2. Create the config file by copying the sample file: cd wordpress cp wp-config-sample.php wp-config.php
    3. Configure database:
      • Use your favourite text editor, open wp-config.php file: emacs ./wp-config.php
      • Replace the corresponding lines in the file with your own database settings: define('DB_NAME', 'wordpress'); define('DB_USER', 'wordpress'); define('DB_PASSWORD', 'jcqu9LH5C82h238c');
      • Save the file
    4. Web interface install:
      1. Launch http://localhost/wordpress, now you should see a welcome page of wordpress.
      2. Follow the instruction in wordpress install page, choose site title, i.e., set up admin password, fill in your email address, then hit 'Install Wordpress'.
      3. If everything above goes well, it will redirect to a page that tells you WordPress has been successfully installed. Now you can log in the wordpress as 'admin' and configure you website via WordPress web interface.
  7. Set up proper folder permissions: Usually you need setup a ftp server to manage plugins. But it can be configured in such a way that the plugins install directly into /wp-content/plugins folder, which is easier. To do so:
    • Add a line in wp-config.php: define('FS_METHOD', 'direct');
    • Change the owner of /wp-content folder to apache server: chown -R apache:apache wp-content