class << self idiom in Ruby

First, the class << foo syntax opens up foo‘s singleton class (eigenclass). This allows you to specialise the behaviour of methods called on that specific object. Now, to answer the question: class << self opens up self‘s singleton class, so that methods can be redefined for the current self object (which inside a class or module body is the class or module itself). Usually, … Read more

Specifying width and height as percentages without skewing photo proportions in HTML

Note: it is invalid to provide percentages directly as <img> width or height attribute unless you’re using HTML 4.01 (see current spec, obsolete spec and this answer for more details). That being said, browsers will often tolerate such behaviour to support backwards-compatibility. Those percentage widths in your 2nd example are actually applying to the container your <img> is in, and not the image’s actual size. Say you … Read more

How to find the size of an int[]?

Try this: Because this question is tagged C++, it is always recommended to use std::vector in C++ rather than using conventional C-style arrays. An array-type is implicitly converted into a pointer-type when you pass it to a function. Have a look at this. In order to correctly print the sizeof an array inside any function, pass the array by reference to that … Read more

How to print instances of a class using print()?

The __str__ method is what gets called happens when you print it, and the __repr__ method is what happens when you use the repr() function (or when you look at it with the interactive prompt). If no __str__ method is given, Python will print the result of __repr__ instead. If you define __str__ but not __repr__, Python will use what you see above as the __repr__, but still use __str__ for … Read more