11/17/08

Riding the rails - Ruby on Rails Tutorial for begginers (part I - Ruby objects)

This one goes for some friends I had convinced with my Rails enthusiasm and want to give it a try and I owe them some work on it.

The first thing one has to know about Rails is that Ruby is not Rails. Period. Ruby is an interpreted, dynamic language; Rails it's killer application. But that doesn't mean learning Ruby without learning Rails is worthless, nor isn't true the myth that Rails is difficult because of it. It's actually the opposite: Ruby is a very nice language that can do almost everything as well as other languages, and as everything, has its own advantages. Rails was built on Ruby because of the language dynamism itself, it was just a perfect situation. I'll try to show why along these posts.

There exist many resources of the Ruby language, starting for it's own documentation. I'll try to give a brief but complete start guide, assuming you have some programming experience (not a lot, though) I'll also assume you have Ruby installed (which should be pretty easy unless you use exotic computers) and you've got irb up and running. If you plan to learn Ruby, don't be afraid to experiment with irb, it's actually a must.

Ruby in a nutshell (but briefer)

Everything is an object

Welcome to the world of Objects. In programming, the Object Oriented Paradigm (OO) states that a class is the abstraction of which a thing is represented. Think of it as a mold, from which actual things are going to be created. The classic example: the Dog class. Let's try it in irb:
class Dog
end

Voilá! We had defined a new Dog class. Unlike Java and friends, it doesn't need to be defined in its own file, or be included in a namespace (although it's recommended by good practices, I'll taking on that later). But now, what does that do?

Well, now we can create dog instances, which are separate entities that are represented by the Dog class. Let's see
lassie = Dog.new
bingo = Dog.new

We have created now two different dogs, which come from the same class, and assigned them to a variable. A variable is simply a label for an object, it's a way to keep track of objects.

Buh, these dogs are pretty boring, they do nothing! I would not want such a uninteresting dog, so let's teach them some tricks.
class Dog
  def bark
    puts "Waff, waff!"
  end
end

Ok, I'll admit it, barking is not such a great trick, but it's enough for demonstration purposes. In this piece of code, we reopened the Dog class; this means that we can alter classes later on the flow of the program. It was done just by stating the Dog class again. It may seem like the entrance to chaos, but I assure you that is actually a nice feature that some other languages don't have and can be used well enough.

Inside the class we defined a method. Methods are procedures or actions that execute some code when they are called. Let's call it:
lassie.bark

Woah, lassie is barking! That is how you call instance methods in Ruby: with the dot operator (.) When you append a point to an instance of a class, you are trying to call a method or access one of its properties (more on that soon). By calling lassie.bark you are invoking the bark method defined in the Dog class, from which lassie was created.

If you come from other languages, you may notice that we called a method without parenthesis (that is, lassie.bark(). Well, Ruby is full of syntactic sugar, and parenthesis are not always necessary. Eliminating the unneeded ones reduces a lot the code noise, which is important. But, if you really miss them, typing lassie.bark() will work as well. Actually, we did another method call without parenthesis in case you didn't notice. Where? In the puts "Waff, waff!". puts is just another method, and we are passing the parameter "Waff, waff!", which is a string. Strings are just text, and the form to indicate them is to surround them with quotes. More info on Ruby strings here.Parameters are arguments (or "things") that are passed to a method to do something with them, as our dogs will soon be able to demonstrate.

puts method just prints whatever you send to it to the screen, and it doesn't seem to belong to an instance of a class, but in fact it does. It belongs to the toplevel, which is a magic construction I'm not to cover right now. But learn from this: even puts is part of an object. And it can be called with parenthesis also, just try puts("Waff, waff!") and it will do exactly the same thing.

These dogs are very dumb. I remember their names, but they don't. Let's give them an attribute 'name'. Attributes are just variables that belong to instances of a class and are defined on it. We'll reopen again the Dog class, and add the attribute and a new trick.
class Dog
  attr_accessor :name
  def salute
    puts "Hi, I'm " + name + "!"
  end
end

The attr_accessor instruction is the way for a class to indicate an attribute that is readable and writable by external objects. The parameter to the attribute accessor is :name, which is the Ruby syntax for symbols, which are just objects used to represent other objects, in this case the attribute name. Now our dogs can have a name:
lassie.name = "Lassie"
lassie.salute

Woah! Our dog talks!. By declaring an attribute name, Lassie can remember her name, and when we invoke the method salute, she will say it. But if we try to get Bingo to say his name:
bingo.salute

We get an error: TypeError: can't convert nil into String. That is because we hadn't assigned a name to Bingo, so his name is nil. Nil is the representation for nothing. And Ruby doesn't know how to concatenate a string with nothing, hence the error. Exercises for you: give a name to bingo and make it say it to you. Change name to Lassie (after all, it's very old fashioned ;D). Create a new dog, and name it however you want. It should be easy pie.

To finish this part, I'll just point you to one of the most wonderful things in Ruby: introspection. Our dogs not only needed to know what their name was, but what can they do. In general, objects can retrieve information about its own methods, properties and classes by their own. For example, just type:
bingo.methods

and it will present to you a bunch of "tricks" that Bingo can do. That is, all the methods that bingo has. You see a lot of standard methods that all objects have; if you only want to see the methods belonging to the Dog class, there is another way:
Dog.instance_methods(false)

And it will give you the methods defined just in the Dog class. The parameter false is just the indication to the method to exclude all methods defined in superclasses or modules: don't break your head by now, I'll explain it later. If everything went right, you should see your bark, salute methods, as well as name and name= methods, which are the ones added by the attribute accessor. In fact, even attributes are instance methods in disguise. But don't complicate things, and just think of them as "things held by other things".

Now you can have fun in irb creating classes and methods. In the next post I'll be covering: blocks, iterators, exceptions and standard classes. See you later!

11/12/08

This blog is going to fail II - Some questions about blogging

Better than that, this blog is dead. And I'll try to resurrect it.

Why do I think that? Well, during the last days (or months) I have been realizing of the potential of all these new ways of communication. In one of my last posts I have given a little perspective on social networking services, but, is blogging really a form to communicate? Ultimately, what is blogging?

Well, I attended TechEvent last week, and they had a blogging panel with five personalities of the Mexican blogsphere. The interesting thing is that each one of them has a very different background, hence very different kind of bloggers.

Blogging platforms (the CMS's) are at everybody's hands. But blogging is not for everybody. There will be always millions of blogs, but at the end only few ones will be relevant to the most of the people. But, and it's a big but, the phenomena of the long tail will become more and more evident, and the blogsphere will segment. Which is a good thing for people, but bad for businesses.

One of the interesting questions was if blogging was about quality or quantity. Uh, that was a hard one. Let this blog speak for itself: quantity doesn't help. I try to keep quality posts, but THAT IS NOT ENOUGH. And certainly, the answer is not straightforward, it depends on the blogger, and the content. For commercial blogs, for example, quantity is essential, while for technical blogs quality is more important overall.

Other thing that caught my attention was the question about if blogging is a one way channel or is more a conversation. The answer again lies at the circumstances. Very few people have the right of being "information radiators" only, based on their communication skills. But those people aside, blogging is mostly a way to interchange opinions to the world back and forth.


by Kristina B

Yep, I must love wordle

Lastly, my personal question: how is microblogging changing the way of blogging? I mean, I have a Twitter account and for me is lot more easy to Twitter about something relevant than blog it. Twitter and other services that were not meant to replace blogging are doing it, at least very gradually and imperceptibly. Twitter may the epitome of quantity, but it can still keep quality.

Other blogs talking about TE08: geek.com.mx

9/27/08

A giant in the house - My own opinion about Microsoft

Some days ago we received Microsoft at school: they were recruiting students for internships and full time jobs. I decided to apply, and get an interview. I was a little skeptic about what the jobs were about and the process itself.

I've said bad things about Microsoft in the past. I must confess, when one is inexpert and biased by other people, giving partial opinions is easy (That's the good or bad thing about strong opinions weakly held). But now, after a few more years of experience using computers, and inevitably, its products, I feel obligated to give an opinion.

I use [other OS] for almost all my needs. Every time I boot into Windows, my friends and teammates won't let me lie, I spend the next thirty minutes ranting about how much I hate Windows. I stop ranting when Windows stops booting (including services and who the hell knows what else), and becomes almost fully usable. Thirty freaking minutes, you can't tell me that is not a waste of time. My [OS] boots in 5 minutes, 10 at most when I hadn't done any maintenance.

Is that really a problem of the software? Maybe. Do I have to hate Microsoft for that? Definitely not. Windows consumes resources like a monster, but definitely the computer I use isn't suited for that matter (BTW, I am talking of Win XP, my computer would commit suicide if I installed Vista). I am a software developer, and have .NET, VS, SQL Server and a hell lot more of services that are essential to my day-by-day work. My computer is a somewhat old Thinkpad T41 that I have pulled to its limits; Windows may be a good platform but surely requires a more robust equipment. I am not discussing here if that's actually good or bad for the common user, I'm just saying the cause of my frustration is that I have a small bad suited computer for all the work I have to do as developer.

Having said that, my main point: I still dislike Microsoft. To be more specific, I dislike monopolistic and uncompetitive behaviors. Yes, I am a big fan (in the good sense) of Open Source. I really believe in it as another way of doing software. I understand it's motivations and benefits, and respect them. What I really dislike is that, based on strength of market and current capabilities, one company can get all the cake and keep it out of reach of the rest of the world. That is what Microsoft does in many ways, some more evident than others.

I would give some examples, but for the brevity and partiality of this post I will just limit to say that there are many you can find discussions about around the web. Fortunately, not everything in Microsoft is that way. The bad thing about it is that the same inertia driving the software industry around the giant Microsoft has made that other platforms and products don't get as much attention -again, I understand, but not condone, the economical rationale-. Hard work has been done to push things in the other direction, and slowly but hardly getting results.



Would I want to work for Microsoft? Definitely. Would I do something to tie up the competence's freedom? No way, and I want to state this clearly from the beginning. Now, speaking from the other side of the coin, I really believe in Open Source, but I don't see it necessarily as antagonist. Is just that both approaches excel in one part or another -beware, because both have the potential to excel on the other's strengths-.

9/25/08

Don't let me singing alone - Karaoke Capitalism (review)

 
Accept no imitations - no limitations. You are here for a reason. You've got the power and the rights. It's your life - you decide. [...] Free your mind - and the rest will follow
Karaoke is about imitation. The whole metaphor about the karaoke club with the modern economy - or more accurately, what economy is and has stop being - takes us to the deepest roots of contemporary successful companies.

The book is about economy, politics, technology, sociology and business management; yet, it never touches a single bit on their underlying theories. What Ridderstråle and Nordström, the authors, describe us is the real world.

Karaoke Capitalism is not a guide, but an invitation. To explore, to free ourselves, to be aware of what is happening, to be confident in ourselves and in our ideas, to take risks, to confront failure and, ironically, to not embrace success. Within its pages, you can perfectly see the mechanisms of the world of today, and at the same time, get the insight of why is it happening.

It would not be hard to compress all the ideas of the book into a blog post; but is impossible to transmit that insight in a few lines. I will limit to say that the book emphasizes on individuality, in which talent is the new real value (besides money). Technology is now just enabling and empowering the individuals to grow. But we now live in a material world (whether we like it or not), and the market is a big, unstoppable mechanism, which is more than the sum of all of us. The world has, is and will keep changing, so the business models have to flow with it. Now, businesses are about innovation, which is far more complicated than just invention, and is a long path with ups and downs. Companies are not controlled by corporations anymore - they are driven by its people, the talented one. At the end, the successful companies are the ones that can engage the customer, provide the most valuable. The survival of the fittest - and the sexiest.

(C) The sinister idea, Felipe Morin


So, what does it has to do a book about markets, economy, globalization and management to us (at least to me), people on the IT? Well, I'm sorry, the answer is not highlighted and in bold font within the book. What it gives us, as I say, is a tour around the minds and souls of some of the most innovating companies - which gives the casualty that many of them are technological companies. It helps us see all those trees in the forest in front of our noses: the world is global. You cannot change it, but can change yourself. If you are lucky, you can excel. And, the most important, valuable lesson. Luck doesn't equal chance. Luck is for the people that expose to the opportunities, who know which to grab - and which to throw away-, who have aspirations, are pro-active and practice luck.

In short, this book is a whole manifesto, one that you shall embed into yourself, and put it into practice. Be sure not to miss it.

9/3/08

Riding the camel - OCaml opinion (i)

I have started just a few weeks ago using OCaml. My only serious experience with a functional programming language was with Erlang, but I really liked it. I liked the shift of paradigm (although I am such an OO progammer (learned to program with Ruby), it was not difficult at all). Here are some of my thoughts about it.

Being a strong typed language for being functional at first sight seems cool. You can have all the benefits of typedness without actually the blot of the extra typing. Actually, it is, but it has its cons.

I really have nothing to complain (yet) about OCaml except for it's output libraries. I really miss my [puts | print | Console.Print | even the fugly System.out.println]. Debugging is a pain not as easy as another languages, just because of the printing. Not debugging itself, but unit testing, as part of test driven development (TDD), something relatively new to my and which I'm falling in love with.

I don't know whose fault it is, if the language itself for not being flexible with the types (at least the very basic ones) or the libraries. I know about some external libraries (MetaOCAML, Sexp, extlib) that try to handle this, but having to rely on external libraries leaves much to desire of the language.

The other thing I find annoying is that the compiler just points the first error it encounters. It's trivial for small files, but I'm scared of what can happen with larger files. Fortunately, TDD helps you not to commit those mistakes, at least not catastrophically.

Maybe the OO part of OCaml has it's advantages, and I want to think it has because pure functional programming in OCaml is not the way one would like, at least not me. But I have not explored that yet. My next goal: functors.

CC by shamiska

Well, all of this is because we are developing a Web framework on school, which had to be done in a functional language. It seemed like the perfect opportunity to learn another language, so I decided to give it a shot. I don't post the link of the project (although it's GPL3), because I don't know if I am able to do it given it is a school project, but once I can I would share the result with you.

8/28/08

Pimp my docs! - What I think about Open Documents

Although I started recently to do it, some people have complained, other have misunderstood. I must admit it: if someone did the same to me, I would have done the same.

I send my documents in Open Document formats.

Actually, I send them by duplicate. That's because Office (which is the offimatic suite most people use) does not support ODF, and I am actually interested on the other people to see it.

Why support ODF? It sucks. Lots.

I don't know much about its specifications. But it's short, and some claim way underdocumented. It cannot be implemented well in Microsoft's applications, and it has had very little reception. That's talking about the format itself, but for the software, OpenOffice, which is the main competitor for Microsoft Office and is the main force in the adoption of ODF also sucks. Their suit doesn't have as much as functionality, plugins, organizational support, widespread adoption, eye-candy, etc. Your call.

So, what drives me to support ODF? Isn't it obvious? It's open. And open means, among other things: 1) we are not tied to a particular vendor any more, and 2) we, as a community, can improve it. And open source has proved many times that once sucky products later become leaders in their market. And making it depends on us as a community (I am not involved in any manner in the development of ODF, but I am user as well, and by exposing these kind of things I hope I help it)



I am not against OOXML or other standards. But I am against monopolies. I am in pro of ODF, call it whim, call it open-mindedness. OOXML is far away of being an standard. Being used by most of the population is not a valid reason for being so. And world needs standards.

8/23/08

Oh, crap 2.0 - The challenge of making Web 2.0 services meaningful

I must admit it, I was disbelieving in Web 2.0. When these applications first came out, I kind of neglected them. I remember it well: my MSN space (however it was called) was a spam nest, as I was completely uninterested of what flavour was my classmate (incorrectly tagged by the application as "friend") party cake. I didn't give a damn. Google Maps was just a fancy toy. Amazon was just another book seller, and eBay sounded like a scamming center. The term WEB 2.0 then popped out. And it sounded like crap.

Then I slowly succumbed by them. I watched a YouTube video. I came to a link to someone's de.licio.us. Someone told me about Twitter. And I liked them.

The reason I am posting this is because I always disliked Facebook, until I started using it. All started, ironically, with a costest for Facebook application, in which I decided to give a shot. I didn't know anything about Facebook, except those annoying requests for giving hugs to people I did not even know, and those emails pushing me to block or concede.

Filtering the good contents of each site is quite a challenge. I know, the model of the long tail tells us that no matter what you do, you will always have crappy contents. But the garbage of one person is the treasure of another. The most those sites can do is to provide us a personal way to filter it. Some of them do it really well, while others have to improve it.

I still dismiss many many sites. I had to block I-don't-know-how-many join requests from social network sites that I am not really interested in. Until I know they are useful, I have more than enough with my Facebook (and probably a couple of sites more that I don't remember and I won't ever visit again). But all these "fancy tools" have an added-value hidden somewhere all those things you may not like. Try them before discarding them. You will be surprised on how useful you can make them be.