我想知道如何在另一个类的类实例中调用方法.
这就是我提出的
class ClassA
def method
return "This is a method_from_class_A"
end
end
class ClassB
def initialize
@method_from_class_A=instance.method
end
def method_calls_method
@method_from_class_A
end
end
instance=ClassA.new
instance2=ClassB.new
puts instance2.method_calls_method
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
Testing.rb:9:
initialize': undefined local variable or method实例'来自Testing.rb的#(NameError):19:innew' from Testing.rb:19:in'
我该怎么办呢?
感谢您的答复.
我试图找到一种方法来获取最旧的 created_at 值以获取类中最旧的对象,因此我可以稍后对其进行操作。
我试图将所有项目添加到数组中,然后获取最旧的值,然后使用 .where 活动记录方法将其转换回来以获取在该日期创建的对象。
这是我到目前为止:
date_array=[]
buy_requests.each do |buy_request|
date_array << buy_request.created_at
end
date_array.min
Run Code Online (Sandbox Code Playgroud)
但我觉得有更简单的方法可以做到这一点。
我遇到的一个问题是,当我将日期添加到数组时,我不再拥有它的创建时间,因此一旦我使用 .min 获得最旧的日期,我就不能再使用 .where。
谢谢你。如果我不够清楚,请告诉我。
我在添加很多HTML时遇到了麻烦.
这就是我所拥有的:
$("#popup1").click(function(){
$(".cd-popup-container").append("<p>Are you sure you want to decline this employement request?</p>");
$(".cd-popup-container").append("<form id='accept_employe' action='/accept_employe' method='post' accept-charset='utf-8'>");
$(".cd-popup-container").append("<ul class='cd-buttons no_margin'>");
$(".cd-popup-container").append("<li><a class='submit'>Yes</a></li>");
$(".cd-popup-container").append("<li><a class='popup-close'>No</a></li>");
$(".cd-popup-container").append("</ul>");
$(".cd-popup-container").append("</form>");
$(".cd-popup-container").append("<a class=cd-popup-close popup-close img-replace>Close</a>");
});
Run Code Online (Sandbox Code Playgroud)
很明显很多附加物都不会起作用,因为它只能得到第一个.但是,当我把它全部放在同一条线上时,它也不起作用.
如何清楚地将所有这些html附加到.cd-popup-container中?
如何将帖子参数添加到我现在拥有的内容中:
@toSend = {
"nonce" => Time.now.to_i,
"command" => "returnCompleteBalances"
}.to_json
uri = URI.parse("https://poloniex.com/tradingApi")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
req.set_form_data({"nonce" => Time.now.to_i, "command" => "returnCompleteBalances"})
req['Key'] = '******-N4WZI2OG-******-10RX5JYR'
req['Sign'] = 'secret_key'
req.body = "[ #{@toSend} ]"
res = https.request(req)
puts "Response #{res.code} #{res.message}: #{res.body}"
Run Code Online (Sandbox Code Playgroud)
这些是我要发送的参数:
"nonce" => Time.now.to_i,
"command" => "returnCompleteBalances"
Run Code Online (Sandbox Code Playgroud)
谢谢你。
我想这样做,以便我在同一行和相同的可点击链接上拥有注销图标和注销链接。
如果我这样做:
<li>
<span><i class="fa fa-sign-out"></i> <%= link_to "Sign Out", destroy_user_session_path, method: :delete %></span>
</li>
Run Code Online (Sandbox Code Playgroud)
我在单独的行上看到图标,但无法点击该图标以访问链接。
这就是我为登录所做的,因为我有一个 get /login 页面:
<li>
<a href="/login">
<i class="fa fa-user"></i>
<span>Edit Account</span>
</a>
</li>
Run Code Online (Sandbox Code Playgroud)
我正在使用设计登录和注销。我怎么能得到它,以便图标和文本以及可点击和在同一行上的注销链接,因为注销链接是删除而不是获取。
我目前已将其设置为根目录是"信息登陆页面",其中包含指向"/ login"和"/ signup"的登录和注册按钮.如果用户未登录并尝试加载需要识别的页面,我已将其设置为已重定向到root.我想这样做,如果用户已经登录,它会重定向到"/ home".
我的问题是,如果用户已经登录,如何在加载根页面时将其重定向到/ home.
非常感谢你.
我正在寻找创建一个程序,将整数转换为二进制,反之亦然,但我不完全知道如何做到这一点或如何解决这个问题。
这是我到目前为止:
#Method that check if string is a number.
def is_number?(string)
true if Float(string) rescue false
end
#Method to convert decimal to binary
def decimal_to_binary(number)
if is_number?(number)==false
return "This method only accepts positive integers."
elsif number<=0
return "This method only accepts positive integers."
else
return #HERE it needs to converts decimal to binary
end
end
#Method to convert binary to decimal
def binary_to_decimal(number)
end
Run Code Online (Sandbox Code Playgroud)
如果您不知道/记住,以下是对二进制文件的一些解释:
你数 0、1,然后你必须从零开始并添加一列!下一列的价值是第一列的两倍。由于二进制是基数为 2 的系统,因此每个数字代表 2 的幂,最右边的数字代表 20 (0),下一个代表 21 (2),然后是 22 (4)、23 (8),依此类推。
ruby ×3
devise ×2
activerecord ×1
class ×1
datetime ×1
http ×1
javascript ×1
jquery ×1
link-to ×1
methods ×1
parameters ×1
post ×1
redirect ×1
undefined ×1
uri ×1