小编bod*_*ous的帖子

ActiveSupport :: Memoizable引用哪种Ruby memoize模式?

所以在Rails 3.2中,不推荐使用ActiveSupport :: Memoizable.

消息如下:

DEPRECATION WARNING: ActiveSupport::Memoizable is deprecated and
will be removed in future releases,simply use Ruby memoization
pattern instead.
Run Code Online (Sandbox Code Playgroud)

它指的是"Ruby memoization pattern"(单数),好像有一个模式,我们都应该知道并参考...

我认为他们的意思是:

def my_method
  @my_method ||= # ... go get the value
end
Run Code Online (Sandbox Code Playgroud)

要么

def my_method
  return @my_method if defined?(@my_method)

  @my_method = # ... go get the value
end
Run Code Online (Sandbox Code Playgroud)

还有其他我错过的东西吗?

ruby memoization activesupport ruby-on-rails-3

54
推荐指数
2
解决办法
1万
查看次数

延迟选项在NGINXs listen指令中意味着什么?

我已经看到示例NGINX配置,其中"延迟"选项添加到listen指令中

server { 
  listen 80 default deferred;
  ... 
}
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚它的作用(以及我是否应该使用它),文档对我来说没有多大意义

延迟 - 表示在Linux上借助选项TCP_DEFER_ACCEPT使用推迟的accept(2)

任何人都可以解释这个选项是什么?

nginx

52
推荐指数
1
解决办法
2万
查看次数

使用REST拆分头发:标准JSON REST API是否违反了HATEOAS?

我今天早上在REST上做了一些阅读,我遇到了HATEOAS原则("超媒体作为应用程序状态的引擎").

引用REST维基百科页面:

客户端仅通过服务器在超媒体内动态识别的动作(例如,通过超文本内的超链接)进行状态转换.除了应用程序的简单固定入口点之外,客户端不会假定任何特定操作可用于除先前从服务器接收的表示中描述的任何特定资源之外的任何特定操作.

Roy Fielding的博客:

...如果应用程序状态的引擎(以及API)不是由超文本驱动的,那么它不能是RESTful的,也不能是REST API.期.

我将其读作:客户端可能只根据服务器响应主体(超文本)提供的操作请求状态更改.

在HTML世界中,这非常有意义.客户端应该只能根据通过超文本(HTML)提供的链接请求状态更改(新操作/页面).

当资源以其他方式表示时 - 例如JSON,XML,YAML等.这不是那么明显.

我们来看一个示例"REST"JSON API:

我通过发送POST请求来创建新资源(例如新注释)

/comments.json? # with params...

服务器响应:

# Headers
HTTP/1.1 201 Created 
Location: http://example.com/comments/3
Content-Type: application/json; charset=utf-8
... Etc.

# Body
{"id":3,"name":"Bodacious","body":"An awesome comment","post_id":"1"}
Run Code Online (Sandbox Code Playgroud)

我知道我现在可以在标题中返回的URI处访问此评论:http://example.com/comments/3.json

当我访问http://example.com/comments/3.json时,我看到:

{"id":3,"name":"Bodacious","body":"An awesome comment","post_id":"1"}
Run Code Online (Sandbox Code Playgroud)

假设API的文档告诉我可以通过向同一URI发送DELETE请求来删除此注释.这在"REST"API中相当常见.

然而:

来自服务器的响应GET http://example.com/comments/3.json并没有告诉我任何关于能够通过发送DELETE请求来删除注释的信息.它向我展示的是资源.

我也可以使用相同的URL删除注释,这是客户端通过带外信息(文档)知道的,并且不会被服务器的响应发现和驱动.

在这里,客户端假定DELETE操作(以及可能的其他人)可用于这一资源,并且此信息尚未先前从服务器接收.

我是否误解了HATEOAS,或者我是否正确地说,与严格意义上的上述描述相匹配的API不会是REST API?

我知道100%坚持REST并不总是可行或最务实的方式.我发布这个问题纯粹是为了满足我对REST背后理论的好奇心,而不是对现实世界最佳实践的建议.

api rest

40
推荐指数
5
解决办法
9440
查看次数

哪里是在iOS应用程序中存储常量的最佳位置?

我正在开发一个从JSON API获取资源的应用程序.

所有资源都具有相同的基本URL:

http://api.mysite.com/resources.json
http://api.mysite.com/other_resources.json
Run Code Online (Sandbox Code Playgroud)

我想存储http://api.mysite.com/字符串,以便它可供所有控制器和模型使用,在编写资源URL时删除一些重复.

哪里是最好的地方?该-prefix.pch文件?

任何建议表示赞赏

objective-c ios

37
推荐指数
3
解决办法
2万
查看次数

unsafe_unretained属性'title'的现有ivar'title'必须__unsafe_unretained

我刚刚掌握了Objective-C 2.0

当我尝试在Xcode中构建以下内容时,它失败了.编译器的错误如下:

unsafe_unretained属性'title'的现有ivar'title'必须是__unsafe_unretained.

// main.m
#import <Foundation/Foundation.h>
#import "Movie.h"
int main (int argc, const char * argv[]){
    Movie *movie = Movie.new;

    NSLog(@"%@", movie);

    return 0;
}

// movie.h
#import <Foundation/Foundation.h>

@interface Movie : NSObject{
    NSString *title;
    int year;
    int rating;
}

@property(assign) NSString *title;
@property(assign) int rating;
@property(assign) int year;

@end

#import "Movie.h"

@implementation Movie;

@synthesize title; // this seems to be issue - but I don't understand why?
@synthesize rating;
@synthesize year;

@end
Run Code Online (Sandbox Code Playgroud)

谁能解释我哪里出错了?

objective-c

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

如何为Rails的部分渲染查找添加视图路径?

我想要以下目录结构:

views/
  app1/
    users/_user.html.erb
    users/index.html.erb

  app2/
    users/index.html.erb

  shared/
    users/_user.html.erb
    users/index.html.erb
Run Code Online (Sandbox Code Playgroud)

在我看来,我打电话

# app1/users/index.html
<%= render :partial => "user" %>
# => /app1/users/_user.html.erb


# app2/users/index.html
<%= render :partial => "user" %>
# => /shared/users/_user.html.erb
Run Code Online (Sandbox Code Playgroud)

所以基本上,如何告诉Rails检查/ app2/users dir然后检查共享目录,然后才会引发它丢失的模板错误?

更新


我绕过了这个(正如Senthil建议的那样,使用 File.exist?

这是我的解决方案 - 欢迎提出反馈和建议

# application_helper.rb

# Checks for a partial in views/[vertical] before checking in views/shared
def partial_or_default(path_name, options={}, &block)
  path_components         = path_name.split("/")
  file_name               = path_components.pop
  vertical_file_path      = File.join(vertical}, path_components, file_name)
  shared_file_path        = File.join("shared", path_components, file_name)
  full_vertical_file_path = File.join("#{Rails.root}/app/views/", "_#{vertical_file_path}.html.erb")
  attempt_file_path …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails partial-views actionview actionviewhelper ruby-on-rails-3

20
推荐指数
1
解决办法
2万
查看次数

为什么我的Rails可安装引擎无法正确加载辅助方法?

我已经构建了一个可以作为引擎安装的rails gem.

引擎的作用域是它自己的命名空间.

在引擎中,有一个MyEngine::ApplicationHelper模块添加了一堆视图助手方法.

在我的应用程序布局中,我指的是其中一些方法.

当我第一次在开发模式下加载任何页面时,我得到一个NoMethodError,抱怨该方法(在gem中定义ApplicationHelper)不存在.

ApplicationController在我的应用内编辑后,问题就会自行解决.

有些东西告诉我这归结于Rails自动加载的最新变化; 我正在使用Rails 3.2.2

我不能为我的生活解决为什么这不能正常工作:/

rubygems ruby-on-rails ruby-on-rails-3.2

18
推荐指数
2
解决办法
1万
查看次数

XCode中的源代码控制是一场噩梦 - 任何人都可以提供建议吗?

使用Git和Xcode(4.3)是一场真正的噩梦.

这是一个场景......

我想添加一个新功能,所以我创建了一个新的主题分支.

我添加了我的新功能,我准备提交,改组和合并......

我承诺改变 - 很好.

我跳回到掌握以进行更改(以防其他人更新了代码).我突然得到:

error: Your local changes to the following files would be overwritten by checkout:
myProject/project.xcworkspace/xcuserdata/Bodacious.xcuserdatad/UserInterfaceState.xcuserstate
Run Code Online (Sandbox Code Playgroud)

是吧?我刚刚承诺.

Xcode喜欢project.xcworkspace每隔一秒更改一次我的文件,这使得几乎不可能进行干净的原子提交.

更重要的是,如果我确实提交了更改project.xcworkspace并快速跳转到另一个分支(例如为了合并到Master),那么Xcode会抱怨文件已经改变并且可能也会崩溃.

从我收集的内容来看,我无法将这些文件添加到我的文件.gitignore中.

我是否必须接受Xcode无法实现简洁有序的git策略,在进行任何Git管理之前关闭Xcode,还是有其他选择?

git version-control ios xcode4

9
推荐指数
1
解决办法
5012
查看次数

iOS推送通知不适用于使用Test Flight的临时分发

有点奇怪的问题......

在我的AppDelegate.m中,我有以下内容:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
    // Override point for customization after application launch.


    // Enable test flight reporting; https://testflightapp.com/sdk/doc/0.8.3/
    [TestFlight takeOff:@"myTestFlightToken"];

    // Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    return YES;
}

-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString *tokenAsString = [[deviceToken description] 
                                 stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

    NSLog(@"Device token: %@", deviceToken);
    User.currentUserPushNotificationToken = tokenAsString;
    [TestFlight passCheckpoint: [NSString stringWithFormat: @"Registered for remote notifications %@", deviceToken]];
}

-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{ …
Run Code Online (Sandbox Code Playgroud)

xcode push-notification apple-push-notifications ios testflight

9
推荐指数
1
解决办法
6583
查看次数

如何检查重用标识符是否已在UITableView中注册?

在iOS应用程序中,我们必须先使用表视图注册nib文件,然后才能使用UITableView#dequeueReusableCellWithIdentifier.

例:

static NSString *myReuseIdentifier = @"MyReuseIdentifier";
UINib *cellNib = [UINib nibWithNibName:myReuseIdentifier bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:myReuseIdentifier];
Run Code Online (Sandbox Code Playgroud)

有没有办法检查Nib是否已经注册了UITableView?

我有一个自定义单元格,我在我的应用程序中的几个控制器的各种表中使用.我想将一些代码移动到宏.就像是

-(CustomCell *)customCell:(UITableView *)tableView
{
    static NSString *reuseIdentifier = @"MyReuseIdentifier";
    if (![table hasAlreadyRegisteredNib:reuseIdentifier]){
       UINib *cellNib = [UINib nibWithNibName:reuseIdentifier bundle:nil];
       [self.tableView registerNib:cellNib forCellReuseIdentifier:reuseIdentifier];     
    }
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

objective-c ios xcode4

8
推荐指数
1
解决办法
6300
查看次数