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 6188717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:17:04+00:00 2026-05-24T02:17:04+00:00

Forgive me, guys. I am at best a novice when it comes to Ruby.

  • 0

Forgive me, guys. I am at best a novice when it comes to Ruby. I’m just curious to know the explanation for what seems like pretty odd behavior to me.

I’m using the Savon library to interact with a SOAP service in my Ruby app. What I noticed is that the following code (in a class I’ve written to handle this interaction) seems to pass empty values where I expect the values of member fields to go:

create_session_response = client.request "createSession" do
  soap.body = {
    :user => @user, # This ends up being empty in the SOAP request,
    :pass => @pass  # as does this.
  }
end

This is despite the fact that both @user and @pass have been initialized as non-empty strings.

When I change the code to use locals instead, it works the way I expect:

user = @user
pass = @pass

create_session_response = client.request "createSession" do
  soap.body = {
    :user => user, # Now this has the value I expect in the SOAP request,
    :pass => pass  # and this does too.
  }
end

I’m guessing this strange (to me) behavior must have something to do with the fact that I’m inside a block; but really, I have no clue. Could someone enlighten me on this one?

  • 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-05-24T02:17:05+00:00Added an answer on May 24, 2026 at 2:17 am

    First off, @user is not a “private variable” in Ruby; it is an instance variable. Instance variables are available within the the scope of the current object (what self refers to). I have edited the title of your question to more accurately reflect your question.

    A block is like a function, a set of code to be executed at a later date. Often that block will be executed in the scope where the block was defined, but it is also possible to evaluate the block in another context:

    class Foo
      def initialize( bar )
        # Save the value as an instance variable
        @bar = bar
      end
      def unchanged1
        yield if block_given? # call the block with its original scope
      end
      def unchanged2( &block )
        block.call            # another way to do it
      end
      def changeself( &block )
        # run the block in the scope of self
        self.instance_eval &block
      end
    end
    
    @bar = 17
    f = Foo.new( 42 )
    f.unchanged1{ p @bar } #=> 17
    f.unchanged2{ p @bar } #=> 17
    f.changeself{ p @bar } #=> 42
    

    So either you are defining the block outside the scope where @user is set, or else the implementation of client.request causes the block to be evaluated in another scope later on. You could find out by writing:

    client.request("createSession"){ p [self.class,self] }
    

    to gain some insight into what sort of object is the current self in your block.

    The reason they “disappear” in your case—instead of throwing an error—is that Ruby permissively allows you to ask for the value of any instance variable, even if the value has never been set for the current object. If the variable has never been set, you’ll just get back nil (and a warning, if you have them enabled):

    $ ruby -e "p @foo"
    nil
    
    $ ruby -we "p @foo"
    -e:1: warning: instance variable @foo not initialized
    nil
    

    As you found, blocks are also closures. This means that when they run they have access to local variables defined in the same scope as the block is defined. This is why your second set of code worked as desired. Closures are one excellent way to latch onto a value for use later on, for example in a callback.

    Continuing the code example above, you can see that the local variable is available regardless of the scope in which the block is evaluated, and takes precedence over same-named methods in that scope (unless you provide an explicit receiver):

    class Foo
      def x
        123
      end
    end
    x = 99 
    f.changeself{ p x } #=> 99
    f.unchanged1{ p x } #=> 99
    f.changeself{ p self.x } #=> 123
    f.unchanged1{ p self.x } #=> Error: undefined method `x' for main:Object
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope you guys forgive me ... I know this is simple but its
Forgive me guys, new to Ruby, actually this is the first lang I have
Hey guys, I'm trying to get my head around LINQ and FP, so forgive
Forgive a newbie, I don't even know how to ask this question properly: I
Forgive me for being a little naive perhaps, but it seems that System.Windows.Controls.DataVisualization.Charting has
Forgive me if the title (and my SQL knowledge) seems a bit vague but
Forgive me if my understanding of this topic has some shortcomings, I only know
Forgive the simplistic question. I'd like to pass a variable from javascript to php
Hey guys. Working on Mac OSX 10.6, My problem is that I would like
Forgive me as this is probably an easy question for your jQuery guys out

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.