class A
private
def foo
puts :foo
end
public
def bar
puts :bar
end
private
def zim
puts :zim
end
protected
def dib
puts :dib
end
end
Run Code Online (Sandbox Code Playgroud)
a = A.new
Run Code Online (Sandbox Code Playgroud)
a.foo rescue puts :fail
a.bar rescue puts :fail
a.zim rescue puts :fail
a.dib rescue puts :fail
a.gaz rescue puts :fail
Run Code Online (Sandbox Code Playgroud)
fail
bar
fail
fail
fail
Run Code Online (Sandbox Code Playgroud)
[:foo, :bar, :zim, :dib, :gaz].each { |m| a.send(m) rescue puts :fail }
Run Code Online (Sandbox Code Playgroud)
foo
bar
zim
dib
fail
Run Code Online (Sandbox Code Playgroud)
标有"测试输出"的部分是预期结果.那么为什么我可以简单地访问私有/受保护的方法 …
说我有一个助手:
def foo(s)
"hello #{s}"
end
Run Code Online (Sandbox Code Playgroud)
视图可能会使用这样的帮助:
<div class="data"><%= foo(@user.name) %></div>
Run Code Online (Sandbox Code Playgroud)
但是,这<div>将通过重复的ajax调用进行更新.我正在to_json控制器中使用调用返回数据,如下所示:
render :text => @item.to_json(:only => [...], :methods => [:foo])
Run Code Online (Sandbox Code Playgroud)
这意味着,我必须foo在我的Item模型中定义:
class Item
def foo
"hello #{name}"
end
end
Run Code Online (Sandbox Code Playgroud)
这会是很好,如果我能有一个干,可以在共享的方法都我的观点和我的模型.
def say_hello(s)
"hello #{s}"
end
Run Code Online (Sandbox Code Playgroud)
def foo
say_hello(name)
end
Run Code Online (Sandbox Code Playgroud)
def foo
say_hello(label)
end
Run Code Online (Sandbox Code Playgroud)
<div class="data"><%= item.foo %></div>
Run Code Online (Sandbox Code Playgroud)
def observe
@items = item.find(...)
render :text => @items.to_json(:only=>[...], …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种优雅的方式来改变这个数组:
Array (
[foo] => 1
[bar] => 1
[zim] => 3
[dib] => 6
[gir] => 1
[gaz] => 3
)
Run Code Online (Sandbox Code Playgroud)
进入这个数组:
Array (
[1] => Array ( foo, bar, gir ),
[3] => Array ( zim, gaz ),
[6] => Array ( dib )
)
Run Code Online (Sandbox Code Playgroud)
注意:键或值之间没有关系.它们完全是任意的,仅用作示例.结果数组应该是一个按输入数组的值分组的关联数组.
谢谢!
我正在开发的网站将是西班牙语.因此,我需要使用该语言的错误消息.
我在Configuration目录下创建了一个名为'en.yml'的文件,以实现此目的.我在其中添加了以下代码:
es:
activerecord:
errors:
models:
announcement:
attributes:
title:
blank: "El título no puede estar vacío."
Run Code Online (Sandbox Code Playgroud)
"Eltítulonopuedeestarvacío"的意思是"标题不能为空白".
当我运行此代码时,我看到如下消息:
"标题Eltítulonopuedeestarvacío."
其中"标题"是字段的名称.但我不想让它显示出来.我只想显示我创建的错误消息.
我有两个表players和scores.
我想生成一个类似于下面的报告:
player first score points
foo 2010-05-20 19
bar 2010-04-15 29
baz 2010-02-04 13
Run Code Online (Sandbox Code Playgroud)
现在,我的查询看起来像这样:
select p.name player,
min(s.date) first_score,
s.points points
from players p
join scores s on s.player_id = p.id
group by p.name, s.points
Run Code Online (Sandbox Code Playgroud)
我需要s.points与min(s.date)返回的行相关联的那个.这个查询会发生这种情况吗?也就是说,我怎么能确定我正在s.points为连接的行获得正确的值?
旁注:我认为这与MySQL缺乏密集排名有某种关系.这里最好的解决方法是什么?
如果我正在使用@synthesize foo;,以下内容之间有什么区别:
// message syntax
[myObj setFoo:5];
[myObj foo];
Run Code Online (Sandbox Code Playgroud)
和
// dot syntax
myObj.foo = 5;
myObj.foo;
Run Code Online (Sandbox Code Playgroud)
我喜欢点语法的一致性,但我不知道它是否正在做我应该关心的事情.
任何其他信息都将是一个很大的帮助.
我正在尝试创建一个可以改变它的textmate片段:
HELLO WORLD<br />
SAY ANYTHING
Run Code Online (Sandbox Code Playgroud)
对此:
hello world say anything
Run Code Online (Sandbox Code Playgroud)
有帮助吗?
我试图在我的用户欢迎电子邮件中提供确认链接,我收到以下Rails错误:
Need controller and action!
Run Code Online (Sandbox Code Playgroud)
它对这一行大惊小怪:
<p>Please take a moment to activate your account by going to:
<%= link_to confirm_user_url(:id => @user.confirmation_code) %>.</p>
Run Code Online (Sandbox Code Playgroud)
在我的development.rb环境中,我有以下几行:
config.action_mailer.default_url_options = {
:host => "localhost", :port => 3000
}
Run Code Online (Sandbox Code Playgroud)
@user变量没有问题.我测试过的电子邮件之类的东西@user.username和@user.confirmation_code.我只是遇到麻烦url_for和命名路线,如confirm_user_url.
当我检查我的路线时rake routes,confirm_user显示,所以这不是指定路线不存在的问题.
我似乎无法弄明白.是什么赋予了?
无需更改函数签名,我希望PHP函数在给定关联数组而不是常规数组时表现不同.
注意:您可以假设数组是同质的.例如,array(1,2,"foo" => "bar")不被接受,可以忽略.
function my_func(Array $foo){
if (…) {
echo "Found associated array";
}
else {
echo "Found regular array";
}
}
my_func(array("foo" => "bar", "hello" => "world"));
# => "Found associated array"
my_func(array(1,2,3,4));
# => "Found regular array"
Run Code Online (Sandbox Code Playgroud)
这可能用PHP吗?
我要创建一个(类)工厂类,接受可变数量的参数并将它们传递给它将调用的类
<?php
class A {
private $a;
private $b;
private $c;
public function __construct($a=1, $b=2, $c=3){
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
}
class B {
private $foo;
public function __construct(){
$args = func_get_args();
$this->foo = call_user_func_array(array('A', '__construct'), $args);
}
public function getObject(){
return $this->foo;
}
}
$b = new B(10, 20, 30);
var_dump($b->getObject()); // should return equivalent of new A(10,20,30);
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, non-static method A::__construct() …Run Code Online (Sandbox Code Playgroud) php ×3
arrays ×2
actionmailer ×1
activerecord ×1
dense-rank ×1
factory ×1
join ×1
mysql ×1
objective-c ×1
oop ×1
pcre ×1
ranking ×1
regex ×1
routes ×1
ruby ×1
syntax ×1
textmate ×1