Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:59:21+00:00 2026-06-11T15:59:21+00:00

module Foo attr_accessor :val end module Bar def bar val = 50 end end

  • 0
module Foo
  attr_accessor :val
end

module Bar
  def bar
    val = 50
  end
end

class FooBar
  include Foo
  include Bar
end

fb = FooBar.new
puts fb.bar # 50
puts fb.val # nil

I would like to, from within Bar, change Foo’s “val” value.

Just so you know, I’m trying to do this because I want to add user authentication through a Facebook Canvas app in Clearance (0.8.2 (old, I know)). The variable that I want to change is https://github.com/thoughtbot/clearance/blob/b8ccca00d91cb336e044b76b5c6ae1d8de9d2d8d/lib/clearance/authentication.rb#L25.

This module gets included into ApplicationController, and I’m including another module afterwards (FacebookAuthenticationHelper) that does something like

def authenticate_facebook
  current_user ||= facebook_user if coming_from_facebook?
end

I would love to know if there’s a better way to do this as well. I’m not using OAuth, I’m just saving Facebook’s user_id sends to my app from signed_request into my DB.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:59:22+00:00Added an answer on June 11, 2026 at 3:59 pm

    In ruby, any statement of the form varname = value will create a local variable named varname if it doesn’t already exist. This is even true within class methods where the class has a setter method of the same name. Furthermore, if a local variable exists, it takes precedence over getters and setters. For example:

    class Demo
        attr_accessor :foo
    
        def demonstrate!
            @foo = 1      #1: Set member variable foo, so we get meaningful output
            puts foo      #2: Prints 1 (this calls the getter)
            puts self.foo #3: Prints 1 (also calls the getter)
    
            foo = 2       #4: Creates a LOCAL variable named foo with the value 2
            puts foo      #5: Prints 2 (locals take precedence over getters)
            puts self.foo #6: Prints 1 (calls the getter - the member variable hasn't changed)
    
            self.foo = 3  #7: Use SELF to ensure you call a getter or setter
            puts foo      #8: Prints 2 (the local again)
            puts self.foo #9: Prints 3 (the member, which is now 3)
        end
    end
    

    The bad thing about this system is: Look at lines 2 and 5. The same exact code does different things! On line 2, the local variable foo didn’t exist yet, so ruby did the “next best” thing and called the getter. But on line 5, the local foo exists, so it takes precedence.

    I’d say that this is bad language design: If setters can’t be called without self, why should it be okay for getters – especially when it can lead to rickety, context-sensitive code like the above? But it’s what we have to work with, and it leads to some general guidelines:

    1. If you can, use @foo. It’s obvious what that does.
    2. If you’re using a getter or setter, always use self, even when it’s not strictly necessary. This makes it obvious what you’re calling. It also guarantees your code won’t behave differently if you add a local with the same name later.
    3. If you do 1 and 2, you can assume that anything without a @ or a self is a local variable (or a function call, but you can probably tell those apart by name).

    And that ended up being a bit long-winded, but I was unable to find a good demonstration of the getter/setter/local problem to link to. So maybe this is it – hope it’s helpful!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Foo def initialize(a) puts Hello #{a} end end module Bar def initialize(b) puts
My problem basically looks like this: module Foo class Bar def self.who self.class.to_s end
given module Foo def bar puts foobar end end I can do String.extend(Foo) and
module Foo def self.bar Module.bar end end def Foo main.Foo end puts Foo #
Given the following module, module Foo def bar :baz end end def send_to_foo(method) #
module Test def self.model_method puts this is a module method end end class A
Can someone explain to me what this would do? Module Foo class Bar class_inheritable_accessor
Module: ./FOO/BAR/Foobar.pm use strict; use warnings; package Foobar; our($VERSION , @ISA , @EXPORT ,
I have a GWT module with the X-GWT-Module-Base http://host:8080/foo/ and would like to call
I have seen this in initializers in rails (config/initializers/foo.rb) module foo module bar def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.