Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Alternatives to sed + awk



On 10 March 2011 06:17, Stephen J. Turnbull <stephen@example.com> wrote:

> It is admittedly relatively hard to add syntax in Python.

I've not yet written enough Python to need a good DSL, so I'll let you
know how it goes when I do. :)

I do actually like Python quite a bit; getattr and getattr let me do a
lot of instance_evaly things when I need to, and the language is nice
and regular. Any language that has an implementation of FlexMock[1][2]
is OK by me!

> However, again it seems to me to be easy to abuse.

Sure, but so's C.

> "Use in haste, repent at leisure" seems to be the watchword of programmers.

There will always be hastily written code requiring penance; I don't
think it is the province of the language to try to prevent bad code.
The bad coders will just find a way to write their bad code anyway. ;)

> However, the basic syntax of Lisp aka "Lots of Irritating Single
> Parentheses" is detested by many.

Just like Python's whitespace requirements, you get used to it.

> Modifying methods can be done by subclassing.  However, Python's
> approach is to decorate them:

Ruby usually uses method chaining to decorate. Rails even provides an
extension for this:

alias_method_chain <func>, <decorator>

e.g.

def func
  1 + 1 == 2
end

def func_with_decorator
  puts "Blah blah blah"
  func_without_decorator
end

alias_method_chain :func, :decorator


The more generic decorator supplied in your example would probably be
implemented using a Proc object:

def trace(func)
  if debug:
    Proc.new do
      puts "Entering #{func}"
      result = func.call
      puts "Leaving #{func}"
      result
    end
  else:
    func
  end
end


> BTW, since Ruby apparently allows you to omit parentheses, how do you
> distinguish between returning the value of a function call and the function
> itself, eg, in the argument to the trace invocation above?

functions themselves are not first class objects as in Python; you
need to turn them into a Proc (or, more powerfully a Binding) object.

>  > def test_crappy_class
>  >   crap = CrappyClass.new
>  >   assert_equal 0, crap.instance_eval{@example.com
>  >   crap.increment
>  >   assert_equal 1, crap.instance_eval{@example.com
>  > end
>
> But, IIUC, in Python the member `value' is not hidden, so you just write
>
> def test_crappy_class
>    crap = CrappyClass.new
>    assert_equal(0, crap.value)
>    crap.increment()
>    assert_equal(1, crap.value)

Sure. What's the difference, really? In Ruby, instance variables are
hidden by default, which is a nice signal to programmers that they
shouldn't be considered part of a class's API. But if you need to get
at them, you can do it.

> That end" end end end looks like gdb. ;-)

Heh heh.

> I see the attraction of the Ruby features for quick hacking, but I
> also see the attraction of two-year-olds to swimming pools not
> surrounded by fences.  The latter is a disaster; I hope you never
> regret succumbing to the temptation of monkey-patching-supported-by-
> syntax. ;-)

Monkey patching is not a tool I use lightly.

Cheers,
Josh


[1] http://flexmock.rubyforge.org/
[2] http://has207.github.com/flexmock/


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links