It's bit confusing when it comes to test equality of objects in Ruby. It has five different ways to do it and each one has a different purpose and context in which it should be used.
equal? returns true if receiver and the parameter object have same object_id. Object ids are unique and its not shared among the objects. This method should not be overridden
== returns true if the receiver and parameter object has same values. This is most intuitive and should be overridden in your sub-classes
eq? Like == it compares objects but is more strict about type. Only reason this exists is to compare Hash keys
=== This is used for Switch Case statement. It compares target in case statement with each selectors. You can override it to control the way case statements are matched inside the switch block
=~ Pattern matching (Side note: In Erlang = does the pattern matching)
== and =~ does have a negated version and since ruby is a great language if you implement == you != for free, isn't that great.
Next time when you have to equate objects you know which one to use
Stuff about software development, agile and testing
Sunday, August 26, 2007
Tuesday, August 21, 2007
Ruby open classes
This issue have been flamed in many blogs and Java programmers are not very comfortable with open classes that includes me. But its really powerful there is not doubt about it, like Neal ford said how many times did we created StringUtils class in our java project. But I guess re-opened classes should be organized in some way where everyone knows where to find it. When I say "1.hour.from_now" example in the rails book for the first time. I was really shocked, does it makes sense to have hour method in Fixnum and to add more confusion if you open irb and look at fixnum methods you will not see it at all.
One of the thing that Java programmers should get over is you cannot know a class in Ruby completely. In Java we could say that I know the String class, I know how many methods are there and what are those methods etc but in the world of open classes you never know. There is no replacement of communications, in ruby world its more important talk about your work with other teammates and unit testing of course.
One of the thing that Java programmers should get over is you cannot know a class in Ruby completely. In Java we could say that I know the String class, I know how many methods are there and what are those methods etc but in the world of open classes you never know. There is no replacement of communications, in ruby world its more important talk about your work with other teammates and unit testing of course.
Tuesday, August 14, 2007
netbeans ruby support
If you are used to eclipse or intellij, you know how hard is to live without auto-completion, debugging and refactoring. Textmate is cool and simple but netbeans ruby support is probably best right now. It actually uses string pattern matching to list out all the options and its pretty fast. To download ruby only version go to link
Subscribe to:
Posts (Atom)
Labels
- agile (4)
- agile testing (1)
- build tool (1)
- design (1)
- DSL (1)
- duck typing (1)
- eclipse (1)
- ejb3 (1)
- Fluent Interface (1)
- grails groovy (1)
- groovy (1)
- gwt (1)
- hacking (1)
- haskell (1)
- java (1)
- javascript (1)
- jvm (1)
- languages (1)
- mac (1)
- pipes (1)
- programming (1)
- qa (1)
- rant (2)
- ruby (6)
- sas (1)
- scala (3)
- scripting (2)
- software rewrite (1)
- statically typed (1)
- testing (4)
- two phase commit (1)