Sunday, November 20, 2005
Code as Language II
I have found the Ruby Extension project.
Let's see what I can get from a first quick pass..
Excerpt VII
very nice for metaprogramming. metaprogramming in ruby can quickly get quite messy if you are not careful to continuously refactor your intent.
Excerpt VIII
Excerpt IX
Excerpt X
Excerpt XI
Of course. of course!
Excerpt XII
Excerpt XIII
Excerpt XIV
How do they do it? I have to find out.
Very useful and very dangerous.
I can see lots of interesting work been done here by exploiting the context of a call.
Let's see what I can get from a first quick pass..
Excerpt VII
str = "Hello, \n\n world!"
str.define_method(:heading) {
('*** ' + self.join + ' ***').indent(3)
}
str.heading # -> " *** Hello, world! ***"
very nice for metaprogramming. metaprogramming in ruby can quickly get quite messy if you are not careful to continuously refactor your intent.
Excerpt VIII
person = OpenStruct.new do |p|Ah, I have written stuff like this in the past! This is very much in the spirit of the nooclay. Maybe I should publish my Clay code one of these days.
p.name = 'John'
p.age = 90
end
Excerpt IX
class Personautoinit is v. useful. that's another bit of nooclay behaviour.
attr_reader :name, :age, :pension
autoinit :name, :age do |p|
@pension = 30000 + (@age - 65) * 350
end
end
Excerpt X
HELP = %{Ah, this is similar to Brian Marick's code trick. I like it. Text should become a first class citizen of code.
| Usage: ...
| Lorem ipsum...
}.trim('|')
Excerpt XI
STR = "Hello, world!"
STR.starts_with? "Hello" # -> true
STR.ends_with? "world" # -> false
File.write("hello.txt", STR)
Of course. of course!
Excerpt XII
arr = [1, 3, 5]I wrote it myself in the past. very useful and practical.
arr.rand # -> 1, 3, or 5
arr.none? { |n| n.even? } # -> true
arr.one? { |n| n > 4 } # -> true
Excerpt XIII
Class.by_name "Process::Sys" # -> Process::Sysagain, very useful metaprogramming.
Process::Sys.basename # -> "Sys"
Excerpt XIV
Binding.of_callerThis allows you to access the context of the caller of a function.
How do they do it? I have to find out.
Very useful and very dangerous.
I can see lots of interesting work been done here by exploiting the context of a call.