tag:blogger.com,1999:blog-11106410979537482922024-02-18T18:47:35.066-08:00The Passionate CraftsmanRuby, PHP, MySql, Software engineering and more.Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.comBlogger95125tag:blogger.com,1999:blog-1110641097953748292.post-7643984497644565682013-03-11T09:15:00.000-07:002013-03-11T09:15:00.888-07:00How to list unmerged files during a GIT conflictAdd this to your list of aliases:<br />
<br />
alias gunmerged='git diff --name-only --diff-filter=U'<br />
<br />
gunmerged will show you the unmerged files.Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-26497300169982690352012-12-15T01:27:00.002-08:002012-12-15T01:35:40.874-08:00Raspberry PiToday I received my Raspberry Pi! I bought this book: <a href="http://www.amazon.co.uk/gp/search/ref=as_li_qf_sp_sr_tl?ie=UTF8&camp=1634&creative=6738&index=aps&keywords=111846446X&linkCode=ur2&tag=httpriccardot-21" target="_blank">Raspberry Pi User Guide</a><img alt="" border="0" height="1" src="http://www.assoc-amazon.co.uk/e/ir?t=httpriccardot-21&l=ur2&o=2" style="border: none !important; margin: 0px !important;" width="1" />. It is short, simple and a practical book. Most of the book is written for Linux newbies but the second part has an expalantion on how ot use the GPIO. I realised I can power the board with an iPhone power supply but I need a micro USB to deliver the power... I am using my Kindle USB cable! The box wrapping the Pi could be used as a 'PC case', but I will have to drill it to connect the cables. I downloaded this program to flash the 2GB SD card: <a href="http://alltheware.wordpress.com/2012/12/11/easiest-way-sd-card-setup/">http://alltheware.wordpress.com/2012/12/11/easiest-way-sd-card-setup/</a>. The Linux distro to be flashed in the card is available on the Pi web site, I download the Debian image but I think I will try the Arch Linux version too. At the initial boot I set up the keyboard, local time and other settings, the configuration menu is very simple. The Windows manager is lightweight and powerful, I love it. I am having an overall good impression.<br />
<br />
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-TAQoyukiXmyNm6R0OYDgPFflOVNZqoiCZzWIwEl1X6wr_uPweQtPci6VJjdmh9p5NuE-8EyIbMVeHTcgGQB-W0GcZc8W2vTgD81pvLr9PSfQlAp3TD3iQA4uUbz-pXcHOJdohIccp2iB/s1600/photo.JPG" />Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-26264186638043667922012-12-02T06:28:00.001-08:002013-01-16T04:18:51.969-08:00Abstract classes in RubyRuby is not a traditional OOP language like Java and C++. An abstract class provides and interface for a programmer, the implementation of some generic methods (useful for subclasses) and abstract methods (to be implemented by subclasses). Usually with Ruby you use mixins, but sometimes you need to provide a class with some generic methods and some stub methods to be implemented by more specialised subclasses; the template pattern just does this. In a Ruby class you just can do this:<br />
<br />
def create_html_header<br />
raise "You cannot call this abstract method"<br />
end<br />
<br />
This will block a programmer to use a method that should be implemented in a subclass, a class specialised in HTML generation. If you have many methods like that one, you could use a module to reduce the amount of methods with a raise statement. One solution could be to declare the abstract methods in this way:<br />
<br />
class AClass<br />
abstract_methods :foo, :bar<br />
end<br />
<br />
A solution to block a user to instantiate a class is:<br />
<br />
module Abstract<br />
def self.append_features(klass)<br />
# access an object's copy of its class's methods & such<br />
metaclass = lambda { |obj| class << obj; self ; end }<br />
<br />
metaclass[klass].instance_eval do<br />
old_new = instance_method(:new)<br />
undef_method :new<br />
<br />
define_method(:inherited) do |subklass|<br />
metaclass[subklass].instance_eval do<br />
define_method(:new, old_new)<br />
end<br />
end<br />
end<br />
end<br />
end<br />
<br />
class A<br />
include Abstract<br />
end<br />
class B < A<br />
end<br />
<br />
B.new #=> #<b:0x24ea0></b:0x24ea0><br />
A.new # raises #<nomethoderror: a:class="a:class" for="for" method="method" new="new" undefined="undefined"></nomethoderror:><br />
<br />
So if you just include 'include Abstract' you will prevent that class to be instantiated. For more information read this StackOverflow question: <a href="http://stackoverflow.com/questions/512466/how-to-implement-an-abstract-class-in-ruby">http://stackoverflow.com/questions/512466/how-to-implement-an-abstract-class-in-ruby</a>. The template pattern is explained here: <a href="http://designpatternsinruby.com/">http://designpatternsinruby.com/</a>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com2tag:blogger.com,1999:blog-1110641097953748292.post-64368007496460326142012-12-01T07:53:00.001-08:002012-12-01T07:53:14.258-08:00PHP and Ruby contractor looking for new projectsI have been working with PHP for many years, around eight and now I have more than 3 years of Ruby experience. If you want to build a web application, or you need a Linux system administrator, send me an email at rtacconi [at] virtuelogic.net.<br />
<br />
My business web site is <a href="http://virtuelogic.net/">virtuelogic.net</a>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-61662400699721912242012-12-01T07:48:00.001-08:002012-12-01T07:54:11.450-08:00Write code that you need now<blockquote class="tr_bq">
Predictions are hard, especially about the future.</blockquote>
Yogi Berra<br />
<br />
This aforism tells us that we should write the code for what we need now. If we write a web site with internationalization support we are betting that we will need a web site to support more than one language, but if we want to develop a site only in English we are wasting time. If is possible to add features later. This does not means that we should write software with tightly-coupled objects and with an inflexible design, but we should find a trade-off between pragmatism and flexibility.<br />
<br />
If you are interested in this topic, read pace 14 of "Design Patterns in Ruby" by Russ Olsen.Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-49447334884909523722012-11-16T11:18:00.001-08:002012-11-18T02:21:22.665-08:00Show hostname in MySql with a queryFirst way:<br />
<br />
mysql> show variables like 'hostname';<br />
+---------------+------------------------+<br />
| Variable_name | Value |<br />
+---------------+------------------------+<br />
| hostname | domU-12-31-39-16-D6-7C |<br />
+---------------+------------------------+<br />
1 row in set (0.00 sec)<br />
<br />
Second way:<br />
<br />
<br />
mysql> SELECT @@hostname;<br />
+-----------------------------+<br />
| @@hostname |<br />
+-----------------------------+<br />
| Riccardos-MacBook-Pro.local |<br />
+-----------------------------+<br />
1 row in set (0.00 sec)<br />
<br />
There are probably more ways to show the hostname, but two are more than enough for me.<br />
Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-16725308649387988042012-07-18T01:47:00.002-07:002012-07-18T02:19:27.259-07:00Install zemomq and Mngrel2 on Debian Squeeze<br />
# as root<br />
apt-get install uuid-dev<br />
apt-get install build-essential<br />
wget http://download.zeromq.org/zeromq-2.2.0.tar.gz<br />
tar -xzvf zeromq-2.2.0.tar.gz<br />
cd zeromq-2.2.0/<br />
./configure<br />
make<br />
sudo make install<br />
ldconfig<br />
<br />
apt-get install sqlite3 libsqlite3-dev<br />
<br />
apt-get install sudo<br />
wget http://mongrel2.org/static/downloads/mongrel2-1.7.5.tar.bz2<br />
tar -xjvf mongrel2-1.7.5.tar.bz2<br />
cd mongrel2-1.7.5/<br />
make clean all && sudo make install<br />
<br />
adduser mongrel<br />
cd ..<br />
mv mongrel2-1.7.5/ /home/mongrel/<br />
cd /home/mongrel<br />
chown -R mongrel mongrel2-1.7.5/<br />
cd mongrel2-1.7.5/<br />
<br />
# log out and login as mongrel user<br />
cd mongrel2-1.7.5/<br />
cp examples/configs/sample.conf mysite.conf<br />
m2sh load -config mysite.conf<br />
ls config.sqlite<br />
mkdir run logs tmp<br />
m2sh start -host localhost<br />
<br />
# access this url to test it http://your_hostname:6767/tests/sample.html<br />
<br />
<pre style="-webkit-box-shadow: rgba(0, 0, 0, 0.0742188) 0px 2px 3px; background-color: #fafafa; border-bottom-left-radius: 2px; border-bottom-right-radius: 2px; border-color: rgb(227, 227, 227) rgb(212, 212, 212) rgb(202, 202, 202); border-style: solid; border-top-left-radius: 2px; border-top-right-radius: 2px; border-width: 1px; box-shadow: rgba(0, 0, 0, 0.0742188) 0px 2px 3px; font-family: 'Courier New', monospace; font: inherit; line-height: 18px; margin-bottom: 18px; padding: 5px; vertical-align: baseline;">curl http://<span style="background-color: white; font-family: Times; line-height: normal; white-space: normal;">your_hostname</span>:6767/tests/sample.html
hi there</pre>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-67956245032162742332012-06-15T10:05:00.001-07:002012-06-15T10:25:50.070-07:00How to install G-WAN on Ubuntu 10.04 32-bit<span class="Apple-style-span" style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 11px; line-height: 14px;"></span><br />
<pre style="font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font: normal normal normal 12px/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><div class="line" id="LC1" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;">
sudo apt-get update</div>
<div class="line" id="LC2" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;">
sudo apt-get install -y sqlite3 libsqlite3-dev # to connect to sqlie3 and it is required</div>
<div class="line" id="LC3" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;">
sudo apt-get install -y libdbi0-dev libdbd-mysql # to connect to mysql - not required</div>
<div class="line" id="LC4" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;">
wget http://gwan.ch/archives/gwan_linux32-bit.tar.bz2</div>
<div class="line" id="LC5" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;">
tar -xjf gwan_linux32-bit.tar.bz2</div>
<div class="line" id="LC6" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;">
<span class="nb" style="color: #0086b3; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">cd </span>gwan_linux32-bit</div>
<div class="line" id="LC7" style="line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 1em; padding-right: 0px; padding-top: 0px;">
./gwan <span class="c" style="color: #999988; font-style: italic; line-height: 1.4em; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"># start g-wan</span></div>
</pre>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-26692281953655619382012-02-04T07:11:00.001-08:002012-08-07T14:43:45.043-07:00G-WAN World's fastest web server for C scriptsUPDATE: The forum of the web site has been closed few months ago. There is not any active community and actually there are very few people supporting the idea that G-WAN is very fast.<br />
<br />
After my <a href="http://riccardotacconi.blogspot.com/2012/02/cgi-script-with-apache-and-c.html">previous post</a> on CGI, I continued to search for a FastCGI example written in C, for the Mac and nginx. There is a post about it, but it was not working. However I found an awesome project, G-WAN, a web server built in ANSI C, super fast, at least from the charts reported on the web site. Another fantastic feature that G-WAN has, is the possibility to write C scripts, yes scripts! You put your scripts in a directory and when the script is called from the browser it is compiled and served. G-WAN can serve hundreds of thousands of connections per second but the response time is much better than any other language and application server.<br />
<br />
This is a Hello World! example, the code does not has to be compiled:<br />
<br />
<pre><span class="punct">/</span><span class="regex"></span><span class="punct">/</span> <span class="punct">============================================================================</span>
<span class="punct">/</span><span class="regex"></span><span class="punct">/</span> <span class="constant">C</span> <span class="ident">servlet</span> <span class="ident">sample</span> <span class="keyword">for</span> <span class="ident">the</span> <span class="constant">G</span><span class="punct">-</span><span class="constant">WAN</span> <span class="constant">Web</span> <span class="constant">Application</span> <span class="constant">Server</span> <span class="punct">(</span><span class="ident">http</span><span class="punct">:/</span><span class="regex"></span><span class="punct">/</span><span class="ident">trustleap</span><span class="punct">.</span><span class="ident">ch</span><span class="punct">/)</span>
<span class="punct">/</span><span class="regex"></span><span class="punct">/</span> <span class="punct">----------------------------------------------------------------------------</span>
<span class="punct">/</span><span class="regex"></span><span class="punct">/</span> <span class="ident">hello</span><span class="punct">.</span><span class="ident">c</span><span class="punct">:</span> <span class="ident">just</span> <span class="ident">used</span> <span class="ident">with</span> <span class="constant">Lighty</span><span class="punct">'</span><span class="string">s Weighttp to benchmark a minimalist servlet
// ============================================================================
// imported functions:
// get_reply(): get a pointer on the </span><span class="punct">'</span><span class="ident">reply</span><span class="punct">'</span><span class="string"> dynamic buffer from the server
// xbuf_cat(): like strcat(), but it works in the specified dynamic buffer
// ----------------------------------------------------------------------------
#include "gwan.h" // G-WAN exported functions
int main(int argc, char *argv[])
{
xbuf_cat(get_reply(argv), "Hello World!");
return 200; // return an HTTP code (200:</span><span class="punct">'</span><span class="constant">OK</span><span class="punct">'</span><span class="string">)
}
// ============================================================================
// End of Source Code
// ============================================================================</span></pre>
<br />
<br />
Now start the server:<br />
<br />
sudo ./gwan<br />
<br />
and enjoy: <a href="http://localhost:8080/csp?hello1.c">http://localhost:8080/csp?hello1.c</a><br />
<br />
I wonder if, in a near future, we will have a super fast REST backend with a thick Javascript interface made with Backbone.js and JQuery.Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com3tag:blogger.com,1999:blog-1110641097953748292.post-77530999412259324812012-02-03T12:20:00.000-08:002012-10-29T00:17:15.413-07:00CGI script with Apache and C programming languageDynamic web pages where born in 1993 when CGI was introduced. The most common language used was Perl, but C was used too. Today I just tried to write a CGI script for the first time in my life and I did it with C, like a real man. Find your CGI path, mine is:<br />
<br />
$ sudo cat /etc/apache2/httpd.conf | grep ScriptAlias<br />
ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"<br />
<br />
So in my Mac I can put my CGI scripts here: /Library/WebServer/CGI-Executables/.<br />
<br />
I created a file, test.c with this content:<br />
<br />
<pre><span class="comment">#include </span>
<span class="comment">#include </span>
<span class="ident">int</span> <span class="ident">main</span><span class="punct">(</span><span class="ident">void</span><span class="punct">)</span> <span class="punct">{</span>
<span class="ident">printf</span><span class="punct">("</span><span class="string">Content-Type: text/plain;charset=us-ascii<span class="escape">\n\n</span></span><span class="punct">");</span>
<span class="ident">printf</span><span class="punct">("</span><span class="string">Hello world<span class="escape">\n\n</span></span><span class="punct">");</span>
<span class="ident">printf</span><span class="punct">("</span><span class="string">The C programming language<span class="escape">\n\n</span></span><span class="punct">");</span>
<span class="ident">time_t</span> <span class="ident">now</span><span class="punct">;</span>
<span class="ident">struct</span> <span class="ident">tm</span> <span class="punct">*</span><span class="ident">d</span><span class="punct">;</span>
<span class="ident">char</span> <span class="ident">li</span><span class="punct">[</span><span class="number">13</span><span class="punct">];</span>
<span class="ident">time</span><span class="punct">(&</span><span class="ident">now</span><span class="punct">);</span>
<span class="ident">d</span> <span class="punct">=</span> <span class="ident">localtime</span><span class="punct">(&</span><span class="ident">now</span><span class="punct">);</span>
<span class="ident">strftime</span><span class="punct">(</span><span class="ident">li</span><span class="punct">,</span> <span class="number">15</span><span class="punct">,</span> <span class="punct">"</span><span class="string">%d/%m/%Y</span><span class="punct">",</span> <span class="ident">d</span><span class="punct">);</span>
<span class="ident">printf</span><span class="punct">("</span><span class="string">Today is %s<span class="escape">\n</span></span><span class="punct">",</span> <span class="ident">li</span><span class="punct">);</span>
<span class="keyword">return</span> <span class="number">0</span><span class="punct">;</span>
<span class="punct">}</span></pre>
<br />
<br />
I compiled the file and made it executable:<br />
<br />
cc testc.c -o testc && chmod +x testc<br />
<br />
And is saw the outcome here <a href="http://localhost/cgi-bin/testc">http://localhost/cgi-bin/testc</a>.<br />
<br />
This is the output:<br />
<br />
Hello world<br />
<br />
The C programming language<br />
<br />
Today is 03/02/2012<br />
<br />
CGI is very simple, you just need to print the content type with newline and carriage return, then you can output your dynamic HTML content.<br />
<br />
Today FastCGI has replaced CGI and it is very popular and probably it is the best way to create dynamic web pages with the most performance possible: C + FastCGI. Another option, more sophisticated and with different way of use it is <a href="http://projects.unbit.it/uwsgi/">uWSGI</a>, created by the Italian hosting company Unbit.it. Yesterday I read an article about the usage of C++ as programming language for Facebook, in 2009 they could shutdown 22500 servers if they where using C++ instead of PHP. We all know that Facebook has created a PHP compiler to compile PHP in C++: <a href="http://developers.facebook.com/blog/post/358/">HipHop</a><br />
<br />
P.S.: charset shout be UTF-8 :-)Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com3tag:blogger.com,1999:blog-1110641097953748292.post-1394967121487620262011-12-29T03:04:00.000-08:002011-12-29T03:07:03.160-08:00PHP hosting on EC2EC2 (Amazon Virtual servers) can be used as any other VPS, if you use EBS for storing your data and the cost is really low. But where EC2 really exceed is on building automatically scaling web hosting solutions. Web servers can be load balanced with Elastic Load Balancers, servers can be added on demand. Best tools to create and manage an infrastructure on EC2 are Chef and Puppet.Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-6984467727942439252011-12-29T02:17:00.001-08:002011-12-29T02:41:28.560-08:00Create a basic Ruby app with Rack middleware - part 1This post - or may be a series of posts - on Rack is intended to understand how modern Ruby frameworks work. Frameworks such as Sinatra or Ruby on Rails use Rack middleware to exchange requests and responses to application servers. Old time of CGI or FCGI are gone. The code below shows how simple is to start a ruby server to serve a 404 error page:<br /><br /><pre><span class="ident">app</span> <span class="punct">=</span> <span class="ident">lambda</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">env</span><span class="punct">|</span><br /> <span class="ident">body</span> <span class="punct">=</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">open</span><span class="punct">(</span><span class="constant">File</span><span class="punct">.</span><span class="ident">dirname</span><span class="punct">(</span><span class="constant">__FILE__</span><span class="punct">)+'</span><span class="string">/404.html</span><span class="punct">',</span> <span class="punct">'</span><span class="string">r</span><span class="punct">').</span><span class="ident">read</span><br /> <span class="punct">[</span><span class="number">404</span><span class="punct">,</span> <span class="punct">{'</span><span class="string">Content-Type</span><span class="punct">'</span> <span class="punct">=></span> <span class="punct">'</span><span class="string">text/html</span><span class="punct">'},</span> <span class="punct">[</span><span class="ident">body</span><span class="punct">]]</span><br /><span class="keyword">end</span><br /><br /><span class="ident">run</span> <span class="ident">app</span></pre><br /><br />Save the previous script in a file named config.ru. If you have a page 404.html in the same directory of config.ru and you run:<br /><br />> rackup -p 3000<br /><br />You will get the 404.html page by connecting to http://localhost:3000. Please notice the curly braces between <i>body</i>: it is needed by Ruby 1.9. The <span style="font-style:italic;">run app</span> statement run the lambda we just created.Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com1tag:blogger.com,1999:blog-1110641097953748292.post-80637667085455112142011-12-29T01:54:00.000-08:002012-05-31T00:01:47.906-07:00Convert ruby code to HTML to paste it into your blogI use the following script to read ruby code and output the HTML to the stdin of the shell:<br />
<br />
<pre><span class="ident">require</span> <span class="punct">'</span><span class="string">syntax/convertors/html</span><span class="punct">'</span>
<span class="ident">code</span> <span class="punct">=</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">read</span><span class="punct">('</span><span class="string">code.rb</span><span class="punct">')</span>
<span class="ident">convertor</span> <span class="punct">=</span> <span class="constant">Syntax</span><span class="punct">::</span><span class="constant">Convertors</span><span class="punct">::</span><span class="constant">HTML</span><span class="punct">.</span><span class="ident">for_syntax</span> <span class="punct">"</span><span class="string">ruby</span><span class="punct">"</span>
<span class="ident">code_html</span> <span class="punct">=</span> <span class="ident">convertor</span><span class="punct">.</span><span class="ident">convert</span><span class="punct">(</span> <span class="ident">code</span> <span class="punct">)</span>
<span class="ident">puts</span> <span class="ident">code_html</span></pre>
<br />
<br />
Put you ruby code in 'code.rb' and run<br />
<br />
> ruby script_name.rb<br />
<br />
Then cut and paste the content. You have to add the following CSS if you want to see your code properly formatted:<br />
<br />
<pre>pre {
background-color: #f1f1f3;
color: #112;
padding: 10px;
font-size: 1.1em;
overflow: auto;
margin: 4px 0px;
width: 95%;
}
/* Syntax highlighting */
pre .normal {}
pre .comment { color: #005; font-style: italic; }
pre .keyword { color: #A00; font-weight: bold; }
pre .method { color: #077; }
pre .class { color: #074; }
pre .module { color: #050; }
pre .punct { color: #447; font-weight: bold; }
pre .symbol { color: #099; }
pre .string { color: #944; background: #FFE; }
pre .char { color: #F07; }
pre .ident { color: #004; }
pre .constant { color: #07F; }
pre .regex { color: #B66; background: #FEF; }
pre .number { color: #F99; }
pre .attribute { color: #5bb; }
pre .global { color: #7FB; }
pre .expr { color: #227; }
pre .escape { color: #277; }</pre>
<br />
<br />
The style of the CSS code above has been created by changing the type of highlighter:<br />
<br />
<br />
<pre><span class="ident">convertor</span> <span class="punct">=</span> <span class="constant">Syntax</span><span class="punct">::</span><span class="constant">Convertors</span><span class="punct">::</span><span class="constant">HTML</span><span class="punct">.</span><span class="ident">for_syntax</span> <span class="punct">"</span><span class="string">css</span><span class="punct">"</span></pre>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-71901028345736550242011-12-29T00:39:00.000-08:002011-12-29T01:30:55.126-08:00How to call a private method in RubyIf you want to call a private method in Ruby - for any reason - you can use <i>send</i>, <i>method</i> and <span style="font-style:italic;">instance_eval:</span><div><span style="font-style:italic;"><br /></span></div><br /><br /><pre><span class="keyword">class </span><span class="class">Foo</span><br /><br /> <span class="ident">private</span><br /> <span class="keyword">def </span><span class="method">bar</span><br /> <span class="ident">puts</span> <span class="punct">'</span><span class="string">You called bar</span><span class="punct">'</span><br /> <span class="keyword">end</span><br /><span class="keyword">end</span><br /> <br /><span class="punct">></span> <span class="ident">foo</span> <span class="punct">=</span> <span class="constant">Foo</span><span class="punct">.</span><span class="ident">new</span><br /><span class="ident">foo</span><span class="punct">.</span><span class="ident">bar</span> <span class="comment"># this, of course, throws an error</span><br /><span class="constant">NoMethodError</span><span class="punct">:</span> <span class="ident">private</span> <span class="ident">method</span> `<span class="ident">bar</span><span class="punct">'</span><span class="string"> called for #<br /> from (irb):129<br /> from :0<br /><br />> foo.send(:bar)<br />You called bar<br />=> nil<br />m = foo.method(:bar)<br />=> #<br />> m.call<br />You called bar<br />=> nil<br />> foo.instance_eval { bar }<br />You called bar<br />=> nil<span class="normal"><br /></span></span></pre><br /><br /><div>Remember you cannot use <i>eval</i> to call a private method:</div><br /><br /><pre><span class="punct">></span> <span class="ident">eval</span><span class="punct">('</span><span class="string">foo.bar</span><span class="punct">')</span><br /><span class="constant">NoMethodError</span><span class="punct">:</span> <span class="ident">private</span> <span class="ident">method</span> `<span class="ident">bar</span><span class="punct">'</span><span class="string"> called for #<br /> from (irb):140<br /> from (irb):140<br /> from :0</span></pre><br /><br /><br /><div>Reference: <a href="http://www.khelll.com/blog/ruby/ruby-dynamic-method-calling/">http://www.khelll.com/blog/ruby/ruby-dynamic-method-calling/</a></div>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-48608872474503428652011-11-16T03:01:00.000-08:002011-11-16T03:07:06.031-08:00How to become successfulThis is a quite inspiring speech. Reaching a target or becoming successful takes time and pain. Rewards are difficult to obtain, but when you get it... it is awesome:<br /><br /><iframe width="420" height="315" src="http://www.youtube.com/embed/JRfoFGGyRvU" frameborder="0" allowfullscreen></iframe>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-48879440705019032592011-11-13T10:19:00.000-08:002011-11-13T10:21:26.322-08:00Create a Rails 3 project with "activerecord", "capybara", "devise", "git", "haml", "jquery", "rspec", "sass", "settingslogic"You can run this commend:<br /><br />rails new APP_NAME -m http://railswizard.org/50fc74d6dfa659c06971.rb -J -T<br /><br />I have created the template using http://railswizard.org/<a href="http://railswizard.org/"></a>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-21900740311887808552011-11-13T10:13:00.000-08:002011-11-13T10:15:39.822-08:00Unix: change password one line<script src="https://gist.github.com/1362431.js"> </script>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-11573352404297404942011-11-13T10:00:00.001-08:002011-11-13T10:02:41.139-08:00How to change the user password without re-typing the passowrdYou can use the following script to change the user password as root without having to re-type the password:<br /><br /><script src="https://gist.github.com/1362404.js"> </script><br /><br />I found this script helpful when called via ssh.Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-16893283097980629792011-11-11T02:27:00.001-08:002011-11-11T02:29:05.442-08:00Catch all exceptions in your Ruby programJust put your code between begin and rescue statements and print the error. An example:<br /><br /><br />ree-1.8.7-head :010 > begin<br />ree-1.8.7-head :011 > Testing.new<br />ree-1.8.7-head :012?> rescue Exception => msg<br />ree-1.8.7-head :013?> puts "Something went wrong ("+msg+")"<br />ree-1.8.7-head :014?> end<br />Something went wrong (uninitialized constant Testing)<br />=> nil<br /><br />You can boot your application between begin and rescue and you will be able to print the error, perform an action, send an email in the rescue begin section:<br /><pre><br />begin<br /> MyApp.run!<br />rescue Exception => msg<br /> puts "Something went wrong ("+msg+")"<br />end<br /></pre>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-46709087266575828452011-08-12T03:35:00.000-07:002011-08-12T03:40:45.414-07:00List MySql database size with a queryThe query:
<br />
<br />SELECT table_schema "Data Base Name",
<br /> sum( data_length + index_length ) / 1024 /
<br />1024 "Data Base Size in MB",
<br /> sum( data_free )/ 1024 / 1024 "Free Space in MB"
<br />FROM information_schema.TABLES
<br />GROUP BY table_schema ;
<br />
<br />The mysql console:
<br />
<br />mysql> SELECT table_schema "Data Base Name",
<br /> -> sum( data_length + index_length ) / 1024 /
<br /> -> 1024 "Data Base Size in MB",
<br /> -> sum( data_free )/ 1024 / 1024 "Free Space in MB"
<br /> -> FROM information_schema.TABLES
<br /> -> GROUP BY table_schema ;
<br />+--------------------+----------------------+------------------+
<br />| Data Base Name | Data Base Size in MB | Free Space in MB |
<br />+--------------------+----------------------+------------------+
<br />| information_schema | 0.00878906 | 0.00000000 |
<br />| mysql | 0.61361027 | 0.00000000 |
<br />| performance_schema | 0.00000000 | 0.00000000 |
<br />| test | 0.01562500 | 10.00000000 |
<br />+--------------------+----------------------+------------------+
<br />4 rows in set (0.00 sec)
<br />
<br />Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-75719135316669051402011-08-12T01:33:00.000-07:002011-08-14T05:10:31.503-07:00Install MySql generic binary on LinuxI want to have the latest GA version of MySql, the 5.5.x instead of the old 5.1 release. 5.5.x has many performance improvements, from x2 to x8, according to an article of <a href="http://datacharmer.blogspot.com/">Guiseppe Maxia</a>, a MySql expert. Go to the <a href="http://www.mysql.com/downloads/mirror.php?id=403422#mirrors">download page</a> and get the tar gizipped binary version of Mysql. I downloaded this one:
<br />
<br /># apt-get install libaio1
<br /># wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.15-linux2.6-x86_64.tar.gz/from/http://mysql.mirrors.ovh.net/ftp.mysql.com/
<br /># mv index.html mysql.tar.gz
<br /># tar xzfv mysql.tar.gz
<br /># mv mysql-5.5.15-linux2.6-x86_64/ mysql-5.5.15
<br /># mv mysql-5.5.15/ /usr/local/
<br /># cd /usr/local/
<br /># ln -s mysql-5.5.15/ mysql
<br /># cd mysql
<br /># chown -R mysql .
<br /># chgrp -R mysql .
<br /># scripts/mysql_install_db --user=mysql
<br /># cp support-files/mysql.server /etc/init.d/mysql.server
<br /># cp support-files/my-medium.cnf /etc/my.cnf
<br /># chmod +x /etc/init.d/mysql.server
<br /># update-rc.d mysql.server defaults
<br /># /etc/init.d/mysql.server start
<br />Starting MySQL.. *
<br /># /etc/init.d/mysql.server restart
<br /># bin/mysql -uroot
<br />
<br />mysql> use mysql;
<br />Reading table information for completion of table and column names
<br />You can turn off this feature to get a quicker startup with -A
<br />
<br />Database changed
<br />mysql> update user set password=PASSWORD("your_passwd") where user='root';
<br />Query OK, 4 rows affected (0.00 sec)
<br />Rows matched: 4 Changed: 4 Warnings: 0
<br />
<br />mysql> flush privileges;
<br />Query OK, 0 rows affected (0.00 sec)
<br />
<br />mysql> exit
<br />Bye
<br />
<br />Done! Simple isn't it?
<br />
<br />Reference: <a href="http://dev.mysql.com/doc/refman/5.5/en/binary-installation.html">http://dev.mysql.com/doc/refman/5.5/en/binary-installation.html</a>
<br />Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-42996459405732840892011-08-05T05:22:00.000-07:002011-12-29T01:33:19.798-08:00conman: automate your server configurationProbably you already know about <a href="http://www.puppetlabs.com/">Puppet</a> and <a href="http://wiki.opscode.com/display/chef">Chef</a> and probably you felt overwhelmed when learning one of the the two server configuration management. I want to show you <a href="https://github.com/moocode/conman">conman</a> a simpler and easier approach then Puppet and Chef. Of course it has very basic features but it could be easily extended. So install conman:<br /><br />gem install conman<br /><br />conman has ingredients and recipes. Recipes contains ingredients. An <a href="https://github.com/moocode/conman/blob/master/lib/conman/ingredient.rb">ingredient is a class</a> extending the Ingredient class having methods with commands. Here you can see and example of a class which can install or remove the apache2 package:<br /><br /><pre><br />class Apache2Ingredient < Ingredient<br /> def install<br /> `sudo apt-get install apache2`<br /> end<br /><br /> def remove<br /> `sudo apt-get remove apache2`<br /> end<br />end<br /></pre><br />The you can include you new ingredient in your recipe:<br /><br /><pre><br />class MyRecipe < Recipe<br /> apache2 :install<br />end<br /></pre><br /><br />apache2 method search and load the Apache2Ingredient, then calls the install method. Simple!<br /><br />You can run the recipe using the command line:<br /><br /><pre><code>conman -i /path/to/ingredients /path/to/recipes/my_recipe.rb<br /><br />or using Ruby:<br /><br /></code><code>Conman.init :ingredients => '/path/to/ingredients/' Conman.run '/path/to/recipes/my_recipe.rb'</code><br /></pre><br /><br />conman does not know about packages, apt-get, rpm... but I think it can be easily implemented. Another important thing that is missing is the possibility to run remotely the recipes; a SSH session using a Ruby library could do the trick (execute the command remotely).<br /><br />Link: <a href="https://github.com/moocode/conman">https://github.com/moocode/conman</a>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-72125204782269914252011-01-31T02:39:00.000-08:002011-01-31T02:46:43.461-08:00Life is, in fact, a battle"Life is, in fact, a battle. Evil is insolent and strong: beauty enchanting but rare; good very apt to be weak; folly very apt to be defiant; wickedness to carry the day; imbeciles to be in great places, people of sense in small, and mankind generally unhappy.<br /><br />But the world as it stands is no illusion, no phantasm, no evil dream of a night; we wake up to it again for ever and ever; we can neither forget it nor deny it nor dispense with it."<br /><br />I do not remember who wrote it, but I fully subscribe to it.Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-16360763758583374122010-11-30T13:36:00.000-08:002010-11-30T13:36:07.518-08:00Bettermeans Introduction<iframe width="480" height="295" src="http://www.youtube.com/embed/MAlnMWlvw9g?fs=1" frameborder="0"></iframe>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0tag:blogger.com,1999:blog-1110641097953748292.post-5048134876397163212010-11-20T12:10:00.000-08:002010-11-20T12:24:07.347-08:00Test unique values in Rails model with RspecI wanted to test 'validates_uniqueness_of' for this model:<br /><pre><br />class CourseItem < ActiveRecord::Base<br /> validates_presence_of :course_id, :day, :url<br /> validates_uniqueness_of :course_id, :scope => :day, <br /> :message => "- this day has been already created."<br />end<br /></pre><br />I tried to create two objects with the same value and I was getting the validetion error when creating the second value, the spec was failing and I did not know you to test it. There is a custom matcher to test the uniqueness of fields and it is available with a <a href="https://github.com/bogdan/accept_values_for">gem</a> developed by <a href="http://gusiev.com/about-me/">Bogdan Gusiev</a>. This blog post will help you to get started: <a href="http://gusiev.com/2010/06/ultimate-rspec-matcher-to-test-validation/">http://gusiev.com/2010/06/ultimate-rspec-matcher-to-test-validation/</a>. That gem has other useful matchers too.<br /><br />I checked that model with the following code:<br /><br /><script src="https://gist.github.com/708121.js?file=gistfile1.txt"></script>Riccardo Tacconihttp://www.blogger.com/profile/15830685266951613839noreply@blogger.com0