What Are The Rails 4 ActiveRecord Migration DataTypes?

Written by Jason Shultz on July 15, 2014

This comes up a lot, so I’m providing this handy list of Rails 4 ActiveRecord Migration DataTypes here for reference.

These can be used across all database types.

If you happen to be using a PostgreSQL database, then you have access to these as well:

  • :hstore - storing key/value pairs within a single value. (Learn more about this new data type)
  • :array - an arrangement of numbers or strings in a particular row. (Learn more about it and see examples)
  • :cidr_address - used for IPv4 or IPv6 host addresses.
  • :ip_address - used for IPv4 or IPv6 host addresses, same as cidr_address but it also accepts values with nonzero bits to the right of the netmask.
  • :mac_address - used for MAC host addresses.

Please note, that they will be stored as strings if you use those with a non-PostgreSQL database.

 


Ruby: Why Use Ruby’s attr_accessor, attr_reader and attr_writer?

Written by Jason Shultz on July 7, 2014

Ruby has this handy and convenient way to share instance variables by using keys like

attr_accessor :var
attr_reader :var
attr_writer :var

You may use the different accessors to communicate your intent to someone reading your code, and make it easier to write classes which will work correctly no matter how their public API is called.

class Person
attr_accessor :age

end

Here, I can see that I may both read and write the age.

class Person
attr_reader :age

end

Here, I can see that I may only read the age. Imagine that it is set by the constructor of this class and after that remains constant. If there were a mutator (writer) for age and the class were written assuming that age, once set, does not change, then a bug could result from code calling that mutator.

But what is happening behind the scenes?

If you write:

attr_writer :age

That gets translated into:

def age=(value)
@age = value
end

If you write:

attr_reader :age

That gets translated into:

def age
@age
end

If you write:

attr_accessor :age

That gets translated into:

def age=(value)
@age = value
end

def age
@age
end

Knowing that, here’s another way to think about it: If you did not have the attr_… helpers, and had to write the accessors yourself, would you write any more accessors than your class needed? For example, if age only needed to be read, would you also write a method allowing it to be written?


Ruby: What Does %w(array) Mean?

Written by Jason Shultz on July 7, 2014

%w(foo bar) is a shortcut for ["foo", "bar"]. Meaning it’s a notation to write an array of strings separated by spaces instead of commas and without quotes around them.

Also, the parenthesis can be almost any other character such as square brackets %w[...], curly braces %w{…} or even something like exclamation marks %w!…!. All of these have the same behavior (returning an array).


On Technology and Helping Others

Written by Jason Shultz on July 7, 2014

A friend of mine reminded me of this today. Let’s not be this guy:

Technology is great, and it’s empowering. We should all work to help spread it’s benefits to everyone. And, one of the most important ways of doing that is to take time to explain it and help others. If you just do it for them, people won’t learn. And, when we’re all learning and growing, we are empowering each other and making the world a better place. We all started this journey somewhere, together we can make it to the end.

And, if we remember that, then those we help will be able to say this:


The Hamburger Icon and What It Represents

Written by Jason Shultz on June 26, 2014
hamburger-icon

The hamburger icon tends to be employed because designers—or more often, clients—aren’t fully committed to a mobile-first approach. They want a ‘regular’ site, but squeezed onto their granddaughters phone.

In short, the hamburger icon is a symptom of our collective failure to wholeheartedly embrace all aspects of the mobile-first approach.

In order to solve the hamburger issue, we have to accept that the Web as we know it doesn’t work. The rise of the mobile web means far more than reducing the number of columns we use and dropping some of the heavier image files.

The mobile web is used in a different way to the web of old. The mobile web exists in the context of dedicated apps, and users expect the Web to be experienced in a similar fashion.

Link to Full Article on Web Design Depot