小编har*_*ell的帖子

使用class_eval和instance_eval访问Ruby类变量

我有以下内容:

class Test
    @@a = 10

    def show_a()
        puts "a: #{@@a}"
    end

    class << self
      @@b = '40'

      def show_b
        puts "b: #{@@b}"
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

为什么以下工作:

Test.instance_eval{show_b}
b: 40
=> nil
Run Code Online (Sandbox Code Playgroud)

但我无法@@b直接访问?

Test.instance_eval{ @@b }
NameError: uninitialized class variable @@b in Object
Run Code Online (Sandbox Code Playgroud)

同样,以下工作

t = Test.new
t.instance_eval{show_a}
a: 10
=> nil
Run Code Online (Sandbox Code Playgroud)

但是以下失败了

t.instance_eval{ @@a }
NameError: uninitialized class variable @@a in Object
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我不能直接从instance_eval块访问类变量.

ruby variables class instance-eval

22
推荐指数
3
解决办法
1万
查看次数

NSInvocation类上的setSelector方法的目的是什么?

我不明白为什么我们必须setSelectorNSInvocation对象上调用该方法,因为该信息已经通过invocationWithMethodSignature.

要创建NSInvocation对象,我们执行以下操作:

SEL someSelector;
NSMethodSignature *signature;
NSInvocation *invocation;

someSelector = @selector(sayHelloWithString:);

//Here we use the selector to create the signature
signature = [SomeObject instanceMethodSignatureForSelector:someSelector];
invocation = [NSInvocation invocationWithMethodSignature:signature];

//Here, we again set the same selector
[invocation setSelector:someSelector];
[invocation setTarget:someObjectInstance];
[invocation setArgument:@"Loving C" atIndex:2];
Run Code Online (Sandbox Code Playgroud)

请注意,我们将选择器传递给[SomeObject instanceMethodSignatureForSelector: someSelector];并再次传递给[invocation setSelector:someSelector];.

有什么我想念的吗?

methods language-design dynamic objective-c nsinvocation

10
推荐指数
1
解决办法
715
查看次数

colorWithSRGBRed与colorWithDeviceRed和colorWithCalibratedRed之间的区别是什么

有几种方便类方法可用于创建NSColor.但是,我似乎无法确定何时使用下面的不同类方法:

colorWithSRGBRed:green:blue:alpha:vs colorWithDeviceRed:green:blue:alpha:vscolorWithCalibratedRed:green:blue:alpha:

macos objective-c nscolor

9
推荐指数
2
解决办法
2784
查看次数

在图层暂停时更新CALayer位置是否存在问题?

在暂停时阅读演示文稿位置是否存在问题?

我想暂停并恢复一个CALayer.一旦CALayer暂停,我想用它的当前演示位置更新图层的位置.当我尝试这样做时,一旦我恢复图层,图层会稍微闪烁.

这是我用来暂停和恢复的代码CALayer(基于Apple提供的技术问答QA1673):

CFTimeInterval pausedTime;
void pauseLayer(CALayer *layer)
{
    pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
    layer.beginTime = 0;
//    layer.position = ((CALayer*)[layer presentationLayer]).position;
}
void resumeLayer(CALayer *layer)
{
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;

    CFTimeInterval _elapsedTimeSincePaused = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = _elapsedTimeSincePaused;
}
Run Code Online (Sandbox Code Playgroud)

如果我取消注释layer.position = ((CALayer*)[layer presentationLayer]).position;in pauseLayer,那么一旦我打电话,图层就会闪烁resumeLayer.

这是我的动画代码:

- (void) startAnimation:(id)sender
{
    layer10Animation = [CABasicAnimation …
Run Code Online (Sandbox Code Playgroud)

iphone animation core-animation objective-c calayer

6
推荐指数
1
解决办法
1800
查看次数

rails technoweenie/restful-authentication magi-code:找不到`User #register!`

我最近安装了technoweenie/restful-authentication插件(按照承诺的方式工作),但是在经历过的时候users_controller#created,我发现了对user模型上的方法调用的引用

 @user.register!
Run Code Online (Sandbox Code Playgroud)

有谁知道定义方法的位置?我几乎搜索了所有生成的代码,但仍然没有看到寄存器方法.

ruby-on-rails restful-authentication

4
推荐指数
1
解决办法
490
查看次数

Rails如何在erb模板中产生多个块?

rails如何在.erb文件中使用以下内容?

<%= yield :sidebar %>
<%= yield :other_bar %>
<%= yield :footer %>
Run Code Online (Sandbox Code Playgroud)

他们如何能够在不同符号的相同上下文中多次产生?这是某种魔力吗?

我完全熟悉:

def some_method(arg1, arg2, &block)
 yield(:block)
end
Run Code Online (Sandbox Code Playgroud)

据我所知,以下不起作用:

def some_incorrect_method(arg1, &block1, &block2)
 yield(:block1)
 yield(:block2)
end
Run Code Online (Sandbox Code Playgroud)

那么他们是怎么做到的呢?他们如何使它工作?

ruby yield ruby-on-rails

4
推荐指数
1
解决办法
3730
查看次数