18 Oct 2014
File permissions can either be: r, w or x.
Readable, Writeable or eXecutable.
There are also 3 ‘tiers’ of access allowed: ‘Owner’, ‘Group’ & ‘Other’
These 3 different access levels can be represented like this:
Owner: rwx Group: rwx, Other: rwx
This can be shortened to: rwxrwxrwx
In the previous example, anyone is able to read, write(edit) or execute(run) the file.
To disallow them, you can substitute -s in:
rwx------ would only be useful for the owner of the file.
r--r--r-- would mean that anyone can read the file.
Clever programmers like to do things with the minimum system requirements; reading 9 letters for each file would be energetically expensive / resource intensive.
To solve this problem, they often shorten this syntax (rwxrwxrwx) even more using binary.
They say that:
- Read = 4 bits (binary 100)
- Write = 2 bits (binary 010)
- Execute = 1 bit (binary 001)
Using these simple rules, you can efficiently say that, in decimal (or octal) numbers:
- 0 = No permissions
- 1 = Execute
- 2 = Write
- 3 = Write & Execute
- 4 = Read
- 5 = Read & Execute
- 6 = Read & Write
- 7 = Read, Write & Execute
Hopefully that makes sense. Read + Execute = 4 + 1 = 5. Geddit?
Anyway, this system allows us to transform:
rwx------ to 700
r--r--r-- to 444
A common file permission to set is chmod 755, which is rwx for the Owner but only rw- for other users.
16 Oct 2014
Beware of setting Hash#default!
Unless you know the pitfalls of Hash#default, you should tread carefully…
You can set default values for hashes a number of ways.
Eg:
- Hash.new(default_value)
- Hash.new.default = default_value
Hash.new(0) usually works as expected, but Hash.new([]) can be tricky.
If you wanted to append any values to a hash instantiated like this, there will be problems; the array is not just a default array, but it’s also a shared array.
An example:
h = Hash.new([])
h['fish'] << 'carp'
puts h # => nil.
What’s happening here is that we’ve edited the default value of the hash. We haven’t actually assigned the default value to our lovely ‘fish’.
To actually assign, we need to do:
Note the = sign.
However, this is not perfect:
puts h # => {fish: ['carp', 'carp']}
Our ‘default’ value how has two carp in it.
If we were to add another key-value pair, this would get even messier:
h['dog'] <<= 'poodle'
puts h # => {fish: ['carp', 'carp', 'poodle'], dog:['carp', 'carp', 'poodle']}
A disaster!
Our carp and Poodle have now mixed.
Hopefully it makes sense what’s happening; we’re altering the (single) default array, which points to all our default values.
How do we resolve the situation?
Blocks to the rescue!
You can pass in a block when initializing the Hash.
Hash.new {|hash, key| ... rest of block... }
To create a default hash which hash a unique default for each key/value pair, we can then do this:
Hash.new {|hash, key| hash[key] = [] }
Since this block is run each time we assign a new key, the default value will always be [].
See also Hash#default_proc.
The benefit of using blocks & procs is that we’re able to write clever pieces of code.. and could make our default value do anything we wanted.
12 Oct 2014
Apparently I sometimes try to be funny….
I thought it would be a good idea to try to port my CV to Ruby.
This is what happened:
class WilliamClarke
attr_accessor :notableProjects, :education, :interests, :employment
def initialize
@home = 'London'
@phone = '07XXXXXXXXX'
@website = 'wmmclarke.com'
@blog = 'wmmc.github.io'
@github = 'github.com/wmmc'
end
def super_enthusiastic?
true
end
def summary
"I really enjoy programming.
Over the last 2 years, I have spent most of my free time focusing on Ruby.
I also have experience with web development & SQL.
I am eager to learn much more about software development."
end
end
my = WilliamClarke.new
class Project
def initialize(args)
args.each { |key, value| instance_variable_set("@#{key}", value) }
end
end
my.notableProjects = [
Project.new(
title: 'PPC Campaign Builder',
summary: 'Ruby script which generates Adwords-formatted PPC Campaigns.'),
Project.new(
title: 'Website & Blog',
summary: %w(HTML CSS JS jQuery Rails ActionMailer Jekyll)),
Project.new(
title: 'Twitter Bot',
summary: 'Smug tweets to people who can’t spell. Hosted on Heroku.'),
Project.new(
title: 'Other projects',
summary: 'Crossword Generator, Shakespeare Prediction, Langton’s Ant')
]
Employment = Struct.new(:name, :title, :date, :role, :achievements, :skills)
my.employment = Employment.new(
'Forward3D', # name
'PPC Analyst', # title
'2012-08 - 2014-08', # date
# role
['Managed the paid search activity for two e-commerce clients.',
'Automated many processes using Ruby, Javascript and Hive.'],
# achievements
['Wrote a script to generate PPC campaigns, saving hundreds of man-hours.',
'Automated most of my reporting duties.',
'Update ads automatically based on product prices & stock levels'],
# skills
['Ruby, Javascript & Hive used on a daily basis.',
'Built, updated and queried Databases using HiveQL and SQL.']
)
my.education = {
durhamUniversity: [
'MSc Evolutionary Anthropology',
'BA Archaeology & Anthropology'],
etonCollege: { aLevels: [
['A', 'Ancient History'],
['A', 'Biology'],
['B', 'English Literature']
]
}
}
my.interests = [
['Programming', 'Ruby, Javascript, SQL & Web Development. Active on Github'],
['Sport', 'I enjoy playing squash, tennis, skiing and diving.'],
['Also', %w(Travelling woodworking Choral Music Go Piano Investing)]
]
# To print out to console:
require 'pp'
pp my
It actually works.. and spits out the data in an even more incomprehensible way.
10 Oct 2014
When I registered my website wmmclarke.com, I had no idea what a DNS was. According to wikipedia, the Domain Name Sysstem (DNS) is a hierarchical distributed naming system for computers, services, or any resource connected to the Internet or a private network. Not exactly immediately obvious…
The most common analogy for DNS records is probably a phonebook, which translates addresses that computers like (93.184.216.119) to addresses that humans like (wikipedia.org).
See my post about IP addresses.
This link is especially good at dealing with what happens when you navigate to a URL.
Anyway… I’m getting sidetracked. I’m going to show how I delt with Heroku & 123-reg.co.uk to set up my DNS properly:
I found this quite fiddly to get working… so here’s what I did:
1). Dealing with DNS
- Click on ‘Manage DNS’ and navigate to ‘Advanced DNS’
- Add 2 new CNAME:
- www
- DNS Entry: www
- Type: CNAME
- Destination Target: <your URL without the 'www' and with a '.' at the end>
- Everything else
- DNS Entry: *
- Type: CNAME
- Destination Target: <your URL without the 'www' and with a '.' at the end>
2). Getting rid of that pesky ‘www’ subdomain.
That should be it!
08 Oct 2014
I have recently spent a while trying to learn how to edit text effectively. I figure that if I’m going to be spending a while editing text, it’s worth learning how to do it as efficiently as possible.
I’ve been using YADR as a basis for my dotfiles. Dotfiles, if you don’t know, are files which start with a fullstop (and therefore are hidden in UNIX systems). Dotfiles mostly refer to configuration files in the home directory - eg. .bashrc or .vimrc. One of the benefits of using a monolithic package like this is it quickly lets you do more cool stuff as you’re learning (over boring native Vim).
I’ve put the extra files I’ve configured into my .vimrc.after file, which you can find HERE!.
Remap the keyboard.
The caps lock key is in a really important place but is largely redundant. You can remap it to something more useful (OS X: go to System Preferences > Keyboard > Shortcuts). For Vim users, the Ctrl key is very important. Remap it!
If you’re clever, you can also use the Caps Lock button as Esc (also very important for Vim users) by downloading Karabiner. This can set Escape for a single-press of Ctrl (which we now have at Caps Lock). Anything other Ctrl commands stay the same.
I’ve found that other keys are poorly placed; eg # and _, so I’ve remapped them too. Especially for Ruby development, I found it tricky to constantly press alt + 3 for #. Now it’s where ± is (next to 1).
Use good vim plugins.
YADR comes with a load of fantastic plugins:
These are the preinstalled plugins that I use most:
- Pathogen (a bundler for plugins)
- Vim Ruby & Vim Rails
- Solarized colours
- Vim sneak
- NERDTree
- Vim Surround
- CtrlR
I’ve added a few more of my own, too:
- Tmux-Navigator - Makes navigating Tmux panes just like navigating Vim panes
- Ruby xmpfilter - Gives you the ability to run a ruby script (or part of it) mid-edit.
- Vim-Rspec - Good Rspec integration from thoughtbot
- Jekyll - Easy Blogging from vim.
Use good keyboard shortcuts
The whole point of Vim is that it’s easy to define the shortcuts you like. Well.. maybe not the whole point, but it’s meant to be designed towards what works for you. So do what works for you. Having said that, I’m going to show the shortcuts which work best for me at the moment:
- the
! command lets you use normal bash functions
- the
% sign refers to the current file.
-
stands for carriage return (enter)
Using this we can make a mapping like this:
noremap <leader>md :!open -a 'Marked 2' %<cr><cr>
noremap <Leader>oc :!open /Applications/Google\ Chrome.app %<CR>
The : key is obviously important for Vim commands. The ; key is less important. Swap them!
jk can be a fast way of returning back to normal mode
Sometimes you can’t save a file. Something about an E212 error. Force it with this:
Shortcuts to save and quit:
inoremap <silent> <C-q> <ESC>:q<CR><ESC>
nnoremap <silent> <C-q> :q<CR>
noremap <C-s> :update<CR><ESC>
vnoremap <C-s> <C-C>:update<CR><ESC>
inoremap <C-s> <C-O>:update<CR><ESC>
This vim function lets you toggle Solarized background colour within the editor.
function! ToggleBackground()
let &background = ( &background == "dark"? "light" : "dark" )
endfunction
This lowers the timeout:
06 Oct 2014
I have been told that it’s good to write a blog. Something about self-promotion and helping other people…
I originally wanted to create something through rails, but, having thought about it, it would have taken a while to set everything up correctly. No need to reinvent the wheel…
Jekyll is a blogging / website platform built with Ruby. It’s what I would have aimed to build, only simpler, more efficient, pre-built and with an active community behind it.
Jekyll’s main selling point is that it integrates text files well into a static site.
It’s simple to use and set up out of the box:
~ $ gem install jekyll
~ $ jekyll new my-awesome-site
~ $ cd my-awesome-site
~/my-awesome-site $ jekyll serve
# => Now browse to http://localhost:4000
If you’re lazy, I’d recommend not doing this, though. Jekyll Bootstrap, discussed below, has a few more features that may be useful.
Jekyll Bootstrap
Jekyll Bootstrap uses Twitter’s bootstrap and includes a number of extra in-build functionality including clever rake tasks, themes and comments.
Here’s a really good starting point: Jekyll Bootstrap Quick Start Guide
One of the great things about Jekyll Bootstrap is that it integrates well with Github Pages. This lets you host your own site for FREE! You get the custom domain of your_github_username.github.io.
The only tweak that I had to make with the current version of Jekyll Bootstrap was to change the default github.com to github.io
Updating Your Settings
Go to _config.yml to update your settings:
- Github Username
- Twitter Username
- Analytics Tracking ID
- Disqus shortname
You’ll probably have to go to Disqus to create a shortname and Google Analytics to get your tracking code.
Generating New Posts
Once you have everything set up, you’ll be able to run
$ rake post title="Hello World" To create new posts or
$ rake page name="about.md" To create new pages.
There you have it - a free, fully functioning static blog set up in only a few minutes!
Here are a few more tips I recommend to make it even easier to use Jekyll and Markdown.
Using Jekyll with Vim
There’s a great vim plugin for jekyll. This lets you, anywhere in the file structure, to access your current posts or to create a new one.
map <Leader>jb :JekyllBuild<CR>
map <Leader>jn :JekyllPost<CR>
map <Leader>jl :JekyllList<CR>.
Ultra fancy Markdown editing.
I use Marked 2, which is a fairly slick markdown live previewer.
You can call it straight from vim using this mapping in your .vimrc:
noremap <leader>md :!open -a 'Marked 2' %<cr><cr>
(Assuming you’re on OS X)
I hope this’ll help some people get started with Jekyll. Good luck with it!
11 Sep 2014
Removing files from git is pretty straightforward. git rm ....
But if you want to completely remove files from git, after they’ve been committed, while keeping them locally, this can be tricky.
git update-index --assume-unchanged <file> should work.
02 Sep 2014
IP (Internet Protocol) addresses are ways of identifying unique internet-accessible devices. They are also ‘addresses’ in the sense of showing its location (think of postal addresses).
Background
When you type a web page into the computer, what you’re really asking is for some information from a different device (usually a big server somewhere in the world). There are several complex stages to finding this, but one of the key components is having an identifying ‘marker’ on each device; so you know which address to get the information from, and also so the information knows how to get back to you.
IPv4
Recently, we’ve been running out of traditional (IPv4) IP addresses: using 4 different 32 bit numbers (eg. 93.184.216.119), giving us access to 4,294,967,296 possible unique addresses (232)).
IPv6
IPv6 uses 2128 numbers, which is quite a bit bigger (340,282,366,920,938,463,463,374,607,431,768,211,456). An example of one of these is: 2001:db8:0:1234:0:567:8:1. To put that in perspective, 2128 E. coli bacteria (which are pretty small) would be around 26 times heavier than Earth (which is pretty big).
Take home point is we’re not going to be running out of IP Addresses soon.
25 Aug 2014
Writing to file in Bash
cat >/my/path.extension <<EOL
line 1,
line 2,
line 3,
line 4 line
...
EOL
Another thing about cat:
cat > file writes to file*
cat » file appends to file*
12 Jul 2014
Starting Vim
Using Vim & TMUX feels like it makes you faster after a couple of weeks.
It does take a while to get used to it.
Although I’ve read loads of contradictory advice, I think that it’s best to get started with a well-defined set-up. YADR is the one that I like.
If you’re unsure about trying vim, you could start a mini-turorial on your unix machine with
It’s a great resource to learn the basic motions & get the feel of Vim.