ruby方法中的变量赋值

时间:2016-05-09 19:45:51

标签: ruby

我有这个非常简单的方法:

def update_context(msg, session, sender)
    previous_context = session.context 
    session.update(context: intent_determination(msg, session.context, sender))
    session.update(context: brand_determination(msg, session.context))
    session.update(context: style_determination(msg, session.context))
    session.update(context: price_range_determination(msg, session.context))
    session.update(context: size_determination(msg, session.context))
       p previous_context
       p session.context
       p (previous_context == session.context)
    unless session.context.size == 0
      if previous_context == session.context
        session.context["intent"] = "lost"
        session.save
      end
    end
  end

我的问题当然是由于我无法看到的一个愚蠢的错误,但请耐心等待我,我真的无法看到它。

如您所见,我"保存"方法开头的previous_context变量中的会话上下文。然后,我在上下文中运行了一些更新。 但是,当我打印previous_contextsession.contextprevious_context == session.context时,我得到前两个相同的结果,而最后一个得到的结果是真的。

这怎么可能?在更新之前,我已将previous_context的值指定为session.context。然后,previous_context与我更新后的session.context具有相同的价值。

我真的无法看到我在这里搞砸了,或者肯定有些东西我不明白。

2 个答案:

答案 0 :(得分:1)

BasicGame.Game = function (game) { // When a State is added to Phaser it automatically has the following properties set on it, even if they already exist: this.game; // a reference to the currently running game (Phaser.Game) this.add; // used to add sprites, text, groups, etc (Phaser.GameObjectFactory) this.camera; // a reference to the game camera (Phaser.Camera) this.cache; // the game cache (Phaser.Cache) this.input; // the global input manager. You can access this.input.keyboard, this.input.mouse, as well from it. (Phaser.Input) this.load; // for preloading assets (Phaser.Loader) this.math; // lots of useful common math operations (Phaser.Math) this.sound; // the sound manager - add a sound, play one, set-up markers, etc (Phaser.SoundManager) this.stage; // the game stage (Phaser.Stage) this.time; // the clock (Phaser.Time) this.tweens; // the tween manager (Phaser.TweenManager) this.state; // the state manager (Phaser.StateManager) this.world; // the game world (Phaser.World) this.particles; // the particle manager (Phaser.Particles) this.physics; // the physics manager (Phaser.Physics) this.rnd; // the repeatable random number generator (Phaser.RandomDataGenerator) // You can use any of these from any function within this State. // But do consider them as being 'reserved words', i.e. don't create a property for your own game called "world" or you'll over-write the world reference. this.bulletTimer = 0; }; BasicGame.Game.prototype = { create: function () { //Enable physics // Set the physics system this.game.physics.startSystem(Phaser.Physics.ARCADE); //End of physics // Honestly, just about anything could go here. It's YOUR game after all. Eat your heart out! this.createBullets(); this.createTanque(); this.timerBloques = this.time.events.loop(1500, this.createOnebloque, this); }, update: function () { if(this.game.input.activePointer.isDown){ this.fireBullet(); } this.game.physics.arcade.overlap(this.bullets, this.bloque, this.collisionBulletBloque, null, this); }, createBullets: function() { this.bullets = this.game.add.group(); this.bullets.enableBody = true; this.bullets.physicsBodyType = Phaser.Physics.ARCADE; this.bullets.createMultiple(100, 'bulletSprite'); this.bullets.setAll('anchor.x', 0.5); this.bullets.setAll('anchor.y', 1); this.bullets.setAll('outOfBoundsKill', true); this.bullets.setAll('checkWorldBounds', true); }, fireBullet: function(){ if (this.bulletTimer < this.game.time.time) { this.bulletTimer = this.game.time.time + 1400; this.bullet = this.bullets.getFirstExists(false); if (this.bullet) { this.bullet.reset(this.tanque.x, this.tanque.y - 20); this.game.physics.arcade.enable(this.bullet); this.bullet.enableBody = true; this.bullet.body.velocity.y = -800; } } }, createOnebloque: function(){ this.bloquecs = ["bloqueSprite","bloquelSprite"]; this.bloquesr = this.bloquecs[Math.floor(Math.random()*2)]; this.bloque = this.add.sprite(0, 360, this.bloquesr); this.game.physics.arcade.enable(this.bloque); this.bloque.enableBody = true; this.bloque.body.velocity.x = 300; this.bloque.body.kinematic = true; this.bloque.checkWorldBounds = true; this.bloque.outOfBoundsKill = true; this.bloque.body.immovable = true; }, createTanque: function() { this.tanqueBounds = new Phaser.Rectangle(0, 600, 1024, 150); this.tanque = this.add.sprite(500, 700, 'tanqueSprite'); this.tanque.inputEnabled = true; this.tanque.input.enableDrag(true); this.tanque.anchor.setTo(0.5,0.5); this.tanque.input.boundsRect = this.tanqueBounds; }, collisionBulletBloque: function(bullet, bloque) { this.bullet.kill(); }, quitGame: function (pointer) { // Here you should destroy anything you no longer need. // Stop music, delete sprites, purge caches, free resources, all that good stuff. // Then let's go back to the main menu. this.state.start('MainMenu'); } }; 使previous_context变量指向与session.context相同的对象。如果你想更改一个而不影响另一个,你需要创建一个session.context的副本来存储在previous_context中。

答案 1 :(得分:0)

在Ruby中,变量只是对象的引用,所以你在那里做的只是创建对同一对象的新引用。如果要保存以前的状态,则必须复制整个对象。

有关更详细的说明,请参阅此answer