My name is Tyler. I'm learning Ruby on Rails. This is a blog for me to talk about what I've learned. I'm no longer completely new to Ruby and while I don't feel comfortable calling myself a coder or programmer yet, I am absolutely a Ruby Enthusiast and a Rails Fancier. I've been working with Ruby on Rails since February 1, 2011. You know that song about "lovin' every minute of it?" Bingo.
Sunday, January 22, 2012
CODETOBER, other code-related things I've been up to
This applies to coding because they want a way to more accurately track where their users are coming from, so this week I'm going to look into coding something that will pull various bits of information into a more readable format. Sound cool, right?
I've also got an interview at iGoDigital on Tuesday, for a junior developer position. I interviewed there back in September, but it wasn't a good fit at the time. Now? Maybe a better fit! It's kind of funny. When I first got into Ruby, I wanted to make more money and programming seemed like a good way to do that. Now I'm less concerned about making more money, and I largely want to become a better programmer. Working full-time would definitely help me improve my skills, especially in a team environment. So I'm pretty excited about this opportunity.
I'm still gathering ideas for CODETOBER, since I really need to a) code more and b) blog about coding more. So far my list is pretty sparse, and I'm still open to suggestions if you've got them. What kind of things should I be looking into? What would be worthwhile, or valuable, or fun, or frustrating, or otherwise teach me a life lesson? I'd like to see more developers doing crazy things in February.
Now I think I'll sit down and work on a stupid project. I'm kind of losing interest in Dungeon Crawler (SURPRISE!)
Thursday, January 12, 2012
Announcing CODETOBER
But I've got an idea to remedy this. I absolutely SHOULD be blogging more often, and I'm going to take a page from my friend Miles' book and try to learn something new every day in February and then blog about it. I want to have an awesome name for this month, so I'm probably just going to call it CODETOBER. Also it's a leap year this year. WEIRD!
I've got a list going for things I want to look into or try to understand a bit. Mostly things I've heard mentioned often enough that I want to look into them, or how to use tools I'm already familiar with more effectively.
This list is pretty short, though. I'm definitely interested in learning how to dry up my code, since I feel that's a big weak point for me. And getting more familiar using Git for source control, learning more terminal shortcuts, Sass, Cucumber, HTML5 (since I'm a sucker for front-end design anyway) and I'm interested in getting more recommendations. What do you think would be beneficial to look into? Useful gems, fun tutorials, etc? I'M COUNTING ON YOU, INTERNET!
In more concrete coding news, I hung out with my friend Eli and he showed me how to dry up my Dungeon Crawler code quite a bit. I've now got one method that generates the rooms as opposed to 10 different methods! Still pretty soggy around the edges, though. And through the middle. It's actually mostly still pretty soggy.
Friday, December 30, 2011
How do you deal with coder's block?
This is extra irritating because I'm pretty sure I DO get how routing with Sinatra works, but starting a user out on one page with an input form, then redirecting that user to another page based on his input, seems to be escaping me.
I've gotten this to work in a limited capacity, when all of the logic is thrown into the
get '/input' do
method, but using a case/when statement here always uses the same logic. For example, dais_room will be north of torch_room, but vine_room will be north of dais_room, and I was always being redirected to dais_room with input "go north."
I also had a problem of simply being redirected to a white page with black text pulling the description of the room. I believe this was the method @netshade showed me for automatically creating a room without explicitly coding it, but for some reason it was not loading a view file.
Here's a full gist of what I've got, if anyone wants to give me some srs help:
Dungeon Crawler full gist
What do you guys do when you get so frustrated with a project? Do you power through, work on something else, or just take a break? I feel like since the holidays began, I've found a ton of great excuses not to code. It's Christmas Eve! It's Christmas! I just had LASIK surgery done!
But I'm sick of making excuses and I want to get back into working on a project without feeling like a gigantic dummy or just tearing my hair out because I can't figure it out. That makes sense, right?
It's not all bad news, though. @netshade and @annamul gave me The Pragmatic Programmer, a philosophical/theoretical book about coding. I really like the PragProg guys, and this book has been a lot of fun to read so far.
I think I need some way of setting goals for myself each week with coding. As in, "This week get better with using Git for source control," or "This week, get more comfortable with Javascript." Any recommendations for simple goal setting applications, or methods you've used? I use Tadalist for simple day-to-day chores and things, but that's not quite what I want. Maybe Evernote would work? I still haven't checked into that.
Either way, thanks for all your help, everyone! And Happy New Year!
Wednesday, December 7, 2011
More work on Dungeon Crawler
I've drawn out the map of the entire dungeon (to help with actually laying it out when coding) and I've written methods and routes for all the rooms. Currently I'm loading a separate view for each room, since I eventually plan to have hand-drawn images representing the rooms themselves (which get updated when things happen, such as chests are opened, items discovered or moved, etc) so I thought this was the best way to handle it.
I've thought of ways to do the entire dungeon with a single view file and simply loading different information when the player makes certain choices, but for now I'm leaving that for a refactoring. Strangely enough, I think the hardest part of doing it this way would be figuring out a method for changing the image that represents the room, not the actual routing or the get method that would change the room. Strange!
I'm also not worrying about the individual features of rooms just yet, such as locked doors, traps, monsters and so on. My immediate goal is to be able to move throughout the entire dungeon, making sure all the routing works correctly and that you can't move into a room you shouldn't be able to.
Sinatra has proved a lot of fun to work with, in addition to being incredibly simple. It's kind of fun to have to do my own routing, too.
Also, I think it's important to name a project, so I'm calling this one Dungeon Crawler. (This was always the name of this project, even when it just lived in a Terminal window, but I'm not sure if I've mentioned that here or not)
Finally, I learned a great trick. I'd originally named my .rb file "dungeon_crawler.rb" and it quickly became tedious to type "ruby dungeon_crawler.rb" in the Terminal every time I make a change and have to reboot Sinatra, so I renamed it to "dc.rb". Much less tedious! My guess is that coders learn tons of tricks like this over the years.
Thursday, November 24, 2011
Working on Skyrim Stories
Take a look:
Skyrim Stories
Basically what this site is doing is searching Twitter for tweets tagged with #skyrimstories, and displaying the 15 most recent Tweets. My friend @netshade sent me the javascript for it, and my friend @jqr got the domain set up and (gently) harassed me to work on it.
So far I've had a lot of fun with this project. It's been good for me for a few reasons: first, the time between thinking of the idea and having a working first version was about 2 hours or so. Within a few minutes of calling @jqr for advice, @netshade had already sent me the code and @jqr had set up the domain. It feels good to get work done really quickly, especially since I sometimes feel like it takes forever for me to get ANYTHING done. It's also been a good opportunity to get my hands dirty with some CSS and I always enjoy styling. And lastly, it's reinforcing what I've known for a long time: programming is programming, and it doesn't make sense to use Ruby or Rails for every project, especially if there's an easier, more elegant solution.
A few notes here: First, I've got very limited experience with Javascript. I can typically take some code, figure out how it works and manipulate it to do things I want it to do, but I can't write my own. For example, the code @netshade sent me worked pretty well, but it was displaying user names instead of users, so instead of displaying a tweet from @illbzo1, it would display it as being from Tyler Moore. The code also displayed retweets by default, but a quick look through Twitter's Get Search API and I was able to fix that. Especially at this stage where I'm not paginating older tweets, I don't want the entire feed to get populated by retweets. And retweets don't really fit with the idea of this site, anyway.
I didn't invent #skyrimstories, but it's already (kind of) popular, so I think with a bit of promotion and some more polish, this could really be a fun site. At least until everyone gets sick of Skyrim, but that'll be another few months, at least.
I've still got a few things I want to do with it. First off, I want to strip out #skyrimstories when the tweet is posted to the site, because I think it'll read better without that Twitter-specific language. I'd also like to make the username displayed with the Tweet a link to that user's Twitter account. I'll also want to look into a way to paginate older Tweets, displaying 15 per page or so.
Anyone with more Javascript powers have any advice?
Here's a link to the project on GitHub:
Skyrim Stories on Git
Wednesday, November 9, 2011
Finished random name generator in Ruby
Random Name Generator
I'm calling it aan (short for adjective, adjective, noun) because the original was just three phrases slapped together. Now I've added functionality that will generate a prefix or a suffix (seemingly) at random. I might spend a bit more time working with this. It's been a fun little project and I'd like to make a Rails project for it so I can put it up on Heroku and show it to non-coders.
Some other things I may look into:
A way to output the full name on a single line. I tried print instead of puts, but it simply outputs the pieces of the array, not the entire string built from the various arrays.
A way to capitalize every word except for words like "of", "the" and so on.
Using this with other data. What about a Full House episode plot generator? What if instead of specifically saying "Uncle Jesse takes Stephanie to the movies, which makes DJ incredibly jealous" I had "DAD takes DAUGHTER1 to the EVENT, which makes DAUGHTER2 incredibly jealous" and I passed in variables, to further randomize output? Maybe this will be my next project. It kind of combines stuff I was doing with the MadLib generator and this random name generator.
One of the things I like most about Ruby is how much you can do with it once you have an idea of what you're doing. I've definitely got a better idea of how reading files and using data found in those files works now. It's also pretty awesome to sit down for a couple of hours and bang out a working program.
I've also found that since I've become less focused on making a career change and more interested in learning how Ruby works, I'm having a lot more fun coding. That's a plus, right?
Tuesday, October 11, 2011
Calling a method in another method in Ruby
I had a really good day with the project I'm currently working on as part of Learn Ruby the Hard Way: a text-based adventure. It's a pretty simple concept, but so far I've got a working four room dungeon. And it's a game! Sure, it's a basic, text based game, not even as complicated as Zork, but it works! Do you know how awesome it feels to build something that works? REALLY AWESOME.
Today at work I was thinking of ways to expand it. How about adding a "help" command to give players some commands that will allow them to manipulate objects in the dungeon? How about a "look" command to remind players which room they're in and to point out important features of that room? How about a "quit" command to let players quit the game?
Done, done and done. Pretty easy: these were all just matters of defining methods and then calling them within the methods I've defined for the various rooms. For example, here's the "help" method I made:
puts "Here's a list of handy commands to help you in your quest:"
puts "Open: open doors, chests, your heart, etc."
puts "Look: get a description of the room you're in."
puts "Take: take an item and add it to your inventory."
puts "Inventory: see what items you're carrying."
puts "Quit: quit the game, as a coward would."
end
And then I wire it up in each of my room methods, so in the torch_room method, I use this code to allow a player to call up the Help menu:
prompt; next_move = gets.chomp
end
As I may have mentioned before, I don't have much coding experience with other languages, so I don't know how easy it is to add functionality, but it's really easy with Ruby, and that's part of what I love about it. I'm having a ton of fun with this little project, and while I did a similar project when I was working through Beginning Ruby, those were different times. It was a matter of simply copying code at that point and not understanding what I could do with Ruby and how to make it do what I want it to.
I'm not saying I've mastered Ruby (not by a long shot!) but I'm definitely getting better and it's getting more fun the more I learn. My goal is to keep working on this dungeon for a few more days, expanding the number of rooms, adding monsters and traps, coming up with an inventory system and so on before I move forward with Learn Ruby the Hard Way.
Sunday, October 2, 2011
Using Ruby to Solve Problems
I work full-time doing SEO for an e-commerce site, and a while back we ran into a problem on our site with duplicate content. We needed a simple tool for entering two blocks of text and calculating how similar they were as a raw percentage. We eventually found some tools, but they weren't quite what we needed and weren't that great.
So I got the idea of building something using Ruby to do just this and spoke to my friend Eli about it. I still haven't actually BUILT this little program (and I'm sure it WOULD be little, at least at first) and part of the reason was that I didn't have a great idea of HOW to build it. I assumed I might have one model, say Text, with two textarea fields that could be compared, then spit out a percentage for similarity.
Obviously, I'd be using Ruby to make this calculation and that would be a matter of defining a word, counting instances of words, and then making a comparison between both blocks of text.
I've been doing a lot of work using Ruby to open and edit files, so it's obvious I could do similar operations to open and compare files, then generate a report based on that comparison. Not an elegant solution, since users would want to paste text into fields and compare them, not upload files to compare. But I'm sure it's doable. So that might be my next project.
I kind of tend to bounce around between projects, but I'm learning the entire time, so I don't feel bad about it. That's what these projects are for, right?
I also tend to think of an idea, and immediately think of ways to expand it even before I have the original idea working. For example, I'm already thinking of how this project could be expanded to do other things with text, such as identifying words that are used too frequently, identifying passive voice, identifying and suggesting replacements for adverbs and so on. In this case, I think it's because the original problem seemed to be "How can we identify content that's too similar in terms of SEO?" but I've begun thinking of it as "How can we write better content for the web?"
From my limited experience and understanding of web development, programming and computer science in general, it seems to be a matter of identifying a problem and then finding a solution. Am I far off the mark here?
Tuesday, August 16, 2011
Validation errors in Rails
Basically here's the problem. I have a model Ac that creates Armor information for a Character. I've created some methods to calculate various data from fields from a Character's Ac model, and to avoid any errors when doing these calculations I need to validate presence_of and numericality_of several fields.
So my problem is two-fold. First off, when creating the Ac, all of the validation errors appear before the user has attempted to enter any data. Furthermore, the user can still create an Ac with no data at all. Clearly my validation isn't working.
Here's the code itself from my Ac model:
class Ac < ActiveRecord::Base
belongs_to :character
validates_presence_of :base_ac, :armor, :shield, :dex, :size, :natural, :misc, :deflection validates_numericality_of :base_ac, :armor, :shield, :dex, :size, :natural, :misc, :deflection
...
end
And the code from my AcsController:
class AcsController < ApplicationController
def new
@character = Character.find(params[:character_id])
@ac = @character.create_ac(params[:ac])
end
def edit
@character = Character.find(params[:character_id])
@ac = @character.ac
end
def create
@character = Character.find(params[:character_id])
@ac = @character.ac(params[:ac])
redirect_to character_path(@character), :notice => 'Armor Class was successfully created.'
end
def update
@character = Character.find(params[:character_id])
@ac = @character.ac
if @ac.update_attributes(params[:ac])
redirect_to character_path(@character), :notice => 'Armor Class was successfully updated.'
else
render :action => "edit"
end
end
end
Also, on a slightly tangential note, when rendering a form like this, is that what's meant by a "nested form" ? I've seen this term a few times while searching for a solution and I'm unclear as to what it means.
Here's a link to the project on Github: https://github.com/illbzo1/Manticore
Monday, May 30, 2011
Gettin my git on
I also started using Git Hub, after almost three years of having an account. I understand how commits and pushing changes work, but I had some trouble last night. I was able to create a repository and upload the readme file, but then I couldn't get the entire project uploaded.
So here's what I think happened. I'm working with Chorenivore (my to-do list application) and I created a new directory at users/tylermoore/chorenivore, ran git init, tossed a README file in there and pushed it to git. Then when I went to upload the whole project, I was trying to push from users/tylermoore/code/chorenivore. Obviously having duplicate directories is a stupid idea!
So if I want to make a folder into a Git directory, ready for upload, all I have to do is navigate to that directory in terminal and use git init? Or do I need to make a brand new directory every time, then copy all my code into it? It seems like I should just be able to set up the necessary files with an existing directory. Also, what does git touch do? Can you just enter git touch . to touch everything in the directory?
In more concrete code-related news, Chorenivore is coming along pretty well. After creating the initial files with a scaffold (something my friend Matt says is a bad idea, even at early stages, and while I DO agree with him, I'm still finding it useful) I was able to edit the default code to do a few things I wanted to do. For example:
After creating a new Task, the user is taken back to the Task index page. A user is also taken back to the Task index page after deleting a Task and a flash :notice is displayed.
Creating a new Task does not allow a user to mark a Task as complete, but Editing a Task DOES allow this. Since scaffold generates New and Edit actions that use the same form, I simply copied the code and modified it so the boolean column for Finished? wasn't included.
Not bad! I've definitely got an application that works and functionality that makes sense. I think I've been good about thinking about how things should work, but actually making that happen is getting easier.
Oh, here's my git profile.
Sunday, May 22, 2011
Everybody's coding on the weekend
RAILS TUTORIALS
I like doing tutorials and I always learn something new from each one, but there are some annoying factors. For one thing, since Rails has been around for a while now, a lot of the tutorials I find that I want to create are for older versions of Rails. I'm still interested in doing these, but figuring out what the new syntax is for older examples is tricky. I found an updated version for the recipe application and finished that up last night. Now I'm trying to find a new version for a to-do list.
I tried a couple of video tutorials, but the problem with those is I like to code along with the tutorial, and it's a pain in the ass to constantly pause and restart a video. Still, railscasts is a great resource and it would be worthwhile just to watch, even if I'm not coding along with the video.
One final gripe: a lot of Rails tutorials are geared towards beginners, so every one walks me through what Rails is, a brief history of Rails, how to install Rails and so on. I get it, and it's useful, but it gets tedious. I'd like to see more tutorials geared for slightly more experienced users. Do such things exist? I don't know!
Still, doing just a fucking ton of tutorials is a good idea. I'm learning new things and reinforcing what I already know to the point that I can do some things automatically.
One useful thing I learned today: setting up a Project with TextMate, because I saw a video where a guy was using it and thought "Oh man, why am I still browsing back and forth through a series of folders? That seems MUCH better!" So sometimes it's not a matter of learning new things, it's a matter of learning new ways to use familiar tools.
Sunday, April 17, 2011
+3 to Code Spear
Remember when I would just delete the entire project and start over because I'd screwed the code up so bad and had no idea how to fix it? Yeah, me too. (It was last week) But those days are over!
So here's my game plan: blast all the way through Agile Web Development for Rails, then get back to work on Manticore. Probably a good time to start using Git as well. It shouldn't take me too long to finish up this book, considering I've had it less than a week and I'm already 100 pages in. My progress with this book is definitely faster than my progress through Learning Ruby, probably due to a combination of AWB4R being much more practical and having some experience under my belt now.
On a side note, I spent maybe 6 to 8 hours working on Rails this weekend and I still feel like I'm not doing enough. I have a suspicion most developers feel this way no matter how much code they're writing.
Thursday, April 14, 2011
Agile Web Development with Rails
I picked up a copy of Agile Web Development with Rails this week. I even sprang for a hard copy, despite my philosophy against owning physical media. Seriously, cavemen had books. Look it up. Anyway! I finally got a chance to sit down with it this evening. (Saying "finally" here is funny, since I just got it yesterday, but couldn't do anything with it since the Indy RB meetup was last night)
STILL, "finally" feels appropriate, considering I spent all day at work foaming at the mouth to get home and start coding. So far I really like the book. Like Beginning Ruby, it's written in an easy to follow, engaging manner with practical examples. Is there any other way to write a coding book? Probably not if you want people to read it. So far a lot of what I've seen in the book is refresher material for me, since I've spent the last couple of weeks doing Rails work by following online tutorials and working on Dungeon Roller.
Still, I've already learned some helpful things. Such as having two terminal windows open: one to run the Rails server of my project, and one to perform tasks. I had no idea! It simply did NOT occur to me that I could have two windows open. Another helpful tidbit came with generating a controller. If you write the code as such:
rails generate controller Say hello goodbye
Rails creates a controller named Say, with two empty methods inside of it: hello and goodbye. This definitely saves time later, because up until now I'd been writing methods by hand. Finally (and this is getting into code theory, but we're talking kiddie pool stuff here) I learned the idea that the controller holds the data and the view displays it. Pretty basic, but essentially even though you CAN write code directly into the view, it's a better idea to have the controller handle it. This idea is similar to separating raw HTML from styling.
On a side note, I've decided to rename Dungeon Roller to Manticore, both as a nod to Dwight from the Office and in homage to my favorite mythological creature.
Tuesday, February 15, 2011
Learning about testing, debugging and optimizing Ruby code
Writing custom exceptions is easy enough to understand. Begin and Rescue makes sense. Once again, I'm amazed at how easy Ruby is to grasp. You want Ruby to do something 100 times?
100.times do
puts "DO WHAT NOW"
end
Maybe it's just because I've got some experience now, but this bit of code is easy to break down. I'm looking forward to the day that more complex bits of code are as easy to grasp! One step at a time.
But here's a question about optimizing code. Do you typically try to optimize as you write the code, or do you write the code and then optimize it? At first, it seemed logical to try and optimize as you write the code, because it seemed like the most efficient way to work. However, once I read further, writing code and then optimizing it seemed more efficient, since you could figure out which parts of your code need to be reworked. This way you wouldn't waste time thinking of ways to optimize code that wasn't really a problem to begin with.
Benchmarking code is another concept that made sense immediately. Since Ruby offers you the ability to do something in multiple ways, being able to figure out if one method is inefficient is important.
Next up is Chapter 9: Files and Databases. OH YEAH! Never thought I'd be so excited to learn how to call a database for information and then manipulate that information and display NEW information to the user! This is what I've been interested in since I started reading Beginning Ruby.
Saturday, February 12, 2011
Working with the Standard Library in Ruby
However, when I said basic, I meant just that. It's just a four room dungeon with no easy way to get input from players and interpret that into changes in the dungeon itself. Apparently I won't be learning about collecting data until chapter 9. I'm getting impatient, since I've wanted to use variables defined by a user from the beginning, since it's obviously more practical than a static variable. All in good time, I suppose.
Chapter 7 was all about the require command, libraries and Ruby Gems.
Wrong Gem! Beginning Ruby walked me through installing and including various Gems. Easy enough!
The require command and why you'd want to use it is another concept that came easily to me. I find the more I learn about Ruby, the more I get why people like it so much. Some of the syntax itself gives me trouble, but the basic concepts are simple. Maybe that's true for most programming languages, but it's especially true for Ruby.
I did have a question about Ruby. Is code that is easier to read later usually preferable to code that's more efficient? I know a lot of things can be done on a single line, but are usually spread out over a few lines for readability. If I'm correct and readable code is typically preferred, is that unique to Ruby? Seems like it is.
