jeudi 28 janvier 2016

Rails / simplifying syntax when comparing nil or not elements

having the following condition, where invitation_sent_at can be nil :

if self.invitation_last_sent_at + 1.hour
    #then do something
end

I end up writing the following :

if (self.invitation_sent_at.presence && (self.invitation_sent_at + 1.hour) ...

I'm wondering if there is a cleaner syntax to handle the fact that self.invitation_sent_at could be nil, but without having to verify it so verbosely if it is present

Deleting an Array of rows and columns vba?

I've got some reports which have sub-totals littered inbetween them. I need to delete the sub-totals, which is easy enough to condition on (as is in my IF statement).

However, my following code just does not delete the entire row of subtotals.

Sub clean_up()
'declare variables
Dim i As Integer
Dim noRows As Integer

'count rows
noRows = Range("B2:B387").Rows.Count

'delete row entries that are duplicates..
For i = 1 To noRows
    If Range("B2").Cells(i + 1, 1) = Range("B2").Cells(i, 1) Then
    Range("B2:G2").Cells(i + 1).Clear


    End If
Next i

End Sub

I do not understand why the Range("B2:G2").Cells(i + 1).Clear of my code is incorrect. Using Range("B2").Cells(i + 1).Clear works to delete the first entry in the row, so shouldn't Range("B2:G2").Cells(i + 1).Clear delete the entire row?

Any help is very much appreciated.

If/Else curl command not working

I am using a grep -c to count the occurrence of a phrase on a curl. Currently the code below return a number 12.

curl WEBSITEURL | grep -c "incident-title"

I want to use this in a single line if/else bash command that says that if the occurrence is greater than 0 then printf or else printf.

if curl WEBSITEURL | grep -c "incident-title" > 0; then printf "Investigating Issue"; else printf "Fully Operational"; fi

It keeps returning "Fully Operational" even though that it should be true as 12 is greater than 0.

Thank you in advance for your assistance.

Can someone help wiht CREATE PROCEDURE

 Table 1
    mID         date             room
    m1          2015.01.01        122
    m2          2016.01.01        151
    m3          2016.07.07        454

 Table 2
    mID       mName
    m1        network
    m2        ood
    m3        java



enter code here

CREATE PROCEDURE that gives a new agenda for a module. I need it to accept the ID and date(1 month in advance) otherwise show error, and then show consequative weeks (e.g next 5 weeks)

I have to use a CURSOR, LOOP and SIGNAL and if (if needed)

Intelligent VBA code from exhausting Excel formula

I have a working formula that searches one sheet for a sub-string, and if it finds that sub-string, returns a mid function, if not, it moves to the next cell down and searches again. However, the formula I created is obviously not the right way to do this, as it is hard coded, and cannot compensate for more entries without my addition of yet another if statement. I cannot change these to relative cell references, as many of the cells should return the same value (dates of an event, with multiple item #'s occurring on the same date), which is why the routine needs to run through each and every record (currently A1-A6, but will grow dynamically as reports come in) until it finds an exact match.

I would like to convert this formula to a "smart" VBA subroutine that just looks in the next cell down if the sub-string was not found in the first cell searched. This would allow it to be dynamic and run through however many entries I have while returning the correct result.

Here is my current excel formula:

=IF(ISNUMBER(SEARCH(B3,'\\Vae1vwinfhomp1\home\1625\I58339\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$1))=TRUE,IFERROR(MID('H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$1,FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$1),(FIND("DATE OF LOSS",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$1)-(FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$1)+5))),""),IF(ISNUMBER(SEARCH(B3,'\\Vae1vwinfhomp1\home\1625\I58339\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$2))=TRUE,IFERROR(MID('H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$2,FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$2),(FIND("DATE OF LOSS",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$2)-(FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$2)+5))),""),IF(ISNUMBER(SEARCH(B3,'\\Vae1vwinfhomp1\home\1625\I58339\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$3))=TRUE,IFERROR(MID('H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$3,FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$3),(FIND("DATE OF LOSS",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$3)-(FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$3)+5))),""),IF(ISNUMBER(SEARCH(B3,'\\Vae1vwinfhomp1\home\1625\I58339\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$4))=TRUE,IFERROR(MID('H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$4,FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A4),(FIND("DATE OF LOSS",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$4)-(FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$4)+5))),""),IF(ISNUMBER(SEARCH(B3,'\\Vae1vwinfhomp1\home\1625\I58339\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$5))=TRUE,IFERROR(MID('H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$5,FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$5),(FIND("DATE OF LOSS",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$5)-(FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$5)+5))),""),IF(ISNUMBER(SEARCH(B3,'\\Vae1vwinfhomp1\home\1625\I58339\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$6))=TRUE,IFERROR(MID('H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$6,FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$6),(FIND("DATE OF LOSS",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$6)-(FIND("RISK CLAIM",'H:\Document\[Risk_Excel_Export.xlsx]Sheet1'!$A$6)+5))),""),0))))))

Here is a sample of the formula working correctly, returning the correct RISK CLAIM # for each Item ID (in the first row that would be 10045597). As you can see, many Item ID's will return the same RISK CLAIM #, as they are linked to the same claim #.

10045597 SERIAL/VIN #: 1984 MAKE: KENT MODEL: KF 4 SS YR: 2012          TYPE OF EQUIPMENT: SKID STEER/MINI EXCAVATOR BREAKER ORIGINAL EQUIPMENT COST: 3832.71 RISK CLAIM #:  RPP3535TF

10251995 SERIAL/VIN #: 177734255 MAKE: STIHL MODEL: TS420-14 YR:  2015         TYPE OF EQUIPMENT: CUT OFF SAW ORIGINAL EQUIPMENT COST: 730.00 RISK CLAIM #:   RPP3534TF

10353520 SERIAL/VIN #: 007379 MAKE: DEWALT MODEL: D25980K YR:  2015         TYPE OF EQUIPMENT: DEMO HAMMER ORIGINAL EQUIPMENT COST: 1118.78 RISK CLAIM #:   RPP3534TF

10326567 SERIAL/VIN #: 71248 MAKE: HILTI MODEL: TE60-ATC YR:  2015         TYPE OF EQUIPMENT: ROTARY HAMMER ORIGINAL EQUIPMENT COST: 1115.49 RISK CLAIM #:   RPP3534TF

10335480 SERIAL/VIN #: 179146608 MAKE: STIHL MODEL: TS420 YR:  2015         TYPE OF EQUIPMENT: CUT OFF SAW ORIGINAL EQUIPMENT COST: 824.96 RISK CLAIM #:   RPP3534TF

10331620 SERIAL/VIN #: 006159 MAKE: DEWALT MODEL: D25980K YR:  2014         TYPE OF EQUIPMENT: DEMO HAMMER ORIGINAL EQUIPMENT COST: 1117.42 RISK CLAIM #:   RPP3534TF

10189822 SERIAL/VIN #: 7305316 MAKE: MULTIQUIP MODEL: DCA70SSJU4I YR:  2013         TYPE OF EQUIPMENT: GENERATOR ORIGINAL EQUIPMENT COST: 33068.65 RISK CLAIM #:   RPP3520T

1226605 SERIAL/VIN #: 5653875 MAKE: MULTIQUIP MODEL: GAW180HE1 YR: 2011          TYPE OF EQUIPMENT: WELDER ORIGINAL EQUIPMENT COST: 2442.03 RISK CLAIM #:   RPP3491T

1219041 SERIAL/VIN #: 20036780 MAKE: WACKER MODEL: BS 60-2I YR: 2011          TYPE OF EQUIPMENT: RAMMER ORIGINAL EQUIPMENT COST: 2642.09 RISK CLAIM #: RPP3490T 

10391557 SERIAL/VIN #: 30101214 MAKE: WACKER MODEL: WP1550AW YR: 2015          TYPE OF EQUIPMENT: VIB PLATE ORIGINAL EQUIPMENT COST: 1499.52 RISK CLAIM #:  RPP3439TF

10305672 SERIAL/VIN #: 4KNTT1210FL160572 MAKE: TOW MASTER MODEL: T-5DT YR: 2014          Lic. Plate : MO / 63E0HL RISK CLAIM #:  RPP3439TF

Sample of text that is being searched

UNITED RENTALS – RPP CLAIM – STOLEN EQUIPMENT RISK CLAIM #:  RPP3535TF DATE OF LOSS: 1/15/16 REPORT DATE:   1/19/16 LOCATION CODE: 481 LOSS STATE: BC - CANADA CONTACT PERSON(s):  JUSTIN TETRAULT/ERICH THIESSEN LOCATION PHONE #: 604-792-0034 CLAIM DETAILS (what happened):    FRAUDULENTLY RENTED UNDER ANOTHER CUSTOMERS ACCOUNT AND WAS NEVER RETURNED. LOCATION/ADDRESS WHERE THEFT OCCURRED:  LOC 481 UNITED RENTALS EQUIP#(s) & DESCRIPTION: EQ # - 10045597 YR: 2012 MAKE: KENT MODEL: KF 4 SS SERIAL/VIN #: 1984 TYPE OF EQUIPMENT: SKID STEER/MINI EXCAVATOR BREAKER ORIGINAL EQUIPMENT COST: 3832.71 BULK EQUIPMENT (non-serialized equipment/accessories) STOLEN ALONG WITH THE ABOVE MENTIONED EQUIPMENT #(s)? – Y/N: N BULK TYPE: BULK COST: POLICE AGENCY THEFT REPORTED TO: ABBOTSFORD PD POLICE AGENCY PHONE #: 604-859-5225 POLICE REPORT/CASE NUMBER: 16-2034 This email is intended for the recipient only.  If you are not the intended recipient please disregard, and do not use the information for any purpose

Function return in branches of an `if` vs outside the `if`, in C++

Are there differences in the compiled code between

double func(bool x) {
  if (x) {
    return 1.0;
  } else {
    return 2.0;
  };
}

and

double func(bool x){
  if (x) {
    return 1.0;
  };
  return 2.0;
}

Are there performance reasons to favor one over the other?

The only thing I know is that the second faster to type.


I have been using g++, but would be interesting to learn what happens in others, if it is different.

French Quiz Not Working

I have made a french quiz but it is not working.

Here is the code:

<html>
<head>
<title>Quiz</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Quiz</h1>
<p id="one"></p>
<input id="aone" />
<br>
<a href="#" class="myButton" id="btn">Mark It</a>
<script>
var verbs = ["accompagner","aider","aimer","apporter","chanter","danser","coûter","écouter","étudier","fermer","jouer","laver","marcher","montrer","oublier","passer","penser","préparer","raconter","agir","bâtir","choisir","désobéir","finir","guérir","nourrir","obéir","punir","réfléchir","remplir","réussir","rougir","saisir","attendre","défendre","descendre","entendre","interrompre","perdre","rendre","répondre","rompre","vendre"];
var verb = verbs[Math.floor(Math.random() * verbs.length)];
if(verb.slice(-2) == "er") {
var newverb = verb.substring(0, verb.length - 2);
newverb += "es";
}
if(verb.slice(-2) == "ir") {
var newverb = verb.substring(0, verb.length - 2);
newverb += "is";
}
if(verb.slice(-2) == "re") {
var newverb = verb.substring(0, verb.length - 2);
newverb += "s";
}
document.getElementById("one").innerHTML = verb;
document.getElementById("btn").onclick = function () {
if (document.getElementById("aone").value==newverb) { // Not Working.
alert("Correct");
}
}
</script>
</body>
</html>

I think I have traced the problem to the final if statement not being true but I don't understand why. All help will be appreciated.