小编Vei*_*hou的帖子

Ruby IO - 间接文件输入/输出

最近我一直在学习Ruby.我在编写File的子类时遇到了问题.

class MyFile < File

end

file_path = "text_file"

file = MyFile.open(file_path) do | file |
    file.each_line do | line |
        puts line
    end
    file.close
end
Run Code Online (Sandbox Code Playgroud)

结果:

line 1
line 2
line 3
Run Code Online (Sandbox Code Playgroud)

如果我想通过调用方法来输出:

class MyFile < File
    def foo
        self.each_line do | line |
            puts line
        end
    end
end

file_path = "text_file"

my_file = MyFile.open(file_path) do | file |
    file.foo
    file.close
end
Run Code Online (Sandbox Code Playgroud)

结果:

/Users/veightz/Developer/RubyCode/io_error.rb:4:in `write': not opened for writing (IOError)
    from /Users/veightz/Developer/RubyCode/io_error.rb:4:in `puts'
    from /Users/veightz/Developer/RubyCode/io_error.rb:4:in `block in foo'
    from …
Run Code Online (Sandbox Code Playgroud)

ruby file

5
推荐指数
1
解决办法
128
查看次数

为什么resolveInstanceMethod:有时调用两次

最近,我正在研究Objective-C中的运行时.

我创建了一个名为的类TO:

@interface TO : NSObject
@end

#import "TO.h"

@implementation TO

- (id)forwardingTargetForSelector:(SEL)aSelector {
    NSLog(@"%@ sel: %@", NSStringFromSelector(_cmd), NSStringFromSelector(aSelector));
    return nil;
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    NSLog(@"%@ sel: %@", NSStringFromSelector(_cmd), NSStringFromSelector(aSelector));
    return NO;
}

+ (BOOL)resolveClassMethod:(SEL)sel {
    NSLog(@"%@ sel: %@", NSStringFromSelector(_cmd), NSStringFromSelector(sel));
    return NO;
}

+ (BOOL)resolveInstanceMethod:(SEL)sel {
    NSLog(@"%@ sel: %@", NSStringFromSelector(_cmd), NSStringFromSelector(sel));
    return NO;
}

+ (IMP)instanceMethodForSelector:(SEL)aSelector {
    NSLog(@"%@ sel: %@", NSStringFromSelector(_cmd), NSStringFromSelector(aSelector));
    return nil;
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

@end
Run Code Online (Sandbox Code Playgroud)

然后,我在某个地方调用一个未经识别的选择器:

TO *to = …
Run Code Online (Sandbox Code Playgroud)

objective-c objective-c-runtime

5
推荐指数
1
解决办法
215
查看次数

标签 统计

file ×1

objective-c ×1

objective-c-runtime ×1

ruby ×1