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
  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|
p.name = 'John'
p.age = 90
end
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.

Excerpt IX
  class Person
attr_reader :name, :age, :pension
autoinit :name, :age do |p|
@pension = 30000 + (@age - 65) * 350
end
end
autoinit is v. useful. that's another bit of nooclay behaviour.

Excerpt X
  HELP = %{
| Usage: ...
| Lorem ipsum...
}.trim('|')
Ah, this is similar to Brian Marick's code trick.  I like it.  Text should become a first class citizen of code.

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]
arr.rand # -> 1, 3, or 5
arr.none? { |n| n.even? } # -> true
arr.one? { |n| n > 4 } # -> true

I wrote it myself in the past. very useful and practical.

Excerpt XIII
Class.by_name "Process::Sys"   # -> Process::Sys
Process::Sys.basename # -> "Sys"
again, very useful metaprogramming.

Excerpt XIV
   Binding.of_caller
This 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.



Comments: Post a Comment

Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?