相同的应用程序,不同的问 我正在使用Dan Benjamin"Meet Sinatra"截屏视频作为参考.我正在尝试包含一个自定义身份验证模块,它位于lib文件夹(lib/authentication.rb)中.我要求代码顶部的那行,但是当我尝试加载页面时,它声称没有这样的文件要加载.
有什么想法吗?
这是我的主要Sinatra文件的顶部:
require 'sinatra'
require 'rubygems'
require 'datamapper'
require 'dm-core'
require 'lib/authorization'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/entries.db")
class Entry
include DataMapper::Resource
property :id, Serial
property :first_name, String
property :last_name, String
property :email, String
property :created_at, DateTime
end
# create, upgrade, or migrate tables automatically
DataMapper.auto_upgrade!
helpers do
include Sinatra::Authorization
end
Run Code Online (Sandbox Code Playgroud)
而实际的模块:
module Sinatra
module Authorization
def auth
@auth ||= Rack::Auth::Basic::Request.new(request.env)
end
def unauthorized!(realm="Short URL Generator")
headers 'WWW-Authenticate' => %(Basic realm="#{realm}")
throw :halt, [ 401, 'Authorization Required' ]
end
def …Run Code Online (Sandbox Code Playgroud) According to the Wikipedia page, the width of a .bmp image is stored in the header of the file, in bytes 0x12 through 0x15. For example, in an image that is 256x256, bytes 0x12 through 0x15 would look like this; Ruby converts each byte to an integer:
file = File.open("red_bmp.bmp", "r")
bytes = file.bytes.to_a
bytes[0x12..0x15]
#=> [0, 1, 0, 0]
Run Code Online (Sandbox Code Playgroud)
为了将其转换为little-endian格式,我最好的解决方案是将每个十进制值转换为十六进制字符串,反转数组,连接元素,并将生成的十六进制字符串转换回整数.
width = bytes[0x12..0x15].map {|x| x.to_s(16).rjust(2, "0")}.reverse.join.to_i(16)
#=> 256
Run Code Online (Sandbox Code Playgroud)
x.to_s(16).rjust(2, "0"))?Mac OSX 10.6.8
postgres网站称postgres 9.2+与Mac OSX 10.6+兼容,所以我在这里下载了Mac OSX的9.2.4版安装程序:
http://www.enterprisedb.com/products-services-training/pgdownload
我接受了所有默认值,因此postgres安装在目录中:
/Library/PostgreSQL/9.2
Run Code Online (Sandbox Code Playgroud)
(如果要为rails开发安装postgres,为了安装pg gem,需要添加到PATH:http://excid3.com/blog/installing-postgresql-and-pg-gem-on-mac-osx /#.UfkImuB6gy5)
现在,我想弄清楚如何使用postgres.我找到了一个stackoverflow线程,说你可以用这个命令启动postgres:
~$ /Library/PostgreSQL/9.2/bin/pg_ctl start -D /Library/PostgreSQL/9.2/data -l postgres.log
Run Code Online (Sandbox Code Playgroud)
但是这会产生这个错误:
pg_ctl:无法打开PID文件"/Library/PostgreSQL/9.2/data/postmaster.pid":权限被拒绝
如果我用sudo尝试相同的命令:
~$ sudo /Library/PostgreSQL/9.2/bin/pg_ctl start -D /Library/PostgreSQL/9.2/data -l postgres.log
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
pg_ctl:无法以root身份运行请登录(使用,例如,"su")作为将拥有服务器进程的(非特权)用户.
这是什么意思??
另一个SO线程说,为了响应该错误,你应该这样做:
$ sudo -u postgres bash
Run Code Online (Sandbox Code Playgroud)
产生这个输出:
Password:
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
bash: /Users/7stud/.bashrc: Permission denied
bash-3.2$
Run Code Online (Sandbox Code Playgroud)
那里的错误是怎么回事?
关闭ARC:
我使用创建自定义类的实例alloc,因此它的保留计数为1.
在下一行,我NSLog()实例(我description在我的自定义类中实现了一个方法).
在下一行,我释放了该对象.
在下一行,我再次NSLog()实例,即我发送description消息,我在控制台中看到相同的输出.我期待某种错误输出.
这是相关代码:
AppDelegate.m:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
Greeter* host = [[Greeter alloc] initWithName:@"Maggie"]; //My custom class
NSLog(@"Greeter %@", host);
[host release];
NSLog(@"Greeter %@", host); //Create error: send a `description` message to an object whose retain count is 0.
return YES;
}
...
...
Run Code Online (Sandbox Code Playgroud)
Greeter.m:
...
- (NSString*)description {
return [
[[NSString alloc] initWithFormat:@"\n\tname: %@ \n\tCreated on %@", [self …Run Code Online (Sandbox Code Playgroud) 我是erlang的新手,我正在尝试实现一个简单的函数,如下所示:
% * ChatServers is a dictionary of usernames with tuples of the form:
% {server, Pid, Reference,LoggedInUsers}
get_chat_server([], _) ->
undefined;
get_chat_server([Key|_], ChatServers) ->
{server, Pid, Reference,LoggedInUsers} = dict:fetch(Key,ChatServers),
LoggedInUsers < 100,
{server, Pid, Reference,LoggedInUsers};
get_chat_server([_|T], ChatServers) ->
get_chat_server(T, ChatServers).
Run Code Online (Sandbox Code Playgroud)
基本上我要做的是找到我的字典的第一个元组,其LoggedInUsers数小于100.
但是,一旦我编译我的代码,我会得到以下2个警告:
main_server_distributed.erl:63:警告:使用运算符'<'无效main_server_distributed.erl:66:警告:此子句无法匹配,因为第61行的前一个子句始终匹配
我有一些prolog的经验,据我所知,这是模式匹配和递归的有效使用.你能指出我在这里做错了什么吗?提前致谢.
a.exs:
defmodule A do
def greet, do: IO.puts "hello"
end
Run Code Online (Sandbox Code Playgroud)
b.exs:
defmodule B do
import A
def say_hello, do: greet
end
Run Code Online (Sandbox Code Playgroud)
结果:
~/elixir_programs$ iex b.exs
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
** (CompileError) b.exs:2: module A is not loaded and could not be found
Run Code Online (Sandbox Code Playgroud)
~/elixir_programs$ tree .
.
??? a.exs
??? app1.exs
??? b.exs
....
Run Code Online (Sandbox Code Playgroud)
就此而言,如何使用限定名称来调用另一个模块中定义的函数:
b.exs:
defmodule B do
def say_hello, do: A.greet
end
Run Code Online (Sandbox Code Playgroud)
~/elixir_programs$ iex b.exs
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] …Run Code Online (Sandbox Code Playgroud) 这是我正在看的代码:
#!/bin/bash
nc -l 8080 &
curl "http://localhost:8080" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--data @<(cat <<EOF
{
"me": "$USER",
"something": $(date +%s)
}
EOF
)
Run Code Online (Sandbox Code Playgroud)
怎么@办?哪里有文件@?
我最近选择了Python,并想知道如何做到以下几点.假设我们有3个数字的列表:
x = [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
然后,我们询问用户如何处理这些数字:
whatdo = raw_input('> ')
Run Code Online (Sandbox Code Playgroud)
例如,用户输入"+2".现在如何将"+ 2"应用于列表的所有元素?
我安装了prostgres以与我的rails4应用程序一起使用,我能够连接到数据库服务器并创建数据库.然后我对我的Gemfile和config/database.yml进行了必要的更改以使用postgres.然后我创建了一个新的应用程序,并在应用程序中创建了一个用户模型:
$ rails generate model User name:string email:string
invoke active_record
create db/migrate/20130806145935_create_users.rb
create app/models/user.rb
invoke rspec
create spec/models/user_spec.rb
Run Code Online (Sandbox Code Playgroud)
然后我做了:
$ bundle exec rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.1498s
== CreateUsers: migrated (0.1500s) ===========================================
Run Code Online (Sandbox Code Playgroud)
但是在db目录中,我看到了这些文件:
development.sqlite3
test.sqlite3
Run Code Online (Sandbox Code Playgroud)
此外,当我尝试使用SQLite数据库浏览器查看这些文件时,我收到一条错误消息,指出它们不是sqlite 3数据库.事实上,它们是空的:
~/rails_projects/sample_app4_0/db$ ls -al
total 40
drwxr-xr-x 8 7stud staff 272 Aug 6 22:50 .
drwxr-xr-x 24 7stud staff 816 Aug 6 22:23 ..
-rw-r--r-- 1 7stud staff 0 Jul 30 00:21 development.sqlite3
drwxr-xr-x 3 7stud staff 102 Aug …Run Code Online (Sandbox Code Playgroud) 当我打电话时gen_server:reply/2:
gen_server:reply(From, Msg),
Run Code Online (Sandbox Code Playgroud)
客户端,From,接收具有以下格式的消息:
{Ref, Msg)
Run Code Online (Sandbox Code Playgroud)
我找不到有关由 发送的消息格式的任何文档gen_server:reply/2,我想知道如何Ref对消息中的进行模式匹配。目前,我使用了一个不关心的变量Ref:
receive
{_Ref, Msg} -> Msg;
Other -> Other
end
Run Code Online (Sandbox Code Playgroud)
这意味着除 之外的进程gen_server可能会向我的客户发送与该{_Ref, Msg}子句匹配的消息。
erlang ×2
erlang-otp ×2
ruby ×2
bash ×1
curl ×1
elixir ×1
gen-server ×1
ios8 ×1
objective-c ×1
python ×1
sinatra ×1
xcode6 ×1