scope

How to modify variables of another scope?

不打扰是莪最后的温柔 提交于 2019-12-08 04:14:56
问题 Currectly, I'm trying the following: Some mutator function accepts other function's local scope using locals() def mutator(locals): locals['n'] = 10 locals['l'].append(10) def f(): n = 1 l = [1] mutator(locals()) print(n,l) # >>> 1, [1,10] f(None) and that works as expected: I was able to modify mutable list contents but failed to 'rebind' another immutable int to the same name. Currently, I see some options: eval() , exec() & Co Play with stack frames & local variables in mutator() Keep such

Scope: Using Variable Defined in a Function

核能气质少年 提交于 2019-12-08 04:13:03
问题 I have a function in a functions.php file that defines certain variables: add_action( 'the_post', 'paginate_slide' ); function paginate_slide( $post ) { global $pages, $multipage, $numpages; if( is_single() && get_post_type() == 'post' ) { $multipage = 1; $id = get_the_ID(); $custom = array(); $pages = array(); $i = 1; foreach( get_post_custom_keys() as $key ) if ( false !== strpos( $key, 'slide' ) ) $custom[$key] = get_post_meta( $id, $key, true); while( isset( $custom["slide{$i}-title"] ) )

Rails 3 Scope - Find users not in a specific group

元气小坏坏 提交于 2019-12-08 03:52:41
问题 Here is my problem (a bit simplified) : I've got the following models : class User has_many :group_users has_many :groups, through: :group_users class Group has_many :group_users has_many :users, through: :group_users class GroupUser belongs_to :group belongs_to :user scope :belonging_to_group ->(group) {where(group_id = group.id)} I would like to scope users that are not in one specific group, let's say veggies for examples, something that would begin like this : scope :not_in_group, ->

How to pass Array/Values to a Javascript function

依然范特西╮ 提交于 2019-12-08 03:52:39
问题 I am learning Javascript for a project using online resources however i don't know how to get this function working. var results =[[a1,a2,a3,a4,a5]]; var winner = 0; function checkWinner (results) { for (var i = 0; i < results.length; i++) if (results[0][i] > 50) { winner = results[0][i]; } } Just after the function, i use: checkWinner(results); In a HTML file i use alert to display the variable winner. But it obviously doesn't work. I realise it is a problem with my understanding of scope

Ways to avoid that for-loop variables cut into Python's global namespace

杀马特。学长 韩版系。学妹 提交于 2019-12-08 03:43:26
I am wondering if there is a way to avoid that for-loop variables cut into Python's global namespace? The only solution I could come up with so far would be using closures, such as list comprehensions E.g., for the following code: i = 1 print([i for i in range(5)]) print(i, 'i in global') j = 1 for j in range(5): if j == 4: print(j, 'j in for-loop') print(j, 'j in global') prints [0, 1, 2, 3, 4] 1 i in global 4 j in for-loop 4 j in global EDIT: I assume that the for-loop namespace falls into the categoy "Global", unlike a function ("Local") in the LEGB rule, and thus there might be no solution

TclOO Variable Scope with Inheritance/superclass

纵然是瞬间 提交于 2019-12-08 03:09:39
问题 I stumbled over the variable scope when inheriting a class with TclOO. The member variable nCrumbs below is not visible to the inherited class without repeating the declaration. Is there a way to avoid replicating all the variable declarations from the superclass? (I read through all the OO documentation, specifically oo::define and oo::object, also the non-exported stuff, googled for it. There are so many concepts to get around various things, I got lost. I am looking for something that

Refactoring has_many with scopes

天涯浪子 提交于 2019-12-08 03:06:36
问题 I'm a newbie and I just showed my code to an expert, that told me I shouldn't use has_many to filter my variables, but scopes . I have three models : User, Product and Ownership. So here is my code in app/models/user.rb : class User has_many :ownerships, foreign_key: "offerer_id", dependent: :destroy has_many :owned_products, through: :ownerships, source: :product has_many :future_ownerships, -> { where owning_date: nil, giving_date: nil }, class_name: "Ownership", foreign_key: "offerer_id"

Scope: Using Variable Defined in a Function

六眼飞鱼酱① 提交于 2019-12-08 02:57:27
I have a function in a functions.php file that defines certain variables: add_action( 'the_post', 'paginate_slide' ); function paginate_slide( $post ) { global $pages, $multipage, $numpages; if( is_single() && get_post_type() == 'post' ) { $multipage = 1; $id = get_the_ID(); $custom = array(); $pages = array(); $i = 1; foreach( get_post_custom_keys() as $key ) if ( false !== strpos( $key, 'slide' ) ) $custom[$key] = get_post_meta( $id, $key, true); while( isset( $custom["slide{$i}-title"] ) ) { $page = ''; $tzTitle = $custom["slide{$i}-title"]; $tzImage = $custom["slide{$i}-image"]; $tzDesc =

Can rails scopes filter on the number of associated classes for a given field

我与影子孤独终老i 提交于 2019-12-08 01:55:02
问题 I am using Rails 3 with Mongoid. I have a Folder class that can then be shared with other User classes as such class Folder has_one :owner has_many :users I am trying to create two scopes one that can be used to return all private folders, and one to return all shared folders. Is there any way to count the number of associations within a scope? scope :personal, where(:users.count => 0) #Erroring on count... scope :shared, where(:users.count.gt => 0) #Erroring on count... I've considered

JQuery $(this) not accessible after ajax POST?

岁酱吖の 提交于 2019-12-08 01:42:54
问题 Let's say I have a bunch of links that share a click event: <a href="#" class="do-stuff">Click me</a> <a href="#" class="do-stuff">Click me</a> <a href="#" class="do-stuff">Click me</a> <a href="#" class="do-stuff">Click me</a> and in the $('.do-stuff').click function I execute a JQuery ajax POST request that updates the database with stuff and I get a successful response. After the ajax is completed, I simply want to change the value of the link text to be whatever I send back from the