Learn Ruby in 48 Hours

1334417501000 » Tagged as: Ruby

The plan was to finish reading Humble Little Ruby Book within one day, but that didn't quite work out, so the Learn Ruby in a day project, became the Learn Ruby in 24 hours project. Now it's officially been rename to Learn Ruby in 48 hours.  What's my excuse? Did you know that yesterday Sri Lankan new year? That means there is a hell of a lot of visiting to do and there were other new year traditions to take part in (like boiling a pot of milk over a fire). Besides the book had areas with coverage that wasn't very deep so I had to look around for additional material from the web. Anyhow, this is what I have covered since the last post.

File System

The File class pretty much has all the feature that you expect to find in such a class and the method names are self explanatory. but note that File.size? returns nil when the size is zero

myfile.each_line is kind of like the .each in an array

equally interesting is IO.foreach(“filename”)  {|line| some code }

You can write to a file with the write method or with the puts the latter will append \n at the end of the line.

The C++ IOStreams << and >> is also available (and thanks for reminding me about this, large parts of my memory which used to hold C++ stuff has been overwritten with other data)

  File.open(“filename.txt”) do |myfile|

     myfile << ‘text1’  << ‘text2’

  end

Threads.

Finally here is area in which Ruby seems to have something in common with java. You create a new thread with Thread.new followed by the code block, but unlike in java you don’t need to call Thread.start(). You can wait for the thread to die with myThread.join() and yes it accepts an integer as a parameter. How are things different? you give up the CPU with myThread.pass you can stop it with Thread.stop (which is a class method) and you can restart it later on with myThread.start

There are quite a few other thread related methods and properties, thankfully the names are self explanatory ost of the time: Thread.current, Thread.list, myThread.alive? , myThread.status , myThread.stop? There, didn't I tell you? Thread.value can be used to grab the return value from the just executed Thread.

Processes

It's good to see that the system method and the backticks operator of PHP can also be found in Ruby. And popen is represented at IO.open , you can use IO.open(‘some executable’) does the trick (you can of course use to open regular files as well. And exec is also there.

The Environment stupid

PHP’s $_ENV is present in RUBY as ENV enough said

Networking

Ruby has a set of classes for dealing with networking and there is no need to go into too much detail. These are things that you don't need to remember, that's what API docs are for.. After all if you are righting socket code with PHP or Java or C or whatever you look at the API and pick and choose which classes and methods to use don’t you? You don’t? you have it all in memory? hmmm

Anyway, what’s important is the TCPSocket class - which allows you to open a client socket and the TCPServer class which obviously deals in server sockets. Once a socket is open you can read and write to it just like you do with files. Nothing to write home about really. Of course if you don’t want your app not to freeze up you need to add Threads to the mix. Here though things are different from the Thread.new which we have already seen.

require “socket”

myserver = TCPServer.new portNumber

while true

 Thread.start myserver.accept do |sock|

    while sock.gets

       sock.write ($_)

    end

    sock.close

 end

end

Did you notice how the perl default variable $_ has been borrowed by Ruby and sneaked into the above snippet of code? (which i have borrowed from the humble little ruby book )

HTTP

Now here is something to get rather excited about. Ruby has a built in webserver called Webrick. Even the ever versatile PHP which tries to be everything to everyone didn’t have a built in webserver until the recent release of version 5.4. To start it up you only need to do

require ‘webrick’

include Webrick

myserver = HTTPServer.new (:Port => 8080 , :DocumentRoot => Dir::pwd + “/temp”)

myserver.start

Hang on a second from where on earth did this include and :: come from? and what’s this strange way of passing parameters. Mr Neighborly, I don’t think you have explained these before. While trying to come up with inform on this include method, I ran into a method named output. If I keep going off on tangents like this, I am not going to finish the book even in 72 hoursso let’s get back to the task at Hand, HTTP.

Turns out that you can fetch a web page real easily using the Net::HTTP.get_print URI.parse(‘http://raditha.com/’) I assure you that this is not perl but Ruby code. get_print as the name suggest is to get and print. If you just want to get, use get. You can post with Net::HTTP.form_post where the second parameter is a hashtable of data to be posted, what’s returned is the server response.

Similarly Ruby has support for host of other protocols including IMAP, SMTP and FTP

Next up the book covered a set  of classes that support distributed computing, I suppose this could be something like RMI, didn’t bother to find out. Are such things really usefull in this day and age?

Databases.

The next sticky spot was with databases, not because it's incomprehensible but because of gem install issues. So let's stop for a cup of tea. Tea is under rated by geeks, it is coffee without the side effects. For example the Caffeine in tea does not provoke migraines while coffee does.

comments powered by Disqus