Robert Klemme wrote:
Since we're in Color anyway, you can simplify the definition that by
I like that.
And I love the collective refactoring that goes on in this list!
Robert Klemme wrote:
Since we're in Color anyway, you can simplify the definition that by
I like that.
And I love the collective refactoring that goes on in this list!
I have a bunch of template documents which I need to manipulate in very simple ways. I'd like to be able to insert Ruby variables into the template. Here is a very short example:
file fruit_template.txt: This is a fruit: #{fruit}.
fruit = "apple"
f = IO.read("fruit_template.txt")Then somehow I'd like to end up with this string:
"This is a fruit: apple."
The easy answer to your question is to use eval():
>> fruit = "apple"
=> "apple"
>> f = 'This is a fruit: #{fruit}.'
=> "This is a fruit: \#{fruit}."
>> eval %Q{"#{f}"}
=> "This is a fruit: apple."
The right answer is to use a template library, like the standard ERB:
>> f = "This is a fruit: <%= fruit %>."
=> "This is a fruit: <%= fruit %>."
>> require "erb"
=> true
>> ERB.new(f).result(binding)
=> "This is a fruit: apple."
Hope that helps.
James Edward Gray II
On Mar 23, 2006, at 3:31 PM, Stephen Bannasch wrote:
I got great improvements to make color usage enterprise ready...
Here comes my first class color object using state-of-the-art
design patterns for better scalability and performance
I was designed using the rational design process + UML
class ColorRegistry
private
def self.mix(*colors)
r,g,b=[0,0,0]
while color = colors.shift
r += color[0]
g += color[1]
b += color[2]
end
[r,g,b]
end
RED = [255,0,0]
GREEN = [0,0b11111111,0]
BLUE = [0,0,0xff]
WHITE = mix(RED, GREEN, BLUE)
YELLOW = mix(RED, GREEN)
# more to come
public
def self.get_binding
binding
end
end
class ColorEvaluator
def self.get(name)
eval(name.upcase, ColorRegistry.get_binding)
end
end
puts ColorEvaluator.get("yellow")
puts ColorEvaluator.get("red")
puts ColorEvaluator.get("white")
--- Ursprüngliche Nachricht ---
Von: Jim Weirich <jim@weirichhouse.org>
An: ruby-talk@ruby-lang.org (ruby-talk ML)
Betreff: Re: Is this a kind of design patterns?
Datum: Fri, 24 Mar 2006 01:13:52 +0900Robert Klemme wrote:
> Since we're in Color anyway, you can simplify the definition that byI like that.
And I love the collective refactoring that goes on in this list!
--
-- Jim Weirich--
Posted via http://www.ruby-forum.com/\.