# use as standalone function def chunk_array(array, pieces=2) len = array.length; mid = (len/pieces) chunks = [] start = 0 1.upto(pieces) do |i| last = start+mid last = last-1 unless len%pieces >= i chunks << array[start..last] || [] start = last+1 end chunks end # use as array.chunk class Array def chunk(pieces=2) len = self.length; mid = (len/pieces) chunks = [] start = 0 1.upto(pieces) do |i| last = start+mid last = last-1 unless len%pieces >= i chunks << self[start..last] || [] start = last+1 end chunks end end
Chunked Array
10 Tips for completing a project
1. Imagine the Completed Product
Make it a prototype or image in your mind and hear the responses from your happy users in order to feel the good vibes. This will be good encouragement for ourself to start the finish end of the project. Prototyping a mock up in photoshop is also a good idea to get the image up to your mind. I feel strongly, this is important, though some of people think this is waste a time.
2. Don’t Procrastinate
Its difficult to do it than said it. Think about it as the times to relaxed which helped us produce the greatest creativity. Its when you know, feel and believe you should be getting on with the project but keep putting it off that you fall into a rut. You develop tension and end up resenting the whole product. If you feel tired or just need a break - then have one, but then get back to the task at hand. Take a break purpose is to run longer road.
3. Switch environment
One thing need to remember that coding is not the only thing in the project. It's involved planning, generating ideas, talking it through with your team. We need to be happy and create the happiness within your working environment. Try to create diversity of environment for your workspace, in term of people, desk, room, and also color. A change of environment whether it be your desk, monitor, office or even your IDE can have a massive impact on the way you feel and therefore your motivation. Remember, like what DHH said, Happy Programmer create good quality of project.
4. Talk to your Client.
Programmers can feel that if they find contact with other human beings is a frightening or disturbing process but there’s nothing that gets the passion moving by using the enthusiastic chat with the customer about the project. Getting better ideas and refresh the ideas again and again, thats the purpose.
5. Be Positive
No matter what, come rain or shine you have to remain positive. If you remain positive the others in your team will bounce off that and it’ll make them positive. Being surrounded by positive people will make you feel positive and give you a drive and sense of purpose in your whole outlook. Thinking positive means talking, walking, behaving, feeling, seeing, hearing nothing but positivity. A user criticizes you? Take it on the chin and sort it out. It will help the product and you grow. Getting stuck in a rut is all about losing sight of the final goal and losing faith in its fruition.
6. Release daily deadlines
Everyday, when you step into your office, do a short briefing and discussion with your team or within yourself, to get to list down todo list and achievement list within a day. Every line of code that programmer code, make sure it error free. The impact of buggy system can make the project testing phase is become longer than the project estimation itself.
7. Encourage Criticism
Many programmers don’t want their code to be criticized and if anyone does its put down to user or managerial stupidity. However, the more criticism you get the better the system. The better the system the happier the users. Don’t expect praise though, that programmer must act selflessly to take all suggestions, comment and critique then set about making the our skill as hard as a diamond stone. Yeah, this is the beginning of bullet proof concept. Be a bullet proof programmer.
8. Enjoy It!
If you don’t have any enthusiasm for a project from the outset you never will. If it fails to inspire you or get you into the groove then you’ll never make it a success. You have to have some connection or desire to do the project, even if only for a short time. If you’re in this position then maybe you’re in the wrong job?
9. Break the Project Up
If you look at the complexity of your project and feel daunted then its best to concentrate on the component parts. Complete them in a procedural manner and just keep it moving. Sometimes, its good to have your colleague to help you up for the module that not within your expertise. Learn from other people also one way to keep you up for the project motivation. Working in a team would be good, but you must make sure you understand and know every one strength and weaknesses for every scope of the project. Break it up to the right people to speed the project completion.
10. Last but not least, Reward Yourself.
However is it, we are still a being, a creature who live in this earth, we have habits, moods and learned behaviors. When you complete a certain task in the project reward yourself. Plan a reward for the completion of the project too - a new monitor, book, holiday, meal, mac book pro or anything you want but only give it to yourself at the completion of the project. That way you’ll be learning and training a habit of forward momentum and completion. Nothing will stop you once you’re habits are forward moving and active!
I would like to hear any comments, please advices and fill the gaps. There must be a room for improvement. Thanks.
Subversion, Mac and Rails
I really miss tortoise svn on Windows XP. Since i use Mac, i gotta get used to it with terminal. I source out some subversion client and plugin, no luck for it. Ok, lets run through what i found out. First of all to get subversion running on your mac, you need to install it first. Get the installation from Subversion Mac OS X or get the latest version of the source code and then compile it yourself (this link). Set the path in your .profile or .bash_profile:
# svn command located at /usr/local/bin/svn, so set the path in your profile: $ vi ~/.profile $ export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
Next, if you not comfortable with command line, below is the subversion clients availabe for mac:
- SCPlugin - Integration with Mac OS X Finder. Download .dmg file here.
- svnX - Recommended
- SmartSVN - Recommended (Commercial - US$69)
- rapidSVN - Multi-platform Subversion GUI
- ZigVersion - Not recommended. Still early phase of build. Maybe will mature in future.
Assemble of tortoise svn but it sometime crash my Finder. Not recommended for Intel Mac. Just refer to this link for the binary and source (not the official site, but it work). After getting the binary, copy SCFinderPlugin.plugin to ~/Library/Contextual Menu Items/ and relauch your finder by ⌘⌥⎋ choose Finder and relaunch. The subversion contextual menu (with right click) will show up.
A cool GUI client for Mac OS X. Even work with Tiger and Mac Intel. You need to put a little effort to understand how it work to foster your coding. I am using this, in fact.
Similarly to svnX with a simple interface and tree directory structure.
Site which help you to understand more about subversion in mac os x? Click here.
Note for rails developer: Assume you have read above article link, and have set up your own svn for your rails application. Few things you need to note down for rails application below: (do these!)
$ cd ~ $ svn checkout svn://(your svn server)/(your rails application directory) $ cd ~/(your rails application directory) $ svn remove log/* $ svn commit -m 'remove logs directory in the version control' $ svn propset svn:ignore "*.log" log/ $ svn update log/ $ svn commit -m 'in the repository also log files either' $ svn move config/database.yml config/database.example $ svn commit -m 'rename database.yml to database.example' $ svn propset svn:ignore "database.yml" config/ $ svn update config/ $ svn commit -m 'ignoring any commit to database.yml'
In the case of you need to get the source from svn without .svn folder (without svn linkage), do:
$ cd ~/(your svn working directory)
$ find . -name .svn -exec rm -rf {} \;
# OR #
$ cd ~/(your svn working directory)
$ svn export svn://(your svn server location)/(your application directory)
Both way serve some purpose. Try to find out yourself whats the hidden purpose.
Cheer!
Reference:
Full Ruby on Rails Installation on Mac Intel
Installing Ruby on Rails development platform, its a long process, which we need to do with our terminal, from download the source files itself to the installation part. Make it simple. Here, I just provided the installation script for Mac Intel platform since i am using it ;p and it proven. Just run a shell script file and run the whole installation. Download the file below:
Download Mac Intel Script Here
The Installation including:
- Ruby 1.8.4
- Rails Edge
- Gems
- FastCGI
- Lighttpd
- Image Magick (also as RMagick gem)
Those are the basic installation of so-called rails ready platform. For some cases you need to install your own gem by yourself.
After download rails-intel.tar.gz, just run the command:
$ tar zxf rails-intel.tar.gz
Then open up the script file to get better understanding how it work and all the installation pointing to /usr/local (you can change it, just change the PREFIX), Next, its better to set the path correctly for your sheel environment, normally the file to set will be .profile or .bash_login under your home folder:
export PATH="$PATH:$HOME/bin" export PATH="/usr/local/bin:/usr/local/sbin:$PATH" export MANPATH="/usr/local/man:$MANPATH"
Before you run the script, just better create a directory 'src' inside your home folder and run the script from '~/src/' as the starting point. Next, run the script:
$ sudo ./rails-intel.sh
Wait for the installation finishing, the last piece of installation process running is mysql installation. After mysql installation done, viola.. run the script below for mysql gem
# TODO Do this after MySQL is installed # NOTE: You may have to do "sudo gcc_select 3.3" first. sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql
Done!
Lighty for Rails
Today i will discuss about Lighttpd installation. I am making few assumptions:
- Your machine running on Linux or Unix platform, infact, there is no distribution of lighty in Windows platform.
- Localhost as your IP and no other services running on port 80.
- No RPM please. This guide support for all UNIX/Linux platform.
- Ruby installed and Rails installed.
Below are the step by step installation:
Run your terminal, through ssh or desktop also applicable ;p Firstly, get the distribution first.
cd /usr/local/src wget http://www.lighttpd.net/download/lighttpd-1.4.8.tar.gz tar zxvf lighttpd-1.4.8.tar.gz cd lighttpd-1.4.8
Make sure you put all your installation in "/usr/local/src", no doubt, just for tidy file management purposes. If you running on Mac OS X, there is no "wget" command, use below:
curl -O http://www.lighttpd.net/download/lighttpd-1.4.8.tar.gz tar zxvf lighttpd-1.4.8.tar.gz cd lighttpd-1.4.8
Next, start to configure:
./configure make sudo make install
Assume lighttpd already successful installed here.
Start configure the config file for running rails application and php. Below just the location where i put my setting file.
- Lighty's config file at /etc/lighttpd/lighttpd.conf
- Lighty's runtime pid file at /var/run/lighttpd.pid
- Lighty's access logs written to /var/log/lighttpd/access_log
- Lighty's error logs written to /var/log/lighttpd/error_log
- Rails application will be deployed to home_dir/rails_application_folder
- PHP aplication will be deployed to home_dir/public_html
In Linux, home_dir = /home and Mac OS X, home_dir = /Users
We will use virtual host for every application deployed. Before, i have created user chardy with configuration as apache group (just let it be under apache group, there is a purpose on that)
su - useradd -g apache -G apache chardy passwd chardy su - chardy mkdir public_html rails depot
We need to install PHP first in order to run PHP application. Just follow the steps:
su - cd /usr/local/src/ wget http://sg2.php.net/get/php-5.1.1.tar.gz/from/sg.php.net/mirror tar zxvf php-5.1.1.tar.gz cd php-5.1.1 ./configure --enable-fastcgi \ --enable-force-cgi-redirect make make install
Tips: for most common support php module, do below configuration.
./configure --prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/usr/com --mandir=/usr/share/man --infodir=/usr/share/info --with-libdir=lib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-force-cgi-redirect --disable-debug --enable-pic --disable-rpath --enable-inline-optimization --with-bz2 --with-db4=/usr --with-exec-dir=/usr/bin --with-freetype-dir=/usr --with-png-dir=/usr --enable-gd-native-ttf --with-gettext --with-gmp --with-iconv --with-jpeg-dir=/usr --with-openssl --with-png --with-expat-dir=/usr --with-pcre-regex --with-zlib --with-layout=GNU --enable-bcmath --enable-exif --enable-ftp --enable-magic-quotes --disable-safe-mode --enable-sockets --enable-sysvsem --enable-sysvshm --enable-track-vars --enable-trans-sid --enable-yp --enable-wddx --with-pear=/usr/share/pear --with-kerberos --enable-ucd-snmp-hack --enable-memory-limit --enable-shmop --enable-calendar --enable-dbx --enable-dio --enable-soap --with-mime-magic=/etc/httpd/conf/magic --without-sqlite --with-libxml-dir=/usr --with-xml --with-mysql --with-gd --with-dom --with-dom-exslt --enable-fastcgi --enable-force-cgi-redirect
To test PHP5 whether is running correctly:
php -v PHP 5.1.1 (cgi-fcgi) (built: Jan 3 2006 23:48:02) Copyright (c) 1997-2005 The PHP Group Zend Engine v2.1.0, Copyright (c) 1998-2005 Zend Technologies
It will show up PHP 5.1.1 (cgi-fcgi). Next, configure your lighttpd.conf
su - mkdir /etc/lighttpd/ cd /usr/local/src/lighttpd-1.4.8 cp doc/lighttpd.conf /etc/lighttpd/ vi /etc/lighttpd/lighttpd.conf
Edit the lighttpd.conf to satisfy few lines below first:
server.modules = ("mod_rewrite", "mod_accesslog", "mod_fastcgi")
server.port = 80
server.bind = "localhost"
# server.username = "apache"
# server.groupname = "apache"
# server username and groupname is not necessary, if not set, all will ignored
server.pid-file = "/var/run/lighttpd.pid"
accesslog.filename = "/var/log/lighttpd/access_log"
server.errorlog = "/var/log/lighttpd/error_log"
server.indexfiles = ( "index.html" )
url.rewrite = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" )
server.document-root = "/Library/Rails/" + var.appname + "/current/public"
server.error-handler-404 = "/dispatch.fcgi"
Below is the sample configuration to be added for rails configuration:
#### depot.spiragram.com ####
$HTTP["host"] =~ "depot.spiragram.com" {
server.document-root = "/home/chardy/depot/public/"
server.errorlog = "/home/chardy/log/lighttpd_error.log"
accesslog.filename = "/home/chardy/log/lighttpd_access.log"
server.error-handler-404 = "/dispatch.fcgi"
server.error-handler-500 = "/500.html"
fastcgi.server = ( ".fcgi" =>
("depot.spiragram.com" =>
( "socket" => "/tmp/lighttpd-fcgi.socket",
"bin-path" => "/home/chardy/depot/public/dispatch.fcgi",
"bin-environment" => ("RAILS_ENV" => "production"),
"min-procs" => 1,
"max-procs" => 5,
"idle-timeout" => 120
)
)
)
}
## end subdomain entries
For PHP configuration below:
$HTTP["host"] =~ "phpsite.spiragram.com" {
server.document-root = "/home/chardy/public_html/"
server.errorlog = "/home/chardy/log/lighttpd_error.log"
accesslog.filename = "/home/chardy/log/lighttpd_access.log"
server.indexfiles = ( "index.php", "index.html" )
fastcgi.server = (".php" => (("bin-path" => "/usr/bin/php",
"socket" => "/tmp/phpsite.socket",
"max-procs" => 1,
"bin-environment" => ("PHP_FCGI_CHILDREN" => "1",
"PHP_FCGI_MAX_REQUESTS" => "5000"
),
"bin-copy-environment" => ("PATH", "SHELL", "USER" ),
"broken-scriptfilename" => "enable"
))
)
}
Run lighttpd server by typing
lighttpd -f /etc/lighttpd/lighttpd.conf
To stop lighttpd server
killall lighttpd
lazy way ;p
To check your installation is running or not, just point it to below url (example):
http://depot.spiragram.com - rails application
http://phpsite.spiragram.com/index.php - php application. To test php application, just create th index.php
su - chardy cd public_html echo '' > index.php
You can see rails page and php info page after you access site provided according to your configuration of v-host.