Note: I'm using Rails 3.0.3.
I decided to use jQuery instead of Protoype on my personal Rails 3 project. At first, I naively thought that I could simply include jQuery instead of Prototype, and everything would run just fine. Instead, I noticed that Chrome was reporting an error in rails.js, which eventually led me to Josh Huckabee's blog post, as well as the official jquery-rails gem.
The installation instructions on the github page are straightforward and simple, so I won't reproduce them here. After you add the gem to your Gemfile and install it, and you run the command:
rails generate jquery:install
it will pull down the latest version of jQuery. You should answer "yes" when it asks if you want to overwrite rails.js.
I modified my default layout (application.html.erb) to include this line:
<%= javascript_include_tag "jquery-1.5.2.min", "jquery.jeditable.mini.js", "rails", "application" %>
Of course, you might not need some of these, and you can include additional files as well.
Saturday, April 16, 2011
Friday, December 31, 2010
vim tip: searching for word under cursor
I was motivated to use vim a few years ago, after watching Corey Haines do his number to LCD kata at the Simple Design and Testing Conference (SDTConf). Although I learned the basics while I was in college, I never really tried to progress beyond that until recently.
Now I try to make a habit of whenever I am doing something that just seems like it's the hard way, I'll google it and learn how to do it better.
Today, I was thinking: there has to be a better way to search for things rather than typing "/thing_to_search_for". So I found out how to search for the word under the cursor, which is # for searching backwards, and * for searching forwards.
Hope this helps, and motivates you to keep learning something new everyday.
Now I try to make a habit of whenever I am doing something that just seems like it's the hard way, I'll google it and learn how to do it better.
Today, I was thinking: there has to be a better way to search for things rather than typing "/thing_to_search_for". So I found out how to search for the word under the cursor, which is # for searching backwards, and * for searching forwards.
Hope this helps, and motivates you to keep learning something new everyday.
Thursday, December 23, 2010
Installing ruby 1.9.2 with rvm
I've been having trouble installing ruby 1.9.2 with rvm on Mac OS X.
Although there are lots of great blog posts with solutions, for some reason, none of them worked for me.
The rvm page and many posts say to pass
However, I kept getting errors about Tcl/Tk not being 64-bit, and iconv also not being 64-bit.
Why would it be complaining about iconv, especially since I used rvm to install it?
I noticed the config.log spit this out:
Success!
The output:
Hopefully, this helps some of you out there with similar problems.
Although there are lots of great blog posts with solutions, for some reason, none of them worked for me.
The rvm page and many posts say to pass
--with-iconv-dir to rvm install.However, I kept getting errors about Tcl/Tk not being 64-bit, and iconv also not being 64-bit.
Why would it be complaining about iconv, especially since I used rvm to install it?
I noticed the config.log spit this out:
configure: WARNING: unrecognized options: --with-iconv-dir
I tried many options that were in configure, but none of them pointed the makefile to rvm's version of iconv.
So I took a look at /usr/local/lib/libiconv.2.dylib and noticed it was a symlink.
Perhaps this is taking the easy way out, but I backed it up by renaming it, and relinking that to the version inside $rvm_path.
Perhaps this is taking the easy way out, but I backed it up by renaming it, and relinking that to the version inside $rvm_path.
After that, I recompiled by running:
rvm install 1.9.2 -C --enable-shared
Success!
The output:
Hopefully, this helps some of you out there with similar problems.
Sunday, December 05, 2010
Installing Nokogiri with bundler
I recently decided to start contributing to open source, so I chose to look at one of my favorite Ruby gems, rspec.
The instructions to get you started are fairly straightforward; however, in the rspec-core repository, bundler was failing to install nokogiri. Although the instructions here worked for some, it didn't work for me. So based on the instructions on the Nokogiri homepage, I used macports to install libxml2 and libxlst, re-ran rake, and finally, bundler was able to install nokogiri, with no problems.
The instructions to get you started are fairly straightforward; however, in the rspec-core repository, bundler was failing to install nokogiri. Although the instructions here worked for some, it didn't work for me. So based on the instructions on the Nokogiri homepage, I used macports to install libxml2 and libxlst, re-ran rake, and finally, bundler was able to install nokogiri, with no problems.
Friday, October 15, 2010
"Failure of State" by Uncle Bob - SCNA 2010
This is the first of many blog posts to come, with my notes on the talks given at SCNA 2010.
This morning, on the first day, Uncle Bob gave a talk titled "Failure of State." My notes on the talk are below.
SICP - Uncle Bob has mentioned this book many times, and I finally put it on my Kindle today. As a craftsman, you should definitely read this book. It's free but if you can, buy it to support the authors. There are also free lectures available as well.
The substitution model - you can replace function calls repeatedly, ending up with a "silly" version but one that is also stateless and thread-safe. The example given was a program that printed the squares of the first twenty (20) integers.
Many stateful programs exhibit temporal coupling; that is one statement (function/method/etc) must be called before another. In other words, time matters. For example, opening a file, then closing it.
However; in functional programming, y=f(x) no matter when you call it and every time you call it.
Programming has seen three failures so far: 1) the GOTO statement; 2) pointers to functions, and 3) state. Finally, Uncle Bob showed the CQRS pattern, where CRUD is separated into CUD and R.
Tuesday, September 28, 2010
JavaScript documentation
I am doing my part to help others find good JavaScript documentation and tutorials.
Googling "JavaScript tutorial" or "JavaScript documentation" doesn't provide any good links.
The Promote JS campaign aims to change this, so that we all can find better JavaScript documentation, such as the docs here.
Please spread the word! Go to http://promotejs.com/ for more information, and place a link and banner on your blog or homepage.
Googling "JavaScript tutorial" or "JavaScript documentation" doesn't provide any good links.
The Promote JS campaign aims to change this, so that we all can find better JavaScript documentation, such as the docs here.
Please spread the word! Go to http://promotejs.com/ for more information, and place a link and banner on your blog or homepage.
Wednesday, November 18, 2009
Objective-C Object Instantiation
Today we'll go over the syntax to create an instance of a class.
New?
In Java (and C++, to dynamically allocate an instance of a class), you use the new keyword. Objective-C's equivalent is the alloc keyword.
I wish I could say that's the end of the story, but there is a bit more.
Initializers
As in Java and C++, a constructor, and its superclass' constructor, and the superclass' superclass' constructor, and so on, are called in order from superclass to subclass to its subclass, etc.
In Objective-C, constructors are called initializers, and you can consider the init: method to be equivalent to a default initializer; i.e. one that takes no arguments and is supplied by the compiler unless you specify one yourself.
What if you wanted to create initializers that take arguments? By convention, it's recommended to:
Another convention is that the initializer initializes only the attributes that need to be initialized, and any other values can be set w/setter methods or through other means.
Return type
A"gotcha" compared to C++ and Java: the return type on these initializers have a return type of (id), not the name of the current class.
Designated initializers
Objective-C has the concept of a designated initializer. This is defined as the one that guarantees that all instance variables are initialized. While your subclass does not necessarily have to invoke a designated initializer on its class, it eventually must call a superclass' designated initializer. NSObject, the superclass of all classes, which is similar to Object in Java, contains a designated initializer called simply, init:.
What to return in an initalizer
An initializer either returns "self" which is like "this" in Java and C++, upon successful initialization, or nil if a failure occured.
Ok, so how do I put this all together?
Although you could make two separate calls and "throw away" the value of alloc, it's not recommended. The convention is to chain the calls together on one line like this:
SomeClass *pointerToSomeClass = [[ SomeClass alloc] init];
New?
In Java (and C++, to dynamically allocate an instance of a class), you use the new keyword. Objective-C's equivalent is the alloc keyword.
I wish I could say that's the end of the story, but there is a bit more.
Initializers
As in Java and C++, a constructor, and its superclass' constructor, and the superclass' superclass' constructor, and so on, are called in order from superclass to subclass to its subclass, etc.
In Objective-C, constructors are called initializers, and you can consider the init: method to be equivalent to a default initializer; i.e. one that takes no arguments and is supplied by the compiler unless you specify one yourself.
What if you wanted to create initializers that take arguments? By convention, it's recommended to:
- begin the initializer name with init
- name the initializer to include the word "with"
Another convention is that the initializer initializes only the attributes that need to be initialized, and any other values can be set w/setter methods or through other means.
Return type
A"gotcha" compared to C++ and Java: the return type on these initializers have a return type of (id), not the name of the current class.
Designated initializers
Objective-C has the concept of a designated initializer. This is defined as the one that guarantees that all instance variables are initialized. While your subclass does not necessarily have to invoke a designated initializer on its class, it eventually must call a superclass' designated initializer. NSObject, the superclass of all classes, which is similar to Object in Java, contains a designated initializer called simply, init:.
What to return in an initalizer
An initializer either returns "self" which is like "this" in Java and C++, upon successful initialization, or nil if a failure occured.
Ok, so how do I put this all together?
Although you could make two separate calls and "throw away" the value of alloc, it's not recommended. The convention is to chain the calls together on one line like this:
SomeClass *pointerToSomeClass = [[ SomeClass alloc] init];
iPhone / Objective-C tips
Even if you've programmed in C, C++, and/or Java, some of the Objective-C syntax might take a while to get used to.
Here are a few tips to keep in mind when you're starting out.
#import "AClassThatINeed.h" : Referring to other classes
In C++, when you want to refer to another class, you #include the appropriate header file. You can safely #include it when you put #define/#ifndef guards. In Java, you only need to import the class if it's in a different package than the class you are currently modifying.
However, in Objective-C, it's a little different. Let's see how.
Let's say you're in the header file for SomeClass (SomeClass.h), and you need to refer to other classes. You could #import those other header files, but most likely you'll run into compilation issues. Instead, just add a forward declaration, such as @class AClassThatINeed
Then, when you're in the implementation file (SomeClass.m), you can safely #import "AClassThatINeed.h"
So, to sum it up: use @class in your header files and use #import in your class files.
Method (Message) declarations, and stranger syntax: calling them
This can get a little confusing. The first parameter, at least as far as I know, is not required to be named, whereas any additional parameters, while not required, should be be explicitly named to improve the readability of your code.
Example:
- (void)setWidth:(float)width
- (void)setWidth:(float)width height:(float)height;
In the first listing, setWidth only takes one parameter, and might be called like this:
[myObject setWidth:25.0];
The second one will be called like:
[myObject setWidth:25.0 height:32.5];
Notice how the second version requires us to use the argument name before the colon that was in the method declaration. This was supposedly done to improve readability of the code when you have long parameter lists, but personally I think that's a code smell. While I do think having the labels helps out, having 3 or more parameters just adds more noise.
Here are a few tips to keep in mind when you're starting out.
#import "AClassThatINeed.h" : Referring to other classes
In C++, when you want to refer to another class, you #include the appropriate header file. You can safely #include it when you put #define/#ifndef guards. In Java, you only need to import the class if it's in a different package than the class you are currently modifying.
However, in Objective-C, it's a little different. Let's see how.
Let's say you're in the header file for SomeClass (SomeClass.h), and you need to refer to other classes. You could #import those other header files, but most likely you'll run into compilation issues. Instead, just add a forward declaration, such as @class AClassThatINeed
Then, when you're in the implementation file (SomeClass.m), you can safely #import "AClassThatINeed.h"
So, to sum it up: use @class in your header files and use #import in your class files.
Method (Message) declarations, and stranger syntax: calling them
This can get a little confusing. The first parameter, at least as far as I know, is not required to be named, whereas any additional parameters, while not required, should be be explicitly named to improve the readability of your code.
Example:
- (void)setWidth:(float)width
- (void)setWidth:(float)width height:(float)height;
In the first listing, setWidth only takes one parameter, and might be called like this:
[myObject setWidth:25.0];
The second one will be called like:
[myObject setWidth:25.0 height:32.5];
Notice how the second version requires us to use the argument name before the colon that was in the method declaration. This was supposedly done to improve readability of the code when you have long parameter lists, but personally I think that's a code smell. While I do think having the labels helps out, having 3 or more parameters just adds more noise.
Subscribe to:
Posts (Atom)