Хорошо форматирующий вывод к консоли, указывая количество вкладок

В Objective-C все методы начинаются с символа «-» или «+». Пример:

@interface MyClass : NSObject
// instance method
- (void) instanceMethod;

+ (void) classMethod;
@end

Символы «+» и «-» указывают, является ли метод class method или instance method соответственно.

Разница была бы ясна, если бы мы назвали эти методы. Здесь методы объявлены в MyClass.

instance method требуется экземпляр класса:

MyClass* myClass = [[MyClass alloc] init];
[myClass instanceMethod];

Внутри MyClass другие методы могут вызывать методы экземпляра MyClass, используя self:

-(void) someMethod
{
    [self instanceMethod];
}

Но, class methods должен быть вызван для самого класса:

[MyClass classMethod];

Или:

MyClass* myClass = [[MyClass alloc] init];
[myClass class] classMethod];

Это не будет работать:

// Error
[myClass classMethod];
// Error
[self classMethod];
54
задан John Topley 6 July 2009 в 04:49
поделиться

3 ответа

There is usually a %10s kind of printf scheme that formats nicely.
However, I have not used ruby at all, so you need to check that.


Yes, there is printf with formatting.
The above example should right align in a space of 10 chars.
You can format based on your widest field in the column.

printf ([port, ]format, arg...)

Prints arguments formatted according to the format like sprintf. If the first argument is the instance of the IO or its subclass, print redirected to that object. the default is the value of $stdout.

23
ответ дан 7 November 2019 в 07:56
поделиться

Если вы знаете, что максимальная длина не должна превышать 20 символов:

printf "%-20s %s\n", value_name, value

Если вы хотите сделать его более динамичным, то должно работать что-то вроде этого:

longest_key = data_hash.keys.max_by(&:length)
data_hash.each do |key, value|
  printf "%-#{longest_key.length}s %s\n", key, value
end
56
ответ дан 7 November 2019 в 07:56
поделиться

You typically don't want to use tabs, you want to use spaces and essentially setup your "columns" your self or else you run into these types of problems.

0
ответ дан 7 November 2019 в 07:56
поделиться
Другие вопросы по тегам:

Похожие вопросы: