我正在尝试创建一个删除另一个函数的函数.
def delete_function(func):
del func
Run Code Online (Sandbox Code Playgroud)
是我到目前为止,但由于某种原因,它不起作用.
def foo():
print("foo")
delete_function(foo)
Run Code Online (Sandbox Code Playgroud)
似乎没有做到这一点.我知道一个人可以轻松地做到这一点
del(foo)
Run Code Online (Sandbox Code Playgroud)
但我试图以不同的方式做到这一点.怎么样?
我是Haskell的新手,我实际上刚刚在10分钟前开始.我试图弄清楚如何在函数内定义变量.让我们说我有这个功能
foo :: Int -> Int
foo a =
b = a * 2
b
-- Yes, I know, it doesn't do anything interesting
Run Code Online (Sandbox Code Playgroud)
当我运行它时,GHCi我得到一个语法错误!如何在函数内定义变量?
def size_of_dir(dirname):
print("Size of directory: ")
print(os.path.getsize(dirname))
Run Code Online (Sandbox Code Playgroud)
是有问题的代码.dirname是一个包含130个文件的目录,每个文件大约1kb.当我调用这个函数时,它返回4624,这不是目录的大小......为什么会这样?
我有以下代码,它基本上切换了一堆图像.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var num = 1;
img = document.getElementsByTagName("img")[0];
hbutton = document.getElementsByTagName("h1")[0];
hbutton.onclick = function() {
num += 1;
img.src = num + ".jpg";
}
</script>
</head>
<body>
<h1>Press Here!</h1>
<img src = "1.jpg"></img>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当我运行它时,没有任何反应,因为我的Firebug控制台显示以下错误.
hbutton is undefined
---
hbutton.onclick = function() {
Run Code Online (Sandbox Code Playgroud)
当我在页面加载后只运行JS时,它工作得非常好!!! 为什么是这样?
module Pigged
String.class_eval do
def pig
newstring = self + self[0]; newstring[0] = ""; newstring += "ay"
return newstring
end
end
end
Run Code Online (Sandbox Code Playgroud)
是相关的代码.我想要做的是制作一个pig!修改原始字符串的方法.如何在不修改自我的情况下做到这一点,因为这是不允许的......?
我有一些代码来计算数字的第n个根.现在,该方法仅适用于Fixnum,因为我在Fixnum类中定义了它.这样做很容易
class Float
#same code as was in Fixnum
end
Run Code Online (Sandbox Code Playgroud)
但这似乎是不必要的.我不知道如何动态调用类.我试过了:
classes = [Fixnum, Float]
classes.each do |x|
x.instance_eval do
def root(pow)
return self ** (1/pow.to_f)
end
end
end
Run Code Online (Sandbox Code Playgroud)
但那没用.我该怎么做呢? 注意:发布后,我意识到这可能更适合Programmers.SE,因为它是理论上的,也是基于单一问题的.随意迁移...
我有一个简单的HTML文件,从服务器获取数据并输出:
<html>
<head>
<script type="text/javascript">
var xmlhttp = new XMLHttpRequest();
function startRequest() {
xmlhttp.onreadystatechange = handleStateChange;
xmlhttp.open("GET", "http://new-host-2.home/test.html", true);
xmlhttp.send(null);
}
function handleStateChange() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert("The server replied with: ", xmlhttp.responseText);
}
else {
alert(xmlhttp.status);
}
}
}
</script>
</head>
<body>
<form>
<input type="button" value="Woooo" onclick="startRequest()"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
服务器上的文件test.html如下所示:
<h1>Data received!</h1>
Run Code Online (Sandbox Code Playgroud)
尽管事实上在控制台中它表示一切正常,并且给出200状态,但我仍然保持0作为状态.当我换if (xmlhttp.status == 200)到时if (xmlhttp.status == 0),它只是输出The server replied with:.为什么是这样?我搞砸了什么?
编辑:它可能只是我的服务器,我将切换到另一个.标题可能会有所帮助:
Response …Run Code Online (Sandbox Code Playgroud) 我使用以下代码在ruby中实现了Greedy算法:
class Greedy
def initialize(unit, total, *coins)
@total_coins1 = 0
@total_coins2 = 0
@unit = unit
@total = total
@reset_total = total
@currency = coins.map
@currency.sort!
@currency = @currency.reverse
unless @currency.include?(1)
@currency.push(1)
end
end
def sorter
@currency.each do |x|
@pos = @total / x
@pos = @pos.floor
@total_coins1 += @pos
@total -= x * @pos
puts "#{@pos}: #{x} #{@unit}"
end
puts "#{@total_coins1} total coins"
end
end
Run Code Online (Sandbox Code Playgroud)
当我尝试运行代码时:
x = Greedy.new("cents", 130, 50, 25, 10, 5)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
NoMethodError: undefined method `sort!' …Run Code Online (Sandbox Code Playgroud) 我有一个我编写的Ruby脚本,它根据文件扩展名在一些混乱的目录中对一些文件进行排序.使用GUI对它进行排序是非常困难的,而且我更容易将文件放在最顶层的目录中并让分拣机完成工作.
问题是,我有点像unix脚本的菜鸟.我想能够做的是能够从我的计算机上的任何地方运行该分拣机脚本,而不必
cd Desktop/Whatever/Foo
ruby sorterscript.rb
Run Code Online (Sandbox Code Playgroud)
只需sortfolders在命令行中编写并运行程序即可.
我已经多次测试过这个脚本了,它运行正常,我只是想要更方便一点.
额外奖励:如果可能的话,并不是太困难,如果我可以自动运行程序,比如每小时,那就更好了.