Thursday, July 2, 2009
Multiple Versions of Rails
I wanted to know whether you can have multiple versions of rails and found this helpful article.
SysInternals ZoomIt
One of the Epic presenters was using various Sysinternals tools during his presentation. These tools are super handy--I've used several of them over the years. But I didn't know about ZoomIt:
ZoomIt is screen zoom and annotation tool for technical presentations that include application demonstrations. ZoomIt runs unobtrusively in the tray and activates with customizable hotkeys to zoom in on an area of the screen, move around while zoomed, and draw on the zoomed image. I wrote ZoomIt to fit my specific needs and use it in all my presentations.
Labels:
tools
Ruby equivalent to Python's getattr
I was looking for the Ruby equivalent to getattr and I ran into this article:
http://stackoverflow.com/questions/786412/what-is-the-ruby-equivalent-of-pythons-getattr
The answer was not generic enough for me. I'd like to implement a Ruby equivalent to Python's built-in sorted function.
So after a little more searching, I found the answer: send and posted it.
http://stackoverflow.com/questions/786412/what-is-the-ruby-equivalent-of-pythons-getattr
The answer was not generic enough for me. I'd like to implement a Ruby equivalent to Python's built-in sorted function.
So after a little more searching, I found the answer: send and posted it.
Wednesday, July 1, 2009
Playing with Ruby
I know Python very well. But it's helpful for me to reflect a little deeper on what this actually means. I don't use Python when I want to write a web app. I use Python when I have to manipulate and/or analyze files or text.
I don't currently have anything like mastery of a very effective and contemporary tool for writing a web app. This is why I'm interested in digging into Rails. And therefore why I need to dig into Ruby...
So I had this little problem: Create a drop-down list of the hours of the day, using "midnight" and "noon", but otherwise the 12 hour clock name (e.g., 1 AM, 1 PM, etc).
Ignore the drop-down list for a second and let's just focus on the data.
In Python, that would look something like this:
"display" here is a function that takes a number (0-23) and returns a string. This is the least important aspect of this problem--probably because I'm more focused on learning how to iterate in Ruby, what the built-in stuff is capable of, etc.
Nonetheless, a simple implementation of display:
In Ruby, I first tried this:
inject is a cool little function. The problem here is that simple sorting is somewhat verbose in Ruby--at least, as far as I know. Here's what I came up with:
That's when I realized, wait a second, why not just inject into an Array instead of a Hash?
Cool, now I have something I can iterate over simply:
I don't currently have anything like mastery of a very effective and contemporary tool for writing a web app. This is why I'm interested in digging into Rails. And therefore why I need to dig into Ruby...
So I had this little problem: Create a drop-down list of the hours of the day, using "midnight" and "noon", but otherwise the 12 hour clock name (e.g., 1 AM, 1 PM, etc).
Ignore the drop-down list for a second and let's just focus on the data.
In Python, that would look something like this:
hours = dict([(hour, display(hour)) for hour in range(24)])
for hour in sorted(hours):
text = hours[hour]
print "" % locals()
"display" here is a function that takes a number (0-23) and returns a string. This is the least important aspect of this problem--probably because I'm more focused on learning how to iterate in Ruby, what the built-in stuff is capable of, etc.
Nonetheless, a simple implementation of display:
def display(hour):
if hour == 0:
return 'midnight'
if hour == 12:
return 'noon'
ampm = hour > 12 and 'PM' or 'AM'
return '%d %s' % (hour % 12, ampm)
In Ruby, I first tried this:
hours = (0..23).inject({}) do |hash, value|
hash[value] = display(value)
hash
end
inject is a cool little function. The problem here is that simple sorting is somewhat verbose in Ruby--at least, as far as I know. Here's what I came up with:
hours_sorted = hours.keys.sort_by {|k| k}.map {|k| [k, hours[k]]}
hours_sorted.each do |k, v|
print "\n"
end
That's when I realized, wait a second, why not just inject into an Array instead of a Hash?
hours = (0..23).inject([]) do |list, value|
list.push([value, display(value)])
list
end
Cool, now I have something I can iterate over simply:
hours.each do |k, v|
print "\n"
end
My Blogger Template Blows
It's too thin. I really don't want to fix this now--even if it's super easy.
Labels:
blogger
Installing Rails on Tiger
I had an aborted attempt last year (late January, 2008) to learn and use Rails. My buddy Phil and I spent a long weekend down in Jacksonville with Jon and Jim. It was a whirlwind weekend. I learned a lot, had a lot of fun, but Phil and I decided to use ASP.Net/C# instead for the CCAP-based services.
I figured I'd come back to Rails, eventually. And that time has come.
My MacBook still had the 1.2x (was that what it was?) version of Rails that Jon helped me install. So when I was casting about for some help in installing a more recent version, I ran across this classic:
Building Ruby, Rails, Subversion, Mongrel, and MySQL on Mac OS X
And I figured I'd put the whole thing into a single bash script (see below). The script has obvious deficiencies. It's not very parameterized. I comment out the function calls at the bottom that I don't want to run--rather than having the script detect when a component is installed or where the version installed matches the to-be-installed version.
Despite these deficiencies, it was useful for me both in setting up Rails this time and in serving as a record of what I installed.
I figured I'd come back to Rails, eventually. And that time has come.
My MacBook still had the 1.2x (was that what it was?) version of Rails that Jon helped me install. So when I was casting about for some help in installing a more recent version, I ran across this classic:
Building Ruby, Rails, Subversion, Mongrel, and MySQL on Mac OS X
And I figured I'd put the whole thing into a single bash script (see below). The script has obvious deficiencies. It's not very parameterized. I comment out the function calls at the bottom that I don't want to run--rather than having the script detect when a component is installed or where the version installed matches the to-be-installed version.
Despite these deficiencies, it was useful for me both in setting up Rails this time and in serving as a record of what I installed.
#!/bin/bash
#
# install_rails_stuff
function install_readline
{
pushd /usr/local/src
if [ ! -f readline-5.1.tar.gz ] ; then
curl -O ftp://ftp.gnu.org/gnu/readline/readline-5.1.tar.gz
fi
if [ -d readline-5.1 ] ; then
rm -fr readline-5.1
fi
tar xzvf readline-5.1.tar.gz
pushd readline-5.1
./configure --prefix=/usr/local
make
sudo make install
popd
}
function install_ruby
{
pushd /usr/local/src
if [ ! -f ruby-1.8.6.tar.gz ] ; then
curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6.tar.gz
fi
if [ -d ruby-1.8.6 ] ; then
rm -fr ruby-1.8.6
fi
tar xzvf ruby-1.8.6.tar.gz
pushd ruby-1.8.6
./configure --prefix=/usr/local --enable-pthread --with-readline-dir=/usr/local --enable-shared
make
sudo make install
sudo make install-doc
popd
}
function install_sqlite3
{
pushd /usr/local/src
if [ ! -f sqlite-amalgamation-3.6.16.tar.gz ] ; then
curl -O http://www.sqlite.org/sqlite-amalgamation-3.6.16.tar.gz
fi
if [ -d sqlite-3.6.16 ] ; then
rm -fr sqlite-3.6.16
fi
tar -zxvf sqlite-amalgamation-3.6.16.tar.gz
pushd sqlite-3.6.16
./configure --prefix=/usr/local
make
sudo make install
popd
}
function install_rubygems
{
pushd /usr/local/src
if [ ! -f rubygems-1.3.4.tgz ] ; then
curl -O http://files.rubyforge.mmmultiworks.com/rubygems/rubygems-1.3.4.tgz
fi
if [ -d rubygems-1.3.4 ] ; then
rm -fr rubygems-1.3.4
fi
tar xzvf rubygems-1.3.4.tgz
pushd rubygems-1.3.4
sudo /usr/local/bin/ruby setup.rb
popd
}
function install_rails
{
sudo gem install rails --include-dependencies
}
function install_mongrel
{
sudo gem install mongrel --include-dependencies
}
function install_capistrano
{
sudo gem install capistrano --include-dependencies
}
function install_termios
{
sudo gem install termios --include-dependencies
}
install_readline
install_ruby
install_rubygems
install_rails
install_mongrel
install_capistrano
install_termios
install_sqlite3
Labels:
rails
SyntaxHighlighter on Blogger
Motivation
I'd like to do more code-related posts. It would be really cool if I could post code with syntax highlighting.
The Quest
So, naturally, I turn to Google with a search for: blogger syntax highlighting.
I added "blogger" to the search because I figured someone who uses Blogger had already been down this road and it's really important to me that the solution work easily/well with Blogger.
The Solution
I found Abhishek Sanoujam's post Blogger + syntax highlighting first. This was a good indication that there was a free and relatively easy-to-use solution: Alex Gorbatchev's SyntaxHighlighter.
Unfortunately, the SyntaxHighlighter site provides no detailed guidance on how to use it with Blogger.
So I turned back to Abhishek's post, only to learn that...
The World Changes Quickly
One of the suggestions Abishek makes--to use Google Pages for hosting the JavaScript files--is no longer an option. Google Pages is being phased out in favor of Google Sites. And there doesn't appear to be support for hosting .js files in Google Sites.
Thankfully, back on the SyntaxHighlighter site, I noticed a link for Hosting.
Now it was clear that the key was going to be editing my Blogger template. (Which I'd never done before.)
Reference Current or Named Version?
You have to decide whether to reference the current version or a named version. Look here for what I'm talking about:
http://alexgorbatchev.com/pub/sh/
I choose to reference "current" because I'd rather forget about this choice and seamlessly take advantage of upgrades, with the risk that something I do may stop working at some point (because of backwards-incompatible changes made in SyntaxHighlighter). I figured that risk is minimal both in terms of probability and consequence.
Editing Blogger Template
There are three edits you need to make to your Blogger template:
I'll cover each of these in turn.
Add the css
I learned that Blogger does not allow link tags within the head element. So instead of adding link tags to reference the .css files hosted at the SyntaxHighlighter site, I needed to copy/paste the css inline:
I choose to put the css at the end of the existing styles in the head.
You'll have to find this snippet:
Add the contents of the following above that end b:skin tag:
pre tag print hack
I found the following snippet on one of the sites I stumbled across in trying to figure this out:
After you paste the css inline, you need to specify absolute URLs for the .png references. For instance, this:
becomes this:
Add the references to the .js files
You have to add a reference to shCore.js. In addition, you need to choose which brush .js files to use--whether all of them or just the ones you're most likely to use.
I added the following immediately after the b:skin end tag and directly before the head end tag:
Add the script to initialize the SyntaxHighlighter
Below the script references just added, I then added the following:
Observations
I customized the toolbar css to set the top property to -40px--so that it wouldn't cover up the first line of my code. I don't really see that much benefit to the toolbar, so I'm tempted to just turn it off.
Examples
CSS:
Groovy:
JScript:
Plain:
Python:
Ruby:
SQL:
XML:
I'd like to do more code-related posts. It would be really cool if I could post code with syntax highlighting.
The Quest
So, naturally, I turn to Google with a search for: blogger syntax highlighting.
I added "blogger" to the search because I figured someone who uses Blogger had already been down this road and it's really important to me that the solution work easily/well with Blogger.
The Solution
I found Abhishek Sanoujam's post Blogger + syntax highlighting first. This was a good indication that there was a free and relatively easy-to-use solution: Alex Gorbatchev's SyntaxHighlighter.
Unfortunately, the SyntaxHighlighter site provides no detailed guidance on how to use it with Blogger.
So I turned back to Abhishek's post, only to learn that...
The World Changes Quickly
One of the suggestions Abishek makes--to use Google Pages for hosting the JavaScript files--is no longer an option. Google Pages is being phased out in favor of Google Sites. And there doesn't appear to be support for hosting .js files in Google Sites.
Thankfully, back on the SyntaxHighlighter site, I noticed a link for Hosting.
Now it was clear that the key was going to be editing my Blogger template. (Which I'd never done before.)
Reference Current or Named Version?
You have to decide whether to reference the current version or a named version. Look here for what I'm talking about:
http://alexgorbatchev.com/pub/sh/
I choose to reference "current" because I'd rather forget about this choice and seamlessly take advantage of upgrades, with the risk that something I do may stop working at some point (because of backwards-incompatible changes made in SyntaxHighlighter). I figured that risk is minimal both in terms of probability and consequence.
Editing Blogger Template
There are three edits you need to make to your Blogger template:
- Add the css
- Add the references to the .js files
- Add the script to initialize the SyntaxHighlighter
I'll cover each of these in turn.
Add the css
I learned that Blogger does not allow link tags within the head element. So instead of adding link tags to reference the .css files hosted at the SyntaxHighlighter site, I needed to copy/paste the css inline:
I choose to put the css at the end of the existing styles in the head.
You'll have to find this snippet:
]]></b:skin>
Add the contents of the following above that end b:skin tag:
- http://alexgorbatchev.com/pub/sh/current/styles/shCore.css
- http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css
- pre tag print hack (see below)
pre tag print hack
I found the following snippet on one of the sites I stumbled across in trying to figure this out:
pre {
overflow-x: auto; /* Use horizontal scroller if needed; for Firefox 2, not needed in Firefox 3 */
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
/* width: 99%; */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
After you paste the css inline, you need to specify absolute URLs for the .png references. For instance, this:
background-image: url(magnifier.png);
becomes this:
background-image: url(http://alexgorbatchev.com/pub/sh/current/styles/magnifier.png);
Add the references to the .js files
You have to add a reference to shCore.js. In addition, you need to choose which brush .js files to use--whether all of them or just the ones you're most likely to use.
I added the following immediately after the b:skin end tag and directly before the head end tag:
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushGroovy.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js'/>
Add the script to initialize the SyntaxHighlighter
Below the script references just added, I then added the following:
<script type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.gutter = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
Observations
I customized the toolbar css to set the top property to -40px--so that it wouldn't cover up the first line of my code. I don't really see that much benefit to the toolbar, so I'm tempted to just turn it off.
Examples
CSS:
.world
{
font-weight: bold;
}
Groovy:
def hello()
{
println "hello world"
}
JScript:
function hello()
{
alert('hello');
}
Plain:
hello world
Python:
def hello:
print 'hello'
Ruby:
def hello
print 'hello'
end
SQL:
// Oracle
select 'hello world' from dual;
// SQL Server
select 'hello world'
XML:
<say>
hello world
</say>
Labels:
blogger
Subscribe to:
Posts (Atom)