Posts

Showing posts from 2008

print what you like

The following web site allows you to select part of a page, or edit a page to help you print in a way you like: http://www.printwhatyoulike.com

Comparison of equality operators in Ruby: == vs. equal? vs. eql? vs. ===

Here is a short explanation of when these predicates are true (From http://www.wellho.net/mouth/985_Equality-in-Ruby-eql-and-equal-.html) The == comparison checks whether two values are equal eql? checks if two values are equal and of the same type equal? checks if two things are one and the same object. How do I remember which is which ... The longer the operator, the more restrictive the test it performs Let's understand the differences with some examples. Each "abc" below is a different object. Therefore, equal? is false. irb(main):065:0> "abc".object_id => 24173330 irb(main):066:0> "abc".object_id => 24165760 irb(main):067:0> "abc" == "abc" => true irb(main):068:0> "abc".eql? "abc" => true irb(main):069:0> "abc".equal? "abc" => false As you might expect, there is a single instance of 1. Therefore, all comparisons are true. irb(main):094:0> 1.class =>

Adam Wiggins' RestClient at SBonRails Ligtning Talk

Full Screen : http://docs.google.com/Present?docid=d5vwc72_570dggpbshh&skipauth=true

Making sense out of ruby's dynamic method calls

Case 1: def n puts "inside n" end def m(&m2) m2.call end m(&method(:n)) m() {puts "hello"} >> inside n >> hello Case 2: def m() yield end m(&method(:n)) m() {puts "hello"} >> inside n >> hello Case 3: def m(&m2) m2.call yield p m2.class end m(&method(:n)) m() {puts "hello"} >> inside n >> inside n >> Proc >> hello >> hello >> Proc Case 4: (Adding checking before calling) def m(&m2) m2.call unless m2.nil? end def m yield if block_given? end Case 5: p = Proc.new(&method(:n)) p.call m(&p) >> inside n #p.call >> inside n #m(&p) Case 6: p = Proc.new() {puts 'hello'} p.call m(&p) >> hello #p.call >> hello #m(&p) Case 7: p = method(:n).to_proc m(&p) >> inside n Conclusions and Summary - The decision of using yield or call in a method does not affect how the method is invoked. It can be given a block or a &a

Rake Tasks

To see the available rake tasks, enter "rake -T" (a list view), or "rake -D" (descriptions). If you are interested in a particular task such db, you can enter "rake -T db" or "rake -D db". Thanks http://www.databasically.com/2008/08/03/rake-list-tasks.

Creating an HTTP Streaming Client in Silverlight

I read and played with WebClient and HttpWebRequest classes. Both of these classes allow you to make requests to an http server. Once you make the request, you give a callback that is invoked once the response is received so you can happily examine the response. Let's say your HTTP server is streaming a file, or it wants to hold on to your request and sends messages as necessary from the same connection. To be able to work with such a server, your http client must be able to read the data as soon as it is available without requiring the response to be received completely.  After searching silverlight forums I found a solution. The solution assumes that you have a control on the server code or the response you are getting is bigger than 4KB (IE buffers upto 4KB). Here is the code snipped for your client side: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "POST"; request.AllowReadStreamBuffering = false; try {  request.BeginGetResponse(On

HTTP Streaming Ruby Client

Let's say you have a HTTP streaming server such as the one below where the server sends the HTTP response slowly or may it even holds on the request and does not close the connection and keep sending data (so called "HTTP Streaming"). How would you write a client that receives the response segments as soon as they arrive? (we do not want to wait till the whole response ends or we do not have a choice since the server is streaming "infinite" data). require 'rubygems' require 'mongrel' class StreamHandler <>   def process(request, response)     response.status = 200     response.send_status(nil)     response.header['Content-Type'] = "text/plain"      response.send_header     10.times do       response.write("Message \r\n")       sleep 5     end     response.done   end end Mongrel::Configurator.new do   listener :port => 3000 do     uri "/", :handler => StreamHandler.new   end   run; join end The cli

The Order of Rails Validation Filters

Here are the available Rails validation filters: before_validation_on_update before_validation_on_create validate validate_on_update validate_on_create after_validation after_validation_on_update after_validation_on_create before_save before_update before_create after_update after_create after_save These filters are callbacks that are invoked automatically. The difference between update and create is really with respect to the database. If the record you are saving is a new record in the db, then it means you are creating. If the item already exists but you are making a change, then it means you are updating. In other words, it does not matter, whether you called obj.save or obj.create, before_update filter will be invoked. The same is true for obj.save vs. obj.update_attributes. So no matter what Active record method you invoke the filter is decided based on the database record. How about before_save, before_after_save? They will always be invoked whether you are creating or updating.