Dev box
I'm a 32-year-old Ruby on Rails web developer. With the help of friends and co-workers, I've been teaching myself Rails. I worked as a Rails developer for Crowd911 in Colorado. I live in Berlin.
In my developer blog I publish a stochastic array of thoughts and ideas, lessons, mistakes, questions and attempts as answers about Rails and Ruby. I am also posting Anki cards that I used to help myself study. In my view most knowledge is about practice. If you have thoughts, questions, ideas, corrections, arguments, feel free to email me at henryvw@gmail.com.

Converting in Ruby between strings and symbols

:sasquatch.to_s

„sasquatch“.to_sym

„sasquatch“.intern 

(.intern is the same as .to_sym)

Read More

Ruby Hashes - including "hash constructors"

Ohne titel

Hashes

When you create a hash from the „hash constructor“ - i.e. simply new_hash = Hash.new — just remember that you’re doing the exact same thing as new_hash = { „one“ => 1 }.

With new_hash = Hash.new, you are literally just… creating an instance of a Class (Hash) that already exists in Ruby!!  And assigning your own data to it!!!  

When you iterate over Hashes, remember you have keys and values, so the iterator needs to have those  separated.  Say, for example, you only want to print the value.  

BUT there are also actually specific hash .each methods to cover these situations:

.each_key

.each_value

Read More

Ruby, memory use, and garbage collection

Ruby is a garbage-collecting language.  Garbage collection has to do with memory. That’s the reason that String IDs change.  Ruby is constantly cleaning up the memory of unused strings during the duration of the program.  Also, anytime the whole program ends, everything, all IDs, etc., are deleted from the program.

Read More

Blocks are methods without names

Blocks are methods without names.  It is just a bit of code between do..end or { }. It can be passed to methods like .each or .select.

You CAN set a variable to a block, e.g.

doubled_fibs = fibs.map |number| do 

number * 2

end

 

But just keep in mind that the block doesn’t go on living as a separate item by itself, it’s just a piece of code that does something, that you happen to have set to a variable.  It’s not like a method or a proc where it’s saved and can be reused. 

A related tangent: Methods don’t need classes!

Think about it… methods don’t need classes all the time.  Sometimes, you have a method that just stands alone.

You use classes to organize bundles of methods, and to create objects that automatically are imbued with the ability to call the bundle of methods.

To pass a block to a method, you simply type the method and then the block.  For example:

greet       { puts „Hi!“ }

If the method has parameters, then you place it first: 

verbose_greet(„Dave“, „loyal customer“)  {puts „Hi!“}

Read More

Git – How to set up a remote repository

Inheritance

There is a great deal of documentation and many posts on Git out there, so this is more of a note to self as I keep forgetting the steps needed to set up a remote repository and doing an initial “push”.

So, firstly setup the remote repository:

on github.com, go to 

On local machine:

cd my_project

git init

git add .

git commit -m "My initial commit message"

git remote add origin https://github.com/Henryvw/new_app

git push -u origin master

Done!

Team members can now clone and track the remote repository using the following:

git clone https://github.com/Henryvw/new_app

cd my_project

Bonus

To have your terminal prompt display what branch you are currently on in green, add the following to your ~/.bash_profile (I have my current directory displayed in cyan):

function git-branch-name {

  git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3

}

function git-branch-prompt {

  local branch=`git-branch-name`

  if [ $branch ]; then printf " [%s]" $branch; fi

}

 

PS1="\u@\h \[\033[0;36m\]\W\[\033[0m\]\[\033[0;32m\]\$(git-branch-prompt)\[\033[0m\] \$ "

 

Resource I used for this post: http://thelucid.com/2008/12/02/git-setting-up-a-remote-repository-and-doing-an-initial-push/

Read More






Footerfull