Newbie: Block Local Variable Inheritance



  • 1. problem with cerise & active_record & mysql on mac os x 10.3
    Hi all, Whenever I try to start the cerise server and one of the applcations requires active_record, I get the following error: dyld: /usr/local/bin/ruby can't open library: /usr/local/mysql-4.0.14/lib/mysql/libmysqlclient.12.dylib (No such file or directory, errno = 2) Trace/BPT trap On my windows machine everything works. On the mac, I'm using cerise 0.7, ruby 1.8.1 compiled from the source, and mysql 4.0.20-standard downloaded from the mysql site. Ruby and mysql run fine, Cerise starts fine if no app imports active_record, and active_record imports fine in ruby programs outside of cerise. I'm really new to mac os x and unix (like, 3 days), so please be kind and don't assume any previous knowledge. Also, when installing ruby, it placed itself in /usr/local/, while I'd prefer it to override the 1.6 that came with the system in /usr/. Is there a way to specify it to make install, or should i do it manually? (how?) Thanks in advance.
  • 2. Problems creating a method to define a class with its method
    Dear list, I want to create a method that creates certain kinds of classes for me. The following doesn't work: ### start pseudo code module My_Module class Bigclass attr_accessor :test def @test = para puts "your parameter is " + @test end def Bigclass.check puts "I'm here" end end def My_Module.subclass(name, &code) eval <<-EOS class #{name} < Bigclass def #{name}.run(p) super(p) #{} end end EOS end end My_Module::subclass("Newclass"){ puts "doesn't work" + @test.to_s check() }"my parameter") ### end pseudo code the problem is that the #{} is executed before the class is defined, so @test isn't filled yet (super(p) isn't executed at this time. how can I change the code, so that ode?is part of the definition of the class (i.e. part of its method un?? (the code can be very long, so putting it in a string is not very comfortable regarding quoting etc. (I also want the code to be highlighted in an editor). benny
  • 3. Ruby Module Naming Convention vs Java Namespaces
    How do we manage namespaces in Ruby to avoid collisions with 3rd parties? In Java, they use something like "" as a naming convention which then maps to directories. In Ruby, how do we name modules so our "foo" module doesn't collide with a 3rd-party module named "foo" from another company?

Newbie: Block Local Variable Inheritance

Postby Andrew C. » Thu, 08 Jun 2006 23:55:17 GMT

Hi, all.

I'm a total newbie, currently working my way through 'Programming Ruby' by 
Dave Thomas et al. I've read a passage about the inheritance of local 
variables by blocks (p51 of my edition), and I can't quite make sense of 
it... or, rather, I thought I had a vague notion of what was being said, but 
I can't come up with any example code that demonstrates what I believe is 
being described.

The passage says: "If the parameters to a block are existing local 
variables, those variables will be used as the block parameters, and their 
values may be changed by the block's execution... If [variables inside the 
block] first appeared outside the block, the variables will be shared 
between the block and the surrounding environment."

Could someone show me a little code that demonstrates a block changing a 
pre-existing local variable, as all my experiments to do so either treat 
variables/parameters within the block as local or tells me they're 
undefined, so I guess I just don't understand.

Thanks in advance for any assistance.


Re: Newbie: Block Local Variable Inheritance

Postby Andrew C. » Thu, 08 Jun 2006 23:58:32 GMT

The m{*filter*}of the story is keeping reading the book! :-)

Pretend I didn't post this dumb question, and it can be our little secret... 


Re: Newbie: Block Local Variable Inheritance

Postby Robert Klemme » Fri, 09 Jun 2006 00:33:13 GMT

Wow, it took you just 3 minutes to digest the book?  Impressive...

My lips are sealed.

Have fun


Re: Newbie: Block Local Variable Inheritance

Postby Frank Swarbrick » Fri, 09 Jun 2006 11:50:47 GMT

I've been puzzling over this as well.  All I can think of is the 
following example:

#!/usr/bin/env ruby

class Frank
   def Frank.test(x)
     yield 1, x

i = 0
Frank.test("Test") do |i, f|
   puts i
   puts f
puts i
puts f

With the results being:
blocks.rb:15: undefined local variable or method `f' for main:Object 

Since i was defined outside of the block prior to being passed as a 
parameter to the block it can be changed (to a value of 1 in this case) 
by the method to which the block was passed and will also exist once the 
block has 'terminated'.

Since f was not defined outside of the block is no longer exists once 
the block has terminated, thus the error on line 15.

If it refers to something else, well, ya got me...


Re: Newbie: Block Local Variable Inheritance

Postby Robert Klemme » Fri, 09 Jun 2006 15:44:44 GMT

That's precisely what the doc is about.  Another example:

module Enumerable
   def my_last
     l = nil
     each {|l| }

 >> %w{aaa bb cc ddd}.my_last
=> "ddd"

Kind regards


Re: Newbie: Block Local Variable Inheritance

Postby Andrew C. » Fri, 09 Jun 2006 15:44:52 GMT

You seem to be on the right track.

My original (mis-)understanding was that variables local to the called 
method could be made accessible in the block by using the same 
variable/parameter names. I'm very glad this initial interpretation of what 
I read didn't turn out to be the case because that would have been a 
dreadful mix-up of variable scope.


Re: Newbie: Block Local Variable Inheritance

Postby Robert Klemme » Fri, 09 Jun 2006 17:45:21 GMT

Um, but it's exactly like this.  Local variables in the method *are* 
accessibly in the block much the same as scope nesting works in Java, C 
etc.  You access a local variable used outside of the block by just 
"using the same name".  These are all accesses to the same variable 

def foo
   whatever.each do |some_local|

   some_thing_else.each do



Re: Newbie: Block Local Variable Inheritance

Postby Andrew C. » Sat, 10 Jun 2006 15:22:32 GMT

Thanks, but I think we're saying the same thing.


Re: Newbie: Block Local Variable Inheritance

Postby Robert Klemme » Sat, 10 Jun 2006 15:48:27 GMT

Ah, ok. Then I don't understand your misunderstanding. :-)



Similar Threads:

1.Issue with block and getting to local variables

2.Same variable name used in block and local question

Can anybody help me here, TIA!!

This is the code.  I expect to see the string "Afterward:turkey", but
why am I getting "Afterward:camel"?  |animal| parameter in a block
should be temporary inside the block, right??

animal = "turkey"

puts "Original:" + animal

["cow","camel"].each { |animal| puts "in block:" + animal }

puts "Afterwards:" + animal


in block:cow
in block:camel


3.Extract local variables from a block?

Is it possible to extract local variables from within a block, while
standing outside said block? Here's an example which might illustrate
what I'm trying to do:

def local_vars_from(&block)
  eval('local_variables', block).each do |var|
    new_local_var = eval(var, block)

local_vars_from do
  unknown_local_var = :blah

Of course, this example doesn't work at all. What I'm trying to
accomplish is a new variable in the current scope which mirrors the
variable inside the block. I'm pretty sure I'm misusing
local_variables; is there another approach to this?


4.block and method local variables

hello all,

consider follow code

irb(main):528:0* def abc
irb(main):529:1> ppp=111
irb(main):530:1> puts ppp
irb(main):531:1> yield
irb(main):532:1> puts ppp
irb(main):533:1> ppp
irb(main):534:1> end
=> nil
irb(main):536:0> abc {}
=> 111
irb(main):537:0> abc { puts ppp }
NameError: undefined local variable or method `ppp' for main:Object
         from (irb):537
         from (irb):537:in `abc'
         from (irb):537
         from :0
irb(main):540:0* ppp = "OOO"
=> "OOO"
irb(main):541:0> abc { puts ppp }
=> 111

I am right assuming that all "local" variables in the method abc
are invisible in the block?

I tried a little more

irb(main):544:0* def cba
irb(main):545:1> ppp = 222
irb(main):546:1> puts ppp
irb(main):547:1> yield ppp
irb(main):548:1> puts ppp
irb(main):549:1> ppp
irb(main):550:1> end
=> nil
irb(main):551:0> cba {}
=> 222
irb(main):552:0> cba {|p|}
=> 222
irb(main):553:0> cba {|p| p = 333}
=> 222
irb(main):554:0> cba {|p| puts p; p = 333}
=> 222

if explicitely passed to block ppp is now visible there.
but seems to be imposible to change the "method local" variable
ppp in this case.

the following *does* work

irb(main):557:0* def xxx
irb(main):558:1> ppp = [1]
irb(main):559:1> p ppp
irb(main):560:1> yield ppp
irb(main):561:1> p ppp
irb(main):562:1> ppp
irb(main):563:1> end
=> nil
irb(main):564:0> xxx {}
=> [1]
irb(main):565:0> xxx {|x| }
=> [1]
irb(main):566:0> xxx {|x| x[0]=2 }
=> [2]

and I kind of understand why it works

if I change line 566 to
irb(main):566:0> xxx {|x| x=[2] }
than it would not work

the reason I came accross this is following
I was reading

Parameters to a block may be existing local variables; if so, the new 
value of the variable will be retained after the block completes. This 
may lead to unexpected behavior, but there is also a performance gain to 
be had by using variables that already exist
and in my understanding all variables defined in a method
are "local" (C++ background)

If not local what are they considered to be then?

Regards, Daniel

5.Block binding does not contains local variable

Maybe it contains.
But please, explain me why  "block binding" does not print a, b, and c

def vars(&block)
  b =
  puts "inner binding: ", eval("local_variables", b)
  puts "block binding: ", eval("local_variables", block.binding)

vars {|a|
  b = 1
  c = 2

My output:

ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
inner binding:
block binding:

6. adding local variables via block

7. Blocks and local variable creation

8. Newbie: require 'filename' - undefined local variable or method...

Return to ruby


Who is online

Users browsing this forum: No registered users and 82 guest