methods

Any good reason for Ruby to have == AND eql? ? (similarly with to_a and to_ary)

北慕城南 提交于 2019-12-07 15:39:36
问题 I know eql? is used by Hashes to see if an object matches a key * , and you do def ==(rb) if you want to support the == operator, but there must be a good reason that Hashes don't use == instead. Why is that? When are you going to have definitions for == and eql? that are not equivalent (e.g. one is an alias to the other) ? Similarly, why have to_ary in addition to to_a? This question came up in response to an answer someone gave me on another question. * Of course, a Hash also assumes eql? =

Generics: Make sure parameters are of same type

我们两清 提交于 2019-12-07 14:41:03
问题 I've got the following method: protected <S> void setValue(final S oldValue, final S newValue) { // Do something } I want to make sure, that both parameters are of the same type. It would be cool, if there'd be a compiler error when you try to pass parameters of two different types. The above way is clearly not the correct one. I can put into a String and an Integer , since the both extend from Object . Is my want even possible? Or is the only way to make sure both parameters are of the same

Java - Subclass calls supers constructor which calls subclass method instead of its own

人盡茶涼 提交于 2019-12-07 14:40:38
问题 I'll start with a code example: class A { public A() { f(); //When accessed through super() call this does not call A.f() as I had expected. } public void f() {} //I expect this to be called from the constructor. } class B extends A { private Object o; public B() { super(); o = new Object(); //Note, created after super() call. } @Override public void f() { //Anything that access o. o.hashCode(); //Throws NullPointerException. super.f(); } } public class Init { public static void main(String[]

Ruby on Rails: Calling an instance method from another model

社会主义新天地 提交于 2019-12-07 14:13:50
问题 I've got a Match model and a Team model. I want to run an instance method (written inside the Team model) after a Match has been saved. Here's what I've got. team.rb def goals_sum unless goal_count_cache goal_count = a_goals_sum + b_goals_sum update_attribute(:goal_count_cache, goal_count) end goal_count_cache end and it works. Now I need to run this whenever a match gets saved. So I tried this: match.rb after_save :Team.goals_sum after_destroy :Team.goals_sum And it doesn't work. I know I'm

ASP.NET lock thread method

家住魔仙堡 提交于 2019-12-07 13:15:45
问题 I'm developing an ASP.NET forms webapplication using C#. I have a method which creates a new Order for a customer. It looks similar to this; private string CreateOrder(string userName) { // Fetch current order Order order = FetchOrder(userName); if (order.OrderId == 0) { // Has no order yet, create a new one order.OrderNumber = Utility.GenerateOrderNumber(); order.Save(); } return order; } The problem here is, it is possible that 1 customer in two requests (threads) could cause this method to

Sinatra Request Object

别来无恙 提交于 2019-12-07 13:13:35
问题 I'm probably missing something painfully obvious here, but I can't seem to find an answer, or work it out myself. In Sinatra, they have a self.get method, which captures blocks, when a block is called, you're able to use the request variable inside, how is this possible? Sinatra module Sinatra class Base class Request < Rack::Request end attr_accessor :request def call!(env) @request = Request.new(env) end class << self def get(path, opts = {}, &block) ... end end end end App class App <

Getting a TypeError when method returns string

偶尔善良 提交于 2019-12-07 13:11:29
问题 What's is wrong with the code? Python returns TypeError when method returns class string. class window: def __init__(self, title='window'): self.title = title def title(self, title): if title: self.title = title else: return self.title window = window() window.title('changed') print(window.title()) Error: Traceback (most recent call last): File "C:/Users/Danilo/Desktop/pygtk.py", line 10, in <module> window.title('changed') TypeError: 'str' object is not callable 回答1: Methods are attributes

java override method invocation

耗尽温柔 提交于 2019-12-07 12:24:30
问题 I have a super class: public class SuperClass { public void dosomething() { firstMethod(); secondMethod(); } public void firstMethod() { System.out.println("Super first method"); } public void secondMethod() { System.out.println("Super second method"); } } A sub class: public class SubClass extends SuperClass { public void dosomething() { super.dosomething(); } public void firstMethod() { System.out.println("Sub first method"); } public void secondMethod() { System.out.println("Sub second

How to use operator= with anonymous objects in C++?

我只是一个虾纸丫 提交于 2019-12-07 11:25:56
问题 I have a class with an overloaded operator: IPAddress& IPAddress::operator=(IPAddress &other) { if (this != &other) { delete data; this->init(other.getVersion()); other.toArray(this->data); } return *this; } When I try to compile this: IPAddress x; x = IPAddress(IPV4, "192.168.2.10"); I get the following error: main.cc: In function ‘int main()’: main.cc:43:39: error: no match for ‘operator=’ in ‘x = IPAddress(4, ((const std::string&)(& std::basic_string<char>(((const char*)"192.168.2.10"), (

Find first N pentagonal numbers

五迷三道 提交于 2019-12-07 09:34:00
问题 I have to find the first N [pentagonal numbers][1] from 1-100 and display them 10 per line. I have to use the getPentagonalNumber(int n) method as well; that is obviously why it is there. Here is my code so far. package chapter_5; public class Five_One { public static void main(String[] args) { int n = 0; int numPerLine = 10; for ( n = 0; n < 11; n ++) } public static int getPentagonalNumber(int n) { int formula = n * (3 * n - 1) / 2; while ( formula < ) } } 回答1: This has a long way to go.