[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:03898] Re: Adding a '<<<' here_document syntax

From: Dave Thomas <Dave@...>
Date: 2000-07-08 00:40:19 UTC
List: ruby-talk #3898
shreeve@s2s.org (Steve Shreeve) writes:

> 1) It leaves leaves my code looking very ugly, with the terminators not 
> indented properly, as in:

In Ruby, you can indent the terminator if you precede the original
with a minus sign:

    str = <<-END
    This is
    the
    END

> 2) Also, I like to indent the actual HTML so my program is more readable, but 
> when I do this, the generated HTML has a bunch of leading whitespace which can 
> sometimes even cause problems... uck! To prevent the leading whitespace 
> problem, I usually do this:

You could do a similar thing in Ruby without munging the source. For
example, you could define a method "tidy" which stripped all leading
whitespace:

    def tidy(str)
      str.gsub(/^\s+/s, '')
    end

And then call it using

    str = tidy <<-END
    This is
    the
    END

Or you could make it a method of class String:

    class String
      def tidy
        gsub(/^\s+/s, '')
      end
    end

And then call it using

    str = <<-END.tidy
    This is
    the
    END



Of course, you can then develop tidy further to honor indentation
within the block and so on.


Regards


Dave

In This Thread

Prev Next