基本上,我知道在命令行中使用"/?"调用某些应用程序 吐出一个格式化的列表,列出如何使用params从命令行调用app.此外,这些应用程序有时甚至会弹出一个框,提醒用户程序只能在传入的某些参数下运行并提供此详细的格式化参数(类似于命令提示符输出).
他们是如何做到的(/?对我来说比弹出框更重要)?
有谁知道如何将以下内容写成一行?
email = params[:user][:email]
@user = User.new(:email => email, :password => generate_random_string)
Run Code Online (Sandbox Code Playgroud)
我想把它写成
@user = User.new(params[:user]. :password => generate_random_string)
Run Code Online (Sandbox Code Playgroud) 我想将设计中的current_user.id添加到另一个模型中......
问题模型:
class Question < ActiveRecord::Base
belongs_to :user
attr_accessible :content
validates :content, :presence => true
end
Run Code Online (Sandbox Code Playgroud)
用户模型:
class User < ActiveRecord::Base
has_many :questions
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
Run Code Online (Sandbox Code Playgroud)
我的问题迁移:
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.string :content
t.date :deadline
t.integer :user_id
t.timestamps
end …Run Code Online (Sandbox Code Playgroud) 我将名为grossScores的地图传递给GSP,并且需要使用GSP中的另一个变量来引用地图的各个实例.例如,
grossScore.Bob = 5
Run Code Online (Sandbox Code Playgroud)
发布如何通过.get(...)间接引用grails GSP模型变量是有帮助的,但我仍然无法实现.
我试过了:
${grossScore."{$player}"}
${pageScope.getProperty("grossScore.${player}")}
${request.getAttribute("grossScore.${player}")}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
这是我目前不能令人满意的解决方案,在传递给超类构造函数之前操纵传递给子类构造函数的值的问题,
class MissingItemsException(items: Set[String], itemsCategory: String)
extends RuntimeException(MissingItemsException.makeMessage(items, itemsCategory))
private object MissingItemsException {
private def makeMessage(items: Set[String], itemsCategory: String): String = {
/* Format set as ['?','?','?'] */
val Items = items mkString ("['", "','", "']")
"the following items %s were missing from '%s'" format (items, itemsCategory)
}
}
Run Code Online (Sandbox Code Playgroud)
是否有一种方法可以分解转换,以便转换代码保持接近使用点,同时保持代码清晰?
我想我已经错过了明显的某个地方,解析格式参数的最佳方法是什么...
'{"phonenumber":"123456", "mobile":"589521215", "website":"www.xfty.co.uk" }'
Run Code Online (Sandbox Code Playgroud)
所以我有个别变量?
我试图通过反射调用接受单个params参数的泛型方法.当我选择它是非泛型的时,传递一个object []项似乎已经足够了,但是当我需要调用泛型方法时它不再起作用了.
var type = typeof (ClassWithGenericMethod);
var method = type.GetMethod("GenericMethod", BindingFlags.Instance | BindingFlags.Public);
var genericMethod = method.MakeGenericMethod(typeof(object));
var result = (bool)genericMethod.Invoke(new ClassWithGenericMethod(), new object[]{"param"});
Assert.IsTrue(result);
Run Code Online (Sandbox Code Playgroud)
被叫班:
public class ClassWithGenericMethod
{
public bool GenericMethod<T>(params string[] input)
{
return input.Length == 1;
}
}
Run Code Online (Sandbox Code Playgroud)
在断言之前代码失败,出现以下异常:
"System.String"类型的对象无法转换为"System.String []"类型.
我已经阅读了关于将对象[]传递给params对象[]的主题,但我不知道为什么它不能与我合作.
我在课堂上也有这些功能:
...
private void CallbackEvent(object source, CallbackEvetArgs e) { // Some event with e.Data as string
...
string[] values = e.Data.Split('|');
DoSave("save", values.Skip(1).Cast<object>().ToArray());
...
}
...
public void DoSave(string action, params object[] values) {
...
string value1 = values[0];
...
}
...
Run Code Online (Sandbox Code Playgroud)
但是,value1不是接收value1中的字符串,而是接收整个数组(string []),因此无效的转换异常.
我究竟做错了什么?
这是一个很难回答的问题,但我会试着解释一下.我将Class其构造函数和参数作为对象.我需要做的是一个函数,它返回这个类的一个实例,将这些参数传递给构造函数.
这是代码:
一些随机和不可修改的类:
public Foo {
public function Foo(a:int, b:String) {
// constructor
}
}
Run Code Online (Sandbox Code Playgroud)
还有一些功能(在另一个类中):
function bar(params:Object):* {
var baz:Foo = new Foo(params.a, params.b);
return baz;
}
Run Code Online (Sandbox Code Playgroud)
我需要做的是使这个函数通用,没有params作为参数传递给Foo构造函数,因为我无法修改它.就像是:
function bar2(clazz:Class, params:Object):* {
var baz:* = new clazz(/*some magic way to transform params in comma separated parameters*/);
return baz;
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
非常感谢.
我正在尝试这样做:
public void CustomMethod(params int[] number,params string[] names)
{
...
}
Run Code Online (Sandbox Code Playgroud)
如果我删除其中一个,没有问题,任何想法为什么我不能这样做?
我试过把一个正常的参数放在两者的中间.
params ×10
c# ×4
constructor ×2
activerecord ×1
casting ×1
class ×1
controller ×1
generics ×1
grails ×1
gsp ×1
invoke ×1
json ×1
map ×1
object ×1
overloading ×1
parsing ×1
php ×1
reflection ×1
scala ×1