« first day (1038 days earlier)      last day (2217 days later) » 

00:48
:) on commit messages
 
1 hour later…
01:51
@WayneConrad This doesn't seem to be working for me. Does this require the use of OpenStruct? I tried it with block.instance_eval({binding}), which gave me access to no variables.
@thesecretmaster The code in the answer works--I tested it. I can't tell you what's wrong with your code without seeing a minimal reproducible example.
Sorry! Where are my manners... An MCVE would be kinda hard because it's part of a bigger thing, but gimme a sec and I'll create one, or I'll solve my issue by trying to create one. Happens too often to me.
:) I think the OpenStruct solution is good, so I'm wondering what's keeping you from adapting it to your needs. It'll be good to see your code.
All of the code is in a gitlab repo, but I get the feeling that you have better things to do than sift through my messy code. If your taking your time to help me, then I should make it as easy as possible for you to do that.
But the basic idea is that the user calls a method which takes a block. I then want to evaluate an ERB objects result in the scope of the block passed by the user. Because result takes a binding, I think that what I need to do is get the binding of the block passed by the user and then pass that binding to result.
My original code:
require "erb"
def blocke(&block)
        block
end
a = blocke do
        @hi = "me"
end
p ERB.new("<%= @hi %>").result(a.binding) #=> "me"
And the last line modified with advice from the answer:
p ERB.new("<%= @hi %>").result(a.instance_eval{binding})
02:23
Let me see if I can make that work. If I can't, I'll propose a simpler alternative.
Something looks funny about your example. What is blocke, and why does it just return the block?
Because the a = do @var = "value" end syntax doesn't work.
I guess I could also use Proc.new
Can you please show me how your API will be used by the end user?
Skip all mention of how to achieve it--just what does it look like from the user's point of view...
method "param1", "filename.erb" do
  # And other logic
  @var = "val"
end
It's part of a rack-based gem, so this would say "when '/param1' is requested, return 'filename.erb' evaluated in the context of this block so that @var is accessible.'
This isn't exactly what you're asking for, but maybe this helps:
    require 'erb'

    def foo(template, &block)
      o = Object.new
      o.instance_eval(&block)
      result = ERB.new(template).result(o.instance_eval {binding} )
    end

    template = "Foo: <%= @foo %>"
    expanded_template = foo(template) do
      @foo = 1
    end
    puts expanded_template    # => Foo: 1
02:52
This works for me. The trick where you call the block in a new object to not pollute the scope is really cool! Thanks so much for your help!
You're welcome :)
 
10 hours later…
12:54
It's sad that there is a question which includes "enter code here" (currently at -11 and on hold)
 
2 hours later…
14:44
I agree.
 
1 hour later…
15:56
I think I may have found a bug in SO. In Chrome and Firefox the tab title of this question displays the backtick over the r in require. Can anyone reproduce?
It's definitely before the r.
The kerning is really small, though
It's above and before.
Win10/FF/no-repro
OK on FF 49.0.2 on Debian Linux
16:11
Chrome spies on you anyways
@WayneConrad It looks fine in the question title, but in the tab title it looks wrong
16:34
Is it confusing to type argument: :value without the space in a keyword argument?
It looks OK in my tab title, too.
OK, apparently it's so confusing it even confuses the parser.
irb(main):650:0> p arg::value
NameError: undefined local variable or method `arg' for main:Object
        from (irb):650
        from C:/Ruby23-x64/bin/irb.cmd:19:in `<main>'
irb(main):651:0> p arg: :value
{:arg=>:value}
=> {:arg=>:value}
irb(main):652:0>
 
5 hours later…

« first day (1038 days earlier)      last day (2217 days later) »