我是红宝石的新手.我试着做一个简单的方法(带参数)调用.
class MeowEncoder
def method(c)
puts c
end
end
print "please enter the thing you want"
s = gets.chomp()
MeowEncoder.method(s)
Run Code Online (Sandbox Code Playgroud)
它只传递参数并将其打印出来.但终端一直给我错误
:MeowEncoder.rb:9: undefined method `toBinary' for MeowEncoder:Class (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
我做了一些改进.
class MeowEncoder
def encode(n)
toBianry(?n)
puts ""
end
def toBinary(n)
if n < 2
print n
else
toBinary(n / 2)
print n % 2
end
end
end
o = MeowEncoder.new
print "please enter the thing you want: "
s = gets.chomp()
s.each_char{|c| o.encode(c)} #this doesn't work
o.toBinary(212) # this works …
Run Code Online (Sandbox Code Playgroud) 我只是尝试使用ruby从project-euler解决问题#45.我知道这个方法.但我写了代码,它没有运行.我是ruby的新手,我不知道为什么后面会有这么多"结束"关键字(否则终端会抱怨)
这是代码:
class Test
def initialize()
end
def Triangle(n)
if 1 + 8*n < 0 then
return false
else
i1 = 0.5 * (-1 + Math.sqrt(1 + 8*n))
i2 = 0.5 * (-1 - Math.sqrt(1 + 8*n))
cut_i1 = i1.to_i
cut_i2 = i2.to_i
if (cut_i1 == i1) & (i1 > 0)
return true
else if (cut_i2 == i1) & (i2 > 0)
return true
else
return false
end
end
end
def Pentagonal(n)
delta = 1 + 24*n
if delta < …
Run Code Online (Sandbox Code Playgroud) 我在PHP编码,这个奇怪的错误弹出.
if(isset($_GET["failauth"])){ ?>
<div>Sorry, your user name or password is not correct. Please re-enter. </div>
<form id="loginform" action="login.php" method="post">
<?php }
else if(isset($_GET("usernotexist"))){ ?>
<div>Sorry, the username is not found</div>
<form id="signupform" action="login.php?signup=true" method="get">
<?php }
else { ?>
<form id="loginform" action="login.php" method="post">
<?php } ?>
<div><input name="name" type="text" size="8" autofocus="autofocus" pattern="/^.{3,8}$/" /> <strong>User Name</strong></div>
<div><input name="password" type="password" size="8" pattern="/^[0-9].{4,10}[^a-zA-Z0-9]$/" /> <strong>Password</strong></div>
<div><input type="submit" value="Log in" /></div>
</form>
Run Code Online (Sandbox Code Playgroud)
第36行引用"else if"语句.我很好奇为什么它会超过第一个,但如果没有通过其他部分?它是否与语法原因有关因为我之前从未使用过if语句...