librelist archives

« back to archive

question about Shoes.app block can not read the var from out side scope

question about Shoes.app block can not read the var from out side scope

From:
yeer kunth
Date:
2012-03-30 @ 13:12
hi all shoes expers:
could some one explain this behavior?


this is ok for vars reading, the output is 'i come from the darkness'
```ruby
@g = 'i come from the darkness'
def fun
  if block_given?
    yield
  end
end
fun do
  p @g
end
```


but not this one
```ruby
@g = 'i come from the darkness'
Shoes.app do
  para @g
end
```

Re: [shoes] question about Shoes.app block can not read the var from out side scope

From:
Steve Klabnik
Date:
2012-03-30 @ 13:26
Hi yeer!

This is actually a Ruby thing, not a shoes thing.

Here's the deal: your method is defined in the same scope as the
instance variable. So it can access it. But when you try to access it
from within the Shoes.app block, you're accessing an instance variable
from inside the instance of Shoes, not the overall global instance. If
you want to do this, make it a constant or a global.

-Steve

On Fri, Mar 30, 2012 at 9:12 AM, yeer kunth <yeerkunth@gmail.com> wrote:
> hi all shoes expers:
> could some one explain this behavior?
>
>
> this is ok for vars reading, the output is 'i come from the darkness'
> ```ruby
> @g = 'i come from the darkness'
> def fun
>   if block_given?
>     yield
>   end
> end
> fun do
>   p @g
> end
> ```
>
>
> but not this one
> ```ruby
> @g = 'i come from the darkness'
> Shoes.app do
>   para @g
> end
> ```

Re: [shoes] question about Shoes.app block can not read the var from out side scope

From:
Steve Klabnik
Date:
2012-03-30 @ 13:27
Here's a pure Ruby example:

1.9.3-p125 :001 > @foo = "foo!"
 => "foo!"
1.9.3-p125 :002 > class Bar
1.9.3-p125 :003?>   def foo
1.9.3-p125 :004?>     puts @foo
1.9.3-p125 :005?>     end
1.9.3-p125 :006?>   end
 => nil
1.9.3-p125 :007 > Bar.new.foo
nil
 => nil

Re: [shoes] question about Shoes.app block can not read the var from out side scope

From:
yeer kunth
Date:
2012-03-30 @ 13:35
hi Steve
thank you for your example
i understood your example. but you dont involve with block.


I just follow your idea and simulate the situation like this:
it works too.

class B
  def app
    if block_given?
      yield
    end
  end
end
@g = 'i come from the darkness'
B.new.app do
  puts @g
end


On Fri, Mar 30, 2012 at 9:27 PM, Steve Klabnik <steve@steveklabnik.com>wrote:

> Here's a pure Ruby example:
>
> 1.9.3-p125 :001 > @foo = "foo!"
>  => "foo!"
> 1.9.3-p125 :002 > class Bar
> 1.9.3-p125 :003?>   def foo
> 1.9.3-p125 :004?>     puts @foo
> 1.9.3-p125 :005?>     end
> 1.9.3-p125 :006?>   end
>  => nil
> 1.9.3-p125 :007 > Bar.new.foo
> nil
>  => nil
>

Re: [shoes] question about Shoes.app block can not read the var from out side scope

From:
Eric Watson
Date:
2012-03-30 @ 13:44
I think the difference is that Shoes doesn't just yield to the block you 
pass in. It changes `self` within the block. 

Eric

On Mar 30, 2012, at 8:35 AM, yeer kunth <yeerkunth@gmail.com> wrote:

> hi Steve
> thank you for your example
> i understood your example. but you dont involve with block.
> 
> 
> I just follow your idea and simulate the situation like this:
> it works too.
> 
> class B
>   def app
>     if block_given?
>       yield
>     end
>   end
> end
> @g = 'i come from the darkness'
> B.new.app do 
>   puts @g
> end
> 
> 
> On Fri, Mar 30, 2012 at 9:27 PM, Steve Klabnik <steve@steveklabnik.com> wrote:
> Here's a pure Ruby example:
> 
> 1.9.3-p125 :001 > @foo = "foo!"
>  => "foo!"
> 1.9.3-p125 :002 > class Bar
> 1.9.3-p125 :003?>   def foo
> 1.9.3-p125 :004?>     puts @foo
> 1.9.3-p125 :005?>     end
> 1.9.3-p125 :006?>   end
>  => nil
> 1.9.3-p125 :007 > Bar.new.foo
> nil
>  => nil
> 

Re: [shoes] question about Shoes.app block can not read the var from out side scope

From:
Steve Klabnik
Date:
2012-03-30 @ 13:50
> I think the difference is that Shoes doesn't just yield to the block you
> pass in. It changes `self` within the block.

Bingo.

Re: [shoes] question about Shoes.app block can not read the var from out side scope

From:
Steve Klabnik
Date:
2012-03-30 @ 13:51
1.9.3-p125 :014 > B.new.instance_eval do
1.9.3-p125 :015 >     app do
1.9.3-p125 :016 >       puts @g
1.9.3-p125 :017?>     end
1.9.3-p125 :018?>   end
nil
 => nil

Re: [shoes] question about Shoes.app block can not read the var from out side scope

From:
yeer kunth
Date:
2012-03-30 @ 14:14
hi, Eric
you are right!
I find out the answer: line 84
app.instance_eval &blk

it pass the block intead of call it.
thank you

in lib/shoes/main.rb line

On Fri, Mar 30, 2012 at 9:44 PM, Eric Watson <wasnotrice@gmail.com> wrote:

> I think the difference is that Shoes doesn't just yield to the block you
> pass in. It changes `self` within the block.
>
> Eric
>
>
> On Mar 30, 2012, at 8:35 AM, yeer kunth <yeerkunth@gmail.com> wrote:
>
> hi Steve
> thank you for your example
> i understood your example. but you dont involve with block.
>
>
> I just follow your idea and simulate the situation like this:
> it works too.
>
> class B
>   def app
>     if block_given?
>       yield
>     end
>   end
> end
> @g = 'i come from the darkness'
> B.new.app do
>   puts @g
> end
>
>
> On Fri, Mar 30, 2012 at 9:27 PM, Steve Klabnik <steve@steveklabnik.com>wrote:
>
>> Here's a pure Ruby example:
>>
>> 1.9.3-p125 :001 > @foo = "foo!"
>>  => "foo!"
>> 1.9.3-p125 :002 > class Bar
>> 1.9.3-p125 :003?>   def foo
>> 1.9.3-p125 :004?>     puts @foo
>> 1.9.3-p125 :005?>     end
>> 1.9.3-p125 :006?>   end
>>  => nil
>> 1.9.3-p125 :007 > Bar.new.foo
>> nil
>>  => nil
>>
>
>

Re: question about Shoes.app block can not read the var from out side scope

From:
yeer kunth
Date:
2012-03-30 @ 13:29
this one seems to be OK too.

class A
  def self.app
    if block_given?
      yield
    end
  end
end
@g = 'i come from the darkness'
A.app do
  puts @g
end

i would like to dive into the insight of shoes.
but there is quit lots of code,
would some one give some tips?
for example, which file?
BTW, I am using green_shoe


On Fri, Mar 30, 2012 at 9:12 PM, yeer kunth <yeerkunth@gmail.com> wrote:

> hi all shoes expers:
> could some one explain this behavior?
>
>
> this is ok for vars reading, the output is 'i come from the darkness'
> ```ruby
> @g = 'i come from the darkness'
> def fun
>   if block_given?
>     yield
>   end
> end
> fun do
>   p @g
> end
> ```
>
>
> but not this one
> ```ruby
> @g = 'i come from the darkness'
> Shoes.app do
>   para @g
> end
> ```
>