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.

Process-Checklist For How to Understand Any Mystery Method

1. Read through the method.

2. Make note of the first part of the method that you don't understand.

3. Search the code base for this method that you don't understand.

4. If it's not defined in the codebase, Google the method name. (Can be switched easily with step 3).

5. Read about how that method works.

6. Rinse and Repeat for each method (or, like, "character") that you don't understand.

Read More

Why Define A Method Using Self?

One line summary: when you see "self" in a method, think, "class method" instead of "instance method."

Great post on this subject: http://www.jimmycuadra.com/posts/self-in-ruby

When we create a class, and we create methods in that class, we have a choice. 

We can define methods that we can call from and onto the instance objects: (WITHOUT SELF)

class Post attr_writer :title def print_title puts "The title of this post is #{@title}" end end pst = Post.new pst.title = "Green Beans" pst.print_title # "The title of this post is Green Beans"

or we can define methods that we can call from and onto the class (WITH SELF)

class Post def self.print_author puts "The author of all posts is Jimmy" end end Post.print_author # "The author of all posts is Jimmy"

What is the difference?

There are two differences between class and instance methods.

The first difference is simply from where you can call the method. In the instance object case, you can call it from an instance of the object (2 steps, step 1: object = Class.new, step 2: object.henrys_method). In the class case, you can call it from the class. (Class.henrys_method).

The second key difference in how these objects interact, is that class methods are "global" - can be called anywhere in the application / specific language that you've written - and instance methods can ONLY be called on objects of that particular class, which need to first be created. (Hence the two-step procedure above). 

What are the different use cases?

Instance Object Methods

Instance methods are easy to understand. You want a method that you can call on the object itself, e.g. change the string in the "title" of an object. 

Class methods take a bit more imagination. But they also make sense! Take examples like Class.first or Class.find or Class.all. These methods enable you to interact with ALL the instances of a Class. Cool, right?

(Sidenote: Now, how would you do that with object.first, or object.find? You COULD do it, but first you would need to set up an object that contained all instances. And in order to do that, you would still need to use a Class method!)

NOTE: Ruby has THREE different ways to write class methods:

1 - def self.method

2 - class << self    (this often used in Rails apparently, and it can be used as a block)

3 - def Class.class_method

 

Read More

Initialize Method and Attr_Reader

http://stackoverflow.com/questions/16655915/intermingling-attr-accessor-and-an-initialize-method-in-one-class

Classes and methods

Now we are ready to create our very own Address class. Let's start simple. Let's start with an address that only contains the "street" field.

This is how you define a class:

class Address
def initialize(street)
@street = street end
end

Let's go through this:

  • The class keyword defines a class.

  • By defining a method inside this class, we are associating it with this class.

  • The initialize method is what actually constructs the data structure. Every class must contain an initialize method.

@street is an object variable. Similar to the keys of a hash. The @ sign distinguishes @street as an object variable. Every time you create an object of the class Address, this object will contain a @street variable.

Let's use this class to create an address object.

address = Address.new("23 St George St.")

That's it. address is now an object of the class Address Reading the data in an object

Suppose that we want to read the data in the address object. To do this, we need to write a method that returns this data:

class Address
def initialize(street)
@street = street end

# Just return @street
def
street @street
end
end

Now the method Address#street lets you read the street of the address. In irb:

>> address.street => "23 St George St."
Read More

Enabling Controller variables in Rails View WITHOUT declaring instance variables

Obviously the core pattern to send objects  from the Controller into the view, is to save them as instance variables (@variable) in your controller and then access them in the view.

However, what if you don't want to use an all-powerful instance variable? What if you don't want to have lots of instance variables floating around? What if you just need a variable for one tiny controller and one tiny view?

Well, you can use the method of 

render locals: { regular_variable_name: ClassObjectLogic }

for example:

instead of @posts = Posts.all

you could do

render locals: { posts: Posts.all }

Read More

Access Gem Modules and Classes

Say you want to peer inside a gem installed in your Ruby version.

Gems listed in a Rails project Gemfile are NOT installed in your Rails App.

Instead, your Gemfile manages their installation in Ruby version folders inside the Users/Henry.vanwagenberg/.rvm/gems/ruby-2.1.5/gems

(Assuming that you are using RVM, which you probably are).

In addition, because it's so common so peer inside Gems, the Rails Bundle feature also includes a method to call your Gem and open it up, without having to navigate into your RVM Ruby 2.1.5 folders etc.

That method is: bundle open gem_name

Usually, it will ask you which editor you want to use to open it. To use VIM, you could write:

EDITOR=vim bundle open gem_name

Read More






Footerfull