我有一个程序可以为我创建工作笔记,它有效,但是有一个我想摆脱的尾随短划线:
def prompt(input)
print "[#{Time.now.strftime('%T')}] #{input}: "
STDIN.gets.chomp
end
def work_performed
count = 0
notes = ''
while true
input = prompt("Enter work notes[#{count += 1}]")
notes << "\n" + "#{input}\n"
if input.empty?
return notes
else
while input.empty? != true
input = prompt('Enter work notes[*]')
notes << " - #{input}\n"
end
end
end
end
运行时:
test
- tset
-
test
- tset
-
tset
- tset
-
如何重构这个以消除关卡末端的尾随破折号?
答案 0 :(得分:5)
library(akima)
x <- nest6$reLDM
y <- nest6$yr
y2 <- y^2
z <- nest6$TCL
m <- lm(z ~ x*y+y2+x:y2)
i <- 25
xtemp <- seq(min(x),max(x),length.out=i)
xrange <- rep(xtemp,times=i)
ytemp <- seq(min(y),max(y),length.out=i)
yrange <- rep(ytemp,each=i)
y2temp <- seq(min(y2),max(y2),length.out=i)
y2range <- rep(y2temp,each=i)
newdata <- data.frame(x=xrange,y=yrange,y2=y2range)
zhat <- predict(m,newdata=newdata)
xyz <- interp(xrange,yrange,zhat)
jet.colors <- colorRampPalette( c("yellow", "red", "blue") )
nbcol <- 500
color <- jet.colors(nbcol)
nrz <- length(xyz[[1]])
ncz <- length(xyz[[2]])
z<-xyz[[3]]
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
facetcol <- cut(zfacet, nbcol)
quartz()
persp(xyz,xlab="x",ylab="y",zlab="z", cex.lab = 1,cex.axis = 1,
theta = 35, phi = 50,col=color[facetcol], border="grey40", ticktype = "detailed", zlim=c(1,7))
将始终附加内容,即使<< " - #{input}\n"
为空字符串,因此您可以检查它是否为空,以便有条件地附加。
input
答案 1 :(得分:3)
我建议你这样写。
<强>代码强>
require 'time'
def prompt(input)
print "[#{Time.now.strftime('%T')}] #{input}: "
gets.chomp
end
def work_performed
1.step.each_with_object([]) do |count, notes|
loop do
input = prompt "Enter work notes[#{count}]"
return notes.join("\n") if input.empty?
notes << input
loop do
input = prompt("Enter work notes[*]")
break if input.empty?
notes << " - #{input}"
end
end
end
end
示例强>
让我们使用以下提示和条目进行尝试:
[11:38:35] Enter work notes[1]: Pets
[11:38:39] Enter work notes[*]: dog
[11:38:40] Enter work notes[*]: cat
[11:38:41] Enter work notes[*]: pig
[11:38:42] Enter work notes[*]:
[11:38:43] Enter work notes[1]: Friends
[11:38:53] Enter work notes[*]: Lucy
[11:38:55] Enter work notes[*]: Billy-Bo
[11:39:04] Enter work notes[*]:
[11:39:06] Enter work notes[1]: Colours
[11:39:15] Enter work notes[*]: red
[11:39:18] Enter work notes[*]: blue
[11:39:20] Enter work notes[*]:
[11:39:22] Enter work notes[1]:
我们获得:
puts work_performed
Pets
- dog
- cat
- pig
Friends
- Lucy
- Billy-Bo
Colours
- red
- blue
备注强>
notes
设为数组而非字符串,因此需要notes.join("\n")
。1.step
返回一个枚举器,该枚举器生成以1
开头的自然数字(请参阅Numeric#step)。loop do
(请参阅Kernel#loop)比while true
更惯用。