在我的构造函数中,我想创建一个随机颜色.
因此,我需要三个随机的7位浮点数,范围为0 ... 1,构成颜色的红色,绿色和蓝色分量.random() % 128 / 128.0我把它放在一个块中,而不是写了相当长的三次:
CGFloat (^randFloat)() = ^(){ return random() % 128 / 128.0; };
color = CGColorCreateGenericRGB(randFloat(), randFloat(), randFloat(), .5);
Run Code Online (Sandbox Code Playgroud)
这是使用块的有效方法吗?
如果没有,你会用什么呢?
我在使用Ruby块时遇到一些困难,传入一个方法.
在下面的例子中,我想从Box实例中显示@array的每个元素(使用.each方法):
class Box
def initialize
@array = [:foo, :bar]
end
def each(&block)
# well, hm..
end
end
a = Box.new
a.each { |element| puts element }
Run Code Online (Sandbox Code Playgroud) 我对ruby中块绑定的定义感到困惑.这是两个样本:
def redfine_a(&block)
a = 2
puts eval("a", block.binding)
block.call
end
a = "hello"
puts redfine_a {a}
Run Code Online (Sandbox Code Playgroud)
这个可以运行预期的结果,而第二个:
def redefine_a(&block)
a= 2
puts eval("a", block.binding)
block.call
end
puts redefine_a{a= "hello"}
Run Code Online (Sandbox Code Playgroud)
会抱怨:
undefined local variable or method `a' for main:Object (NameError)
Run Code Online (Sandbox Code Playgroud)
删除后puts eval("a", block.binding),我的代码运行:
def redefine_a(&block)
a= 2
block.call
end
puts redefine_a{a= "hello"}
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
此方法适用于我的本地计算机,但在远程服务器上不起作用.它正在寻找一个区块,但我不知道"在哪里"或"如何"放置它.
def generate_csv
if params[:print_envelopes]
@signups = CardBatch.find(params[:id]).card_signups.each.reject { |a| a.envelope? }
@filename = 'envelopes.csv'
csv_string = FasterCSV.generate do |csv|
csv << ["first name, last name, street address, city, state, zip code"]
@signups.each do |signup|
csv << [signup.first_name, signup.last_name, signup.street_address_1, signup.city, signup.state, signup.zip_code]
signup.envelope = true
signup.save
end
end
elsif params[:print_cards]
@signups = CardBatch.find(params[:id]).card_signups.reject { |a| a.card? }
@filename = 'cards.csv'
csv_string = FasterCSV.generate do |csv|
csv << ["name", "expiration date", "card number"]
@signups.each do |signup|
csv << [signup.full_name, signup.exp_date.strftime('%m/%y'), …Run Code Online (Sandbox Code Playgroud) 我正在创建一个文件阅读程序.我需要过滤掉任何不是'0-9'或'.'的字符.
除了这些之外的任何字符都需要触发IF语句.
这是我试过的 -
if ( ( ((char)c < '0') || ((char)c > '9') ) || ((char)c != '.') )
Run Code Online (Sandbox Code Playgroud)
要么-
( ( ((char)c != '0' ) || ((char)c != '.' ) || ((char)c != '1' ) || ((char)c != '2' ) || ((char)c != '3' ) || ((char)c != '4' ) || ((char)c != '5' ) || ((char)c != '6' ) || ((char)c != '7' ) || ((char)c != '8' ) || ((char)c != '9' ) ))
Run Code Online (Sandbox Code Playgroud)
两者都不起作用.
我想将变量粘贴到我用于子集数据的逻辑表达式中,但是子集函数在粘贴时不会将它们看作列名(ot不带引号).
我有一个名为col1,col2等列的数据框.我想为colx <0.05的行进行子集化
这个工作:
subsetdata<-subset(dataframe, col1<0.05)
subsetdata<-subset(dataframe, col2<0.05)
Run Code Online (Sandbox Code Playgroud)
这不起作用:
for (k in 1:2){
subsetdata<-subset(dataframe, paste("col",k,sep="")<0.05)
}
for (k in 1:2){
subsetdata<-subset(dataframe, noquote(paste("col",k,sep=""))<0.05)
}
Run Code Online (Sandbox Code Playgroud)
我找不到答案; 有什么建议?
编辑:在C++如果我有,我需要测试8个标志.哪个在处理速度方面更有效:
在单个if语句中检查8个bool变量或者使用表示标志的char,每个标志使用1位并在单个if语句中使用单个按位运算?
如果我有1个标志要测试,那会有所作为吗?
我最近发现了一条javascript正在评估null/undefined,如下所示:
// The single & is on purpose!
if(x !== null & x !== undefined) {
// Do this...
}
Run Code Online (Sandbox Code Playgroud)
这看起来对我来说是一种非常糟糕的做法,但似乎有效.
据我所知,这些操作符应该执行两个不同的任务:
var x = 3; x &= 6; // No syntax errors.
var x = 3; x &&= 6; // Syntax error.
if(x == null && x == undefined) // No syntax errors.
if(x == null & x == undefined) // No syntax errors, but wrong operator usage?
Run Code Online (Sandbox Code Playgroud)
有人可以对此有所了解吗?
我有以下R脚本:
x <- c('PE', 'MG', 'SP', 'GO', 'ES', 'PB', 'DF')
y <- c('PB', 'MG', 'SP', 'GO', 'ES', 'SE', 'DF')
z <- x == y
Run Code Online (Sandbox Code Playgroud)
以便
> z
[1] FALSE TRUE TRUE TRUE TRUE FALSE TRUE
Run Code Online (Sandbox Code Playgroud)
但是,我希望z(以及脚本中的其他逻辑变量)显示"是"和"否",所以我执行此重新编码:
z <- ifelse(z == TRUE, "Yes", "No")
Run Code Online (Sandbox Code Playgroud)
有没有办法跳过这个额外的步骤,即定义zshow"Yes"而不是"TRUE"和"No"而不是"FALSE".
当然,我也可以这样做z <- ifelse(x == y, "Yes", "No"),但我仍然在寻找类似于options()我可以定义一次的函数内部的参数,并让它工作到脚本结束(或直到我重新定义参数).找不到类似的东西?options.
我已尝试使用内置数据集重现此问题,但它只发生在我自己的数据集中.
如果我们采用我的数据的随机子集:
structure(list(ID = structure(c(27L, 1L, 27L, 7L, 5L, 10L, 23L,
19L, 21L, 26L), .Label = c("AC ", "AJ ", "AT ", "AWY", "BP ",
"BW ", "CA ", "CK ", "CS ", "DJ ", "EN ", "ES ", "HF ", "HG ",
"HL ", "HR ", "IP ", "JA ", "JG ", "JN ", "KB ", "KP ", "MJ ",
"PC ", "RFH", "RPA", "SB ", "SG ", "TM "), class = "factor"),
TNO = c(30L, 60L, 30L, 10000L, …Run Code Online (Sandbox Code Playgroud)