Syntax matters...

October 10, 2006 by pedro mota

C:\nfor (int i=0; i<3; i++) {\n  do_something()\n}\n\nRuby:\n3.times do\n  do_something\nend\n\nThis 3 in Ruby is an object. It belongs to the Ruby Class Integer and you can apply several methods like +, *, /, -, class, to_s, to_f, is_a?, …\nOne must make sure to understand the difference between classes and objects. Simplifying, an object is a unit of data. A class is what kind of data it is. For example, 3 and 5 are different numbers. They are not the same object. But they are both integers, so they belong to the same class.\n\n3.to_s => \“3\“\n3.to_f => 3.0\n3.is_a?(Integer) => true\n3 == 3.0 => true\n3.eql?3.0 => false\n3.eql?\"3\".to_i => true\n\nHave a pdf cheatsheet.

© 2018 | Follow on Twitter | pedro mg & Hugo