variable-assignment

Why does single `=` work in `if` statement?

一笑奈何 提交于 2019-12-01 16:04:03
问题 This code is provided as an example in for use with devise and OmniAuth, it works in my project. class User < ActiveRecord::Base def self.new_with_session(params, session) super.tap do |user| if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] user.email = data["email"] if user.email.blank? end end end end I don't know why it's a single equals sign as apposed to a double equals sign, which I thought was necessary for if -statements. My IDE

Easy assignments with empty square brackets? x[]<-

我与影子孤独终老i 提交于 2019-12-01 15:12:11
While looking at an answer posted recently on SO, I noticed an unfamiliar assignment statement. Instead of the usual form of myVar<- myValue , it used the for myVar[]<- myValue . Personally, I had never seen such an assignment, but it had a highly useful effect-- it reshaped the assigned data myValue to the shape of myVar. I would like to use this in my code. However the documentation for "<-" seems to be silent on it. Is this a well established feature and one can rely on it to work in all cases? Also, my guess is that it might be a side effect of a function call stack, i.e. calling <- and [

Multiple assignment on one line not working as expected

点点圈 提交于 2019-12-01 14:58:52
I'm trying to swap two int s - x and y in the example, and do it in one line without a library function. So I started with this: int x = 4; int y = 3; System.out.println(x); System.out.println(y); x ^= y; System.out.println(x); System.out.println(y); y ^= x; System.out.println(x); System.out.println(y); x ^= y; System.out.println(x); System.out.println(y); The output was 4, 3, 7, 3, 7, 4, 3, 4 as expected. All good so far. Next up was this: int x = 4; int y = 3; System.out.println(x); System.out.println(y); y ^= (x ^= y); System.out.println(x); System.out.println(y); x ^= y; System.out.println

PHP “Assign by reference” oddity

谁都会走 提交于 2019-12-01 14:50:00
I came across a code snippet which included $a = & $b; but hadn't tested whether $b actually existed ( if (isset($b)) ). I wasn't sure how PHP handled this so I knocked up a quick bare test and now I'm even more intrigued. $a = array('a'=>'b', 'x'=>'y'); $b = array(); $b[10] = &$a['a']; $b[11] = &$a['ppp']; var_dump($a); var_dump($b); echo (isset($a['ppp']) ? "SET" :" NOT SET") . "\n"; echo (isset($b[11]) ? "SET" :" NOT SET") . "\n"; It's bare code but what the output shows is: Just the bare assignment of $b[11] = &$a['ppp'] is enough, var_dump($a) is reported as having 3 members not 2, even

Multiple assignment on one line not working as expected

会有一股神秘感。 提交于 2019-12-01 13:46:14
问题 I'm trying to swap two int s - x and y in the example, and do it in one line without a library function. So I started with this: int x = 4; int y = 3; System.out.println(x); System.out.println(y); x ^= y; System.out.println(x); System.out.println(y); y ^= x; System.out.println(x); System.out.println(y); x ^= y; System.out.println(x); System.out.println(y); The output was 4, 3, 7, 3, 7, 4, 3, 4 as expected. All good so far. Next up was this: int x = 4; int y = 3; System.out.println(x); System

UnBoundLocalError: local variable referenced before assignment (Python)

浪尽此生 提交于 2019-12-01 12:58:37
I'm trying to create a function servo_to_quadrant that returns the value servo_quadrant . Questions similar to this one have involved there being an issue with a global variable outside of the function. I don't think that's the issue in this case, as the variable is only needed from within the function (although I could be wrong). Code: def servo_to_quadrant(servo_val): if servo_val < 0: 360 + servo_val if servo_val >= 360: servo_val = servo_val - 360 if servo_val >= 0 and servo_val < 90: servo_quadrant = 1 if servo_val >= 90 and servo_val < 180: servo_quadrant = 2 if servo_val >= 180 and

How can I evaluate variable to another variable before assigning? [closed]

随声附和 提交于 2019-12-01 12:41:33
This question broken into subquestions : Pointers in Python suggested by one reply to look at, more here "why not to modify locals?" -question here. Original Question #!/usr/bin/python # # Description: trying to evaluate array -value to variable before assignment # but it overwrites the variable # # How can I evaluate before assigning on the line 16? #Initialization, dummy code? x=0 y=0 variables = [x, y] data = ['2,3,4', '5,5,6'] # variables[0] should be evaluted to `x` here, i.e. x = data[0], how? variables[0] = data[0] if ( variables[0] != x ): print("It does not work, why?"); else: print(

using a foreach loop to initialize variables

爷,独闯天下 提交于 2019-12-01 12:22:55
I have built an empty associative array whos keynames refer to submitted post data. I can capture the postdata just fine, but I run into trouble trying to instantiate variables who's names match the array key. for example: $insArray = array('rUsername'=>'', 'rPass'=>'', 'rQuestion'=>'', 'rAnswer'=>'', 'rFName'=>'', 'rLName'=>'', 'rBDateD'=>'', 'rBDateM'=>'', 'rBDateY'=>'', 'rHCheck'=>'', 'rHCeckOption'=>'', 'rEmail'=>''); foreach($insArray as $key=>$value){ if (filter_input(INPUT_POST, $key) != ''){ $key = stripslashes(filter_input(INPUT_POST, $key)); $insArray[$key] = $key; } } First line

Invalid assignment left-hand side, javascript

ぐ巨炮叔叔 提交于 2019-12-01 10:53:49
I am sure I am doing something silly here: var addhtml = '<div id="leftbio" class="left-float">' += '<div id="bioname">e["screen_name]</div>' += '<div id="biophoto"><img src="e["profile_image_url"]"/></div>' += '<div id="biodetails">e["description"]</div>' += '</div>'; // invalid assignment left-hand side console.log(addhtml); And Netbeans is telling me that invalid assignment left-hand side error. Whats wrong ? You don't need += to concatenate, you just need + This is ok var addhtml = '<div id="leftbio" class="left-float">' + '<div id="bioname">e["screen_name]</div>' + '<div id="biophoto">

Python list extension and variable assignment

丶灬走出姿态 提交于 2019-12-01 10:52:48
I tried to extend a list and was puzzled by having the result return with the value None. What I tried was this: >>> a = [1,2] >>> b = [3,4] >>> a = a.extend(b) >>> print a None I finally realized that the problem was the redundant assignment to 'a' at the end. So this works: >>> a = [1,2] >>> b = [3,4] >>> a.extend(b) >>> print a [1,2,3,4] What I don't understand is why the first version didn't work. The assignment to 'a' was redundant, but why did it break the operation? Because, as you noticed, the return value of extend is None . This is common in the Python standard library; destructive