Tag Archives: ruby

Gem Install Gone Wrong

Today I was working on my Gem  Toolshed and wanted to install it globally. That way I wouldn’t have to add it to each of my projects Gemfile. Then run bundle install on each. The reason why I didn’t want to do that was first of all would take more time than I wanted. Also if I could just install it globally I wouldn’t have to worry about installing it for new projects. Also for this type of Gem it’s nice to be able to use it outside of a projects context. So I attempted to install it globally so that it could be used by all my projects. I am not too familiar with rvm or bundler to be honest. I have used both of them but I don’t understand enough about them to be called an expert. So I ran the following command in an attempt to install it globally. Note that I did not pull this command out of thin air I found it on a stackoverflow post and it looked promising.

gem install /home/path/to/gem

After spending several hours debugging it I finally figured out what was happening. Well it turns out that not only does that command not work but it also switches the gem install default to that directory. I figured this out through several debugging steps. The first step was when I went into a different project and did bundle install. This is a project that I have been working on for a while so I was expecting it to just work. Well when I ran bundle install I seen it was looking at my gems directory. I was thinking well something must be in the path and kind of just ignored it. I didn’t put the two together for a while. After thinking about it for a while I thought that bundler has to be smart enough to know my rvm is setup to use a specific gemset. Well it turns out that is not the case if you have a customized path like I did. It installs it to whatever directory you put in that path above. I verified that this was happening by going into my Gems directory and there is was a ruby directory with ruby installed. After doing some searching on how to fix it I found that this worked to reset the gem install back to the default directory.


bundle install --system

After doing that my system was back to normal and I was able to run bundle install again without any issues. I hope that this post can provide some insight that bundler and rvm don’t always work hand and hand. If you ever try the gem install command above remember to set it back so all your bundler commands work as expected. At least I can say I now understand more about bundler and rvm as a whole.

Fixtures Vs. Factories

I have used both fixtures and factories in writing tests. I actually use both currently I use fixtures for my personal project and factories at work. I feel that their can be benefits to both but if I had to go with one I would go with fixtures. It seems to me that you can do everything and more with fixtures. Also your test suite stays a little more organized.

The first question one may ask is what is the difference between a fixture and a factory. Well a  fixture is a set of data that is static in nature. This data will get loaded for each test run. A fixture is created using a static file within your testing structure. This is different for each language you use it in. For example in Rails it will be a yaml file while in PHP it’s just an array. This could also depend on the framework your using. If you want the table to start out empty you can create an empty fixture file. Having the static file allows you to setup specific test scenarios. Then you can write your tests around those scenarios and you know the data will always be the same. So if a test shall fail you know it’s not because of a dataset change. In contrast factories create the object and the data at the same time. The database is not stocked with any preloaded data. If you need data for your specific test case you need to call the factory and have it create the data for you. Instead of creating a static file you need to create a factory file. This file will contain the name of the factory and then any custom data you need. Using a factory can have some benefits as you will get random data so your code is being tested against unknown data. Which may be reasonable as you have outside users using your system passing in random sets of data to.

So why do I feel that fixtures are better than factories if fixtures take longer to setup. I find it that factories tend to get messy as you have data objects being created all over your test suite. No factory is identical so you tend to have to work though a lot of different data scenarios. Also if you don’t setup your factory to pass in the proper data you could get false positives. For example maybe your interface validates a field to be one of three values. But you forget to setup your factory with one of those values. Now your tests are failing but the interface and application are running just fine. I suppose you could make the argument you can make the same mistake in a fixture but I feel you have to think about it more as you are filling this data in. Another reason why I don’t think factories are better is they are slower running. For example if you create your fixture files they run once per test run. But they run fast because they don’t have to use active record and create the object. Factories do create an object while creating the data. This takes extra time especially if you have setup a lot of data in your scenario. Another issue I have with using factories is you have to setup your data scenario on each test. You can keep it dry in a way by calling sub classes but again a lot of times you end up just doing factory.new because it’s a one off type of setup. So you tend to duplicate setting up factories a lot especially in a larger development team.

I feel that using either factories or fixtures will be fine. But I feel that fixtures gives you a slight edge for keeping your test suite organized. You are able to write tests against predictable scenarios. Also you are not having to repeat the same datasets over and over again. I feel that this is easier to avoid using fixtures rather than factories.

Ruby Gems

I have been using Ruby for about two years now/ I have used various Gems throughout those years. I recently decided to look at more than just the popular gems out of curiosity. Sort of to see what is out there and what people are creating. While doing this I noticed that there is a lot of really crappy gems. Not only are there a lot of really crappy gems but some of them literally don’t do anything. They are just a stub when you run gem create [gemname]. I got to thinking why hasn’t Ruby put some system in place to verify that one the gems actually do something but two that they have some quality.

I feel that to fix this problem they need to implement  a rating system so if your gem gets voted down to much it should be removed. That name would then become available for others to use. A comparison I would make to this is the Android market. They have structure in place that at least makes you be semi serious about developing a good application. First you have to pay a fee to publish to the market. Now for Ruby gems that may not work because there is no profit being made off of these gems. More that Ruby gems needs to come up with some criteria that defines you to have the ability to create these gems. At the end of the day I’m sure it doesn’t matter that much but it’s annoying searching through a bunch of crap gems just to get to what you want. When you have been doing it long enough though you tend to know what are good gems and which ones are a waste of time.

Ruby Duck Typing

In Ruby duck typing means that it does not rely on the type of class. It’s more concerned about what the class can do. The object is only defined by what it can do and is not limited by a specific type. In Ruby if you need to find out if the object implements a specific method you can use the respond_to? method. The reason why it’s called duck typing is a little saying in Ruby if it walks like a duck and talks like a duck then it’s a duck. That is where the saying came from.

In contrast Java does not use duck typing. You have to declare specific types within methods and classes. Also you have to declare the return type within the method definition. So it’s not as flexible as Ruby. Therefore it’s not considered to have the ability of duck typing.

Frameworks

When I started out my development career I thought the idea of a framework was stupid. If I already know the language then what was the point in learning a framework. It’s just another thing I have to know about. At the time my mindset was I can do anything the framework can do. Yeah I might have to spend the time coding it up but at least I understood it at the end. Also a lot of the core functionality you need when you are creating a site is built into the language itself. For example PHP has all of the date functions one would need. At the time PHP wasn’t really not an object oriented language either so there wasn’t frameworks built for it when I started my career. When PHP was enhanced to support objects they kind of came over time. At that time I was in the mindset that I didn’t want to learn a custom framework. Since I already was doing everything myself. Then it all changed when I worked at a company who started working with a framework. After struggling for a little bit while first learning it I am glad I spent the time.

The main benefit I see from a framework is consistency. If you start with a framework and follow the frameworks pattern’s your code base will be consistent. It makes adding new developers onto the team way easier also. You are able to point them to documentation already created. Also that documentation is always up to date. If you are rolling your own framework most of the time documentation is the last step or in most cases doesn’t get done. Not only will you have consistency but you will not trap yourself into a bad pattern. If you choose a popular framework you know it has been tested on a wide variety of applications. If it can handle all of those applications you know it can handle yours. Unless you are trying to do something way out there.

Another benefit from using a framework is you get to use the communities code. Most frameworks at least the good ones have some form of extensions. They are called something different with each framework. For example in the yiiframework they are called extensions while in ruby they are called gems. These extensions allow you to easily pull in code and modify it to your needs. You save yourself a ton of time by not having to write that datatable helper or the audit helper. A lot of this is already done. Why not use something you know is working for others? You can then focus your time on the important things. No need to reinvent the wheel.

There can be some drawbacks to using a framework. One thing we face these days is frameworks are becoming to heavy. They have a ton of code that projects may not use. Also they kind of force you into a certain way of doing things. You can always code your way out of it but it might take you extra time. Another drawback is if you fall behind on major versions. If you fall behind upgrading can be a big pain. If you don’t upgrade to the latest version you could have security issues or extension compatibility issues.

At the end of the day you want to get your website up and running super fast. When you have a framework to start with you can have a simple application up and running in minutes. Also you don’t want to spend you time writing common code that is already been done a million times by other developers. Instead you want to focus on creating the customized aspects of your website or product. Even though it does take a little investment learning the framework it’s well worth the time. After you learn one framework you will see the pattern throughout other frameworks will be very similar.