Thursday, May 25, 2006

Assignment in Ruby - Block Assignment

For the second installment of Assignment in Ruby, I would like to explore block assignment. Block assignment is very similar to the simple scoped assignments I explored in my first post. To understand the differences, it is necessary to understand how block variables are scoped.

When a block uses a local variable, Ruby has to determine what is being used. { |x, y, z| puts x, y, z } uses three local variables. Interestingly, these three variables could each potentially have a different scope.

If there is an x in the current local scope, the variable x used in the block will be that local variable, meaning the assignment will be an :lasgn (local assignment).

If there is no y in the current local scope but there is a y in an enclosing block, the y variable used will be the block scoped variable from the outer block and the assignment will be a :dasgn (block assignment).

If there is no z in the current local scope and there is no z in any enclosing blocks, then a new z will be created in the scope of the current block and the assignment will be a :dasgn_curr (block local assignment).

The different scopes are each illustrated in the following code:
$ echo "x = 123; foo() {|x| y = 456; bar() {|x,y,z| puts x, y, z}}" | parse_tree_show -f
[[:lasgn, :x, [:lit, 123]],
[:iter,
[:fcall, :foo],
[:lasgn, :x],
[:block,
[:dasgn_curr, :y],
[:dasgn_curr, :y, [:lit, 456]],
[:iter,
[:fcall, :bar],
[:masgn, [:array, [:lasgn, :x], [:dasgn, :y], [:dasgn_curr, :z]]],
[:fcall, :puts, [:array, [:lvar, :x], [:dvar, :y], [:dvar, :z]]]]]]]

Written a little more nicely, the code under inspection is:
x = 123
foo() { |x|
y = 456
bar() { |x,y,z|
puts x, y, z
}
}

Note the 2nd to last line of the S expression [:masgn, [:array, [:lasgn, :x], [:dasgn, :y], [:dasgn_curr, :z]]]. The first thing to notice is:masgn. :masgn stands for multiple assignment and will be covered in its own post. After the :masgn you can see concrete examples of the local assignment to x, the outer block assignment to y and the current block assignment to z.

This can be a sticky situation and I know I missed it when I was learning Ruby. I only internalized how block scopes are handled when I saw it reflected in ParseTree and the AST. I still don't really understand why you would want to reuse local scoped variables inside a block but that is probably an indication of my weakness with closures. If you have any ideas, please let me know.

I hope you have enjoyed another brief forray into Assignment in Ruby. Join me next time as we explore multiple assignment and how it can simplify your life.

9 Comments:

Anonymous Anonymous said...

Very interesting! Ruby's block assignment and variable scoping rules in blocks are the one area in which I believe it really lacks flexibility (at least as compared to the rest of the language).

2:40 PM  
Blogger gnupate said...

It's funny, but I got caught by this same part of the Ruby landscape while I was working on CheckR. Once I looked at it with ParseTree it all became much more clear.

Thanks for continuing to write about stuff like this.

6:49 AM  
Anonymous Anonymous said...

Sean,

I am a Ruby newbie. I was surprised with the different scope of block variables.

x=(1..1000).to_set
x.find_all{|x|x%2==0}
x was changed to 1000, contradict to my understanding. So I googled, your site comes up. I thank you for the clarification. However, a better question is, why Ruby choose to implement this way?

4:15 AM  
Anonymous Anonymous said...

The problem is not that Ruby decided to implement it (by default) this way, but that there is no way to change that, there is no

blah.find_all{ my |x| ... }

Another great thing is that if you define a setter x= in a class and then in another method you write

x = 5

Ruby silently creates a new local variable. Again, there is no way to ask it to make you specify that you do want a new variable. You have to remember to use self. even though calling other methods works without. Sweet.

7:01 AM  
Anonymous Term Papers said...

I have been visiting various blogs for my term papers writing research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards

4:04 AM  
Anonymous canada goose sale said...

When it comes to the corporate clothing your employees wear, do you prefer to provide it yourself or give them a clothing allowance so you don't have to worry about choosing their attire? While each has their benefits, here are some pros and cons to giving your employees a clothing allowance.Advantages of a Clothing AllowanceYou may not have the time to delegate each item of clothing your employees wear, especially if you run a large corporation. By providing your employees with a clothing allowance, both you and the employee can benefit in the following ways:You will not have to take the time to choose the best clothing for each of your employeesYour employees will have a little bit of freedom when it comes to deciding what they wear to work.A certain colour or fabric may look better on their frame than another, and if everyone is wearing different types of corporate clothing, they don't have to wear something that isn't flattering.Employees don't have to worry about the cost of their clothing. A clothing allowance allows them to choose the type of clothing they need, whether it is pantyhose, socks, shoes, suits, or dress shirts.Disadvantages of a Corporate Clothing AllowanceWhile there are several advantages to providing your employees with a corporate clothing allowance, there are quite a few disadvantages as well.Employees may fail to provides receipts as proof they used the allowance for clothingClothing items that are not needed for use in the work place may be purchased and worn other places.Employees may not reserve certain items, like belts, shoes, and shirts for just the workplace. As a result, these clothing items may become worn from excess use.They may provide proof of a clothing purchase, but may sell the item and keep the extra cash for themselves.Certain employees may choose inappropriate clothing items, which may not reflect the professional attitude of your company.Employees may spend all of their allowance on one expensive item of clothing and may not be able to afford the rest of the clothing they need to work with your company.The Bottom Line on Clothing AllowancesWhile clothing allowances can give your employees the freedom to choose the clothes they want to wear, you may not be happy with the results. Some workers may even take advantage of your kindness, especially if they already own professional attire.Consider the advantages of using a clothes allowance and see if you can provide your employees with the same advantages when you supply corporate clothes. Many times, corporate wear can be altered and tailored to fit and hang well on many different types of body frames. You may want to provide other needed items besides the basic clothing, such as shirts and socks.All in all, you may find it is better to provide your workers with the corporate clothing you wish for them to wear in order to avoid problems.

5:12 PM  
Blogger amily johns said...

Guys, I' m looking for a dissertation methodology writing assistance for a long time and can't find. Can you recommend me some professional ones?

1:18 AM  
Blogger Ube said...

With the help of your advice I found the place where I ordered a dissertation discussion assignment. Thank you so much.

2:18 AM  
Anonymous Anonymous said...

I'm wondering if it's hard to learn Ruby, is that more complicated than JavaScript? I was so confused with JS so had to order hypothesis for a dissertation due to missing a deadline

4:40 AM  

Post a Comment

<< Home