def test
"Hello World"
end
p method(:test).call #"Hello World"
p method("test").call #"Hello World"
Run Code Online (Sandbox Code Playgroud)
我的问题是:当我们将符号传递给call方法时会发生什么?ruby会将符号转换为String然后执行吗?如果是这样,那么它的目的是什么?
如果没有,那么实际发生了什么?你能详细说明吗?对不起,如果我没有说清楚.
let vec1 = vec![1, 2, 3, 4];
let vec2 = vec![Box::new(1), Box::new(2), Box::new(3), Box::new(4)];
Run Code Online (Sandbox Code Playgroud)
他们之间有什么区别?我已经在堆上分配了vec1.那么vec1的所有元素都不在堆上吗?为什么我需要像在vec2中那样在堆上单独分配它们?
class A
def test
"Test from instance"
end
class << self
def test
"Test from class"
end
end
end
p A.send(:test) # "Test from class"
p A.new.method(:test).call # "Test from instance"
Run Code Online (Sandbox Code Playgroud)
符号按预期工作,但在这里:
s="test"
s1=:s
p s1 # :s
Run Code Online (Sandbox Code Playgroud)
为什么:s在这里打印?我不明白它背后的原因.有人可以帮我解释一下吗?
<ul>
<% for file in @files %>
<li><%= file %></li>
<% end %>
</ul>
Run Code Online (Sandbox Code Playgroud)
输出:
但如果我想使用枚举器:
<% @files.each{|file| file} %>
Run Code Online (Sandbox Code Playgroud)
输出:['file1','file2']
我无法将输出作为<\ li>列表.
我的问题是是否可以使用枚举获取列表输出?如果是这样的话?我是rails的初学者.
ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2
>>> odd,even=[ ],[ ]
>>> [even.append(x) if x%2==0 else odd.append(x) for x in range(51)]
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
>>> odd
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, …Run Code Online (Sandbox Code Playgroud) 在浏览ActiveRecord源代码时,我发现:
class ActiveRecord::Base
Run Code Online (Sandbox Code Playgroud)
我不明白课程名称是怎么样的.ActiveRecord::Base
请为我解释,我没有得到这个概念.
在我的Rails项目中,我正在使用Postgresql.
User.find_by_sql("update users set address = '#{params[:address]}' where id = #{current_user.id}")
Run Code Online (Sandbox Code Playgroud)
此代码工作正常,但等效的activerecord不起作用:
user = User.find(current_user.id)
user.update_attributes(:address => params[:address])
Run Code Online (Sandbox Code Playgroud)
它总是显示Begin; Rollback在日志中.你能解释一下为什么会这样吗?有任何与Postgres有关的问题吗?我正在使用Rails 3.2
编辑
我发现了我错在哪里......验证失败导致了这一点.解决方案是:
user.update_column(:address,params[:address])
Run Code Online (Sandbox Code Playgroud)
这将绕过验证.谢谢你们的帮助.