Entries RSS Comments RSS

Posts Tagged ‘Ruby’

Easy Sinatra Project

Saturday, January 30th, 2010

I lovelovelove Sinatra. (SinatraRB.com) I created a very simple app (the fortune cookie generator I mentioned in an earlier post) to play with styling/views, get and post requests, and the very bare bones workings of Sinatra. I absolutely love how pure and simple it is. The structuring is a bit confusing at first, but the Sinatra Peepcode video helped me a lot on this: http://peepcode.com/products/sinatra

In the Peepcode video, they make a Sinatra app in one file, and then break it down from there into separate files. It’s good to see how everything CAN fit in one file. This is how I learned CSS, actually– by putting my styles in with the rest of my website. Once I realized you put the CSS in a separate file, I had a mini epiphany: sometimes it’s best to put things together before you take them apart. Then you can truly appreciate the fragmentation of a given application and truly understand the purpose of breaking things up for simplicity’s sake. Seeing this helped me understand a lot about Sinatra, and even shed some light for me on why Rails is the way it is, as well.

So, dundundun: I made this: http://fortunefinder.heroku.com

Don’t judge my poor styling and wacky coloration. ;) This really was just a silly little experiment to play with the basic features of Sinatra. Now that I understand the basics, I’m moving on to another project confidently. I’ll still probably tweak this one a little, too. Just for fun.

You can check out the code on github at http://github.com/ashumz/fortunefinder Nothing special, but might help a Sinatra newbie get a feel for the basic set up of a simple app.


Defining Useful Methods in Ruby: Adding a rand method to the Array Class

Tuesday, January 26th, 2010

The ‘rand’ method in Ruby is a method of the Kernel class that generate a random float between 0 and 1 (and thus will always round down to 0 when converting to an int.) Being a new Ruby developer, I decided to make an array and try to pull a random element like so:

@name=%w[bob, judy, don, john]
@get_random_name = @name[rand]

But this will always return ‘bob’, the 0th element of the array.

Instead, to pull a random element from an array (of any length), you need:

@get_random_name= @name[rand(@name.length)]

But that’s icky and unRubyish, and Ruby really SHOULD have a rand method for the Array class, shouldn’t it? So let’s make one, because we can! :)

class Array
def rand
self[super(self.length)]
end
end

@name.rand will now return a random element from the array. Awesome.

Sinatra: The answer to my Ruby GUI problem

Monday, November 9th, 2009

Ruby and GUI’s have been on the forefront of my brain lately because I’ve been playing around with fun ways to program with Ruby. I am honestly bored of command line programs, and for good reason: the command line isn’t pretty enough for me. Since I’ve had problems installing Shoes and Hackety Hack on my version of Ubuntu, I asked one of my friends who uses Ruby frequently if there were other GUI options. There are options, but he referred to them as a bit of a mess to implement and pointed out that javascript and the web make for better uses of, what I would call, “fun Ruby.” Better yet, when you program for the web, everyone can use your program without worrying about what platform they are on.

So, I gave this a little thought. I started getting jealous and even a little frustrated at Python programmers and their connectedness with GUI’s and their pride in Python’s support for GUI’s. Apparently, Ruby has similar support, but it’s not nearly as widely embraced amongst Rubyists as it is in the Python community. (True? False? opinions?) Admittedly, I think I should have just learned visual basic and got my GUI fix 10 years ago. (I don’t particularly want to learn Python, and there are issues with it’s syntax that have turned me off pretty heavily.)

So, I have decided that the best course of action, given my new fixation on Sinatra, is to begin a jquery/javascript and Ruby project. Although I’ve been making websites for about 10 years, they have generally been informative, usually static, very low level applications of HTML, CSS, and bits and pieces of PHP. I have used javascript for fun little menus now and again, but honestly I’m a total copy/paster when it comes to js. I started recently on some javascript tutorials, but was then quickly reprimanded: SKIP TO JQUERY, says everyone. Unless you really wanna become a pro in javascript, it sounds like jquery is a nice shortcut to the overwhelming syntax (especially coming from Ruby!) and implementations of javascript. I can’t quite figure out a consistency of why and how something does what in javascript. But, if I can figure out what I want the code to do, I’m pretty sure I can figure out how to use jquery to get the job done. We’ll see.

I am going to begin working on a blog that is purely in javascript, using jquery, and also uses Sinatra. I’m going to use sqlite for the DB. Did I mention I have very little experience with DB’s, too? :) Okay, this project, honestly, seems a little over my head to me right now. But once complete, I think I will have experience with all of these pieces I need to make a pretty, fun, and useful application. Something every programmer craves– right?! :)

Updates to follow. Thanks to Julio for helping me come up with the idea on how to practice messing with all of the components I’m craving: Ruby, Sinatra, Javascript/Jquery, Sqlite, ERB, CSS… wee! This brings up another point about how multilingual and implementation exposure at the early stages of learning a programming language can be so incredibly integral to understanding how to best utilize a programming language like Ruby… but that’s for another post! Cheers!