Ask a Question related to Ruby, Design and Development.

  1. #1

    Default macros

    has anyone given any thought to having macros in ruby?

    perhaps there's another way to do what i want, but i'm not sure. i keep reusing the same snippit of code over and over in this one class that includes a few variables, a dumbed-down example:

    "/Path/To/#{variable}/Directory"

    with such a simple example one would think i only needed to set an @variable to it and resuse that, but this class is usually marshalled via yaml and hence loaded using YAML::load, so initialize will not be run. the upshot of this is that i have no single place in the class that's definitely going to be excecuted where i can set the variable. so what do you do without repeating the same snippet over and over?

    oh, of course i could write a method to do it and call that each time. but this snippet really should be in a configuration file of some sort. from the example above, b/c the exact path could change.

    hmmm...guess there's always eval.

    thoughts?

    -t0


    T. Onoma Guest

  2. Similar Questions and Discussions

    1. Macros in Illustrator?
      Is it possible to record a text box in a macro and/or action? I read through the action help file, but had a hard time understanding it. I want to...
    2. macros in Photoshop?
      I have a Mavica camera that stores photos on floppies. I have to resize each photo to place them on one 8 x 10 sheet. Is there any way to memorize...
    3. [PHP-DEV] userland macros
      Is possible to implement userland macros in a PHP extension or it can only be implemented in the core? I know that macros is a preprocessor task...
    4. macros in perl
      -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi all, I'm new to perl. How it's possible to write macros in perl? C-like macros would be...
    5. Finder macros in OSX
      On Sat, 12 Jul 2003 14:26:04 +1000, heron stone wrote (in message <heronstone-9F4049.21260411072003@news-central.giganews.com>): Presumably...
  3. #2

    Default Re: macros


    11/11/2003, "T. Onoma" <transami@runbox.com> you wrote:
    >has anyone given any thought to having macros in ruby?
    >
    >perhaps there's another way to do what i want, but i'm not sure. i keep reusing the same snippit of code over and over in this one class that includes a few variables, a dumbed-down example:
    >
    > "/Path/To/#{variable}/Directory"
    >
    snipped
    >hmmm...guess there's always eval.
    >
    >thoughts?
    Is ("/Path/To/%s/Directory" % [variable]) usable to you?
    You can store string itself in some place, and then just do
    (str % [variable])

    --
    sdmitry -=- Dmitry V. Sabanin
    MuraveyLabs.

    Dmitry V. Sabanin Guest

  4. #3

    Default Re: macros

    T. Onoma wrote:
    >has anyone given any thought to having macros in ruby?
    >
    >
    As for this question, people have talked about macros in the past.

    As I recall, Matz is against adding macros to Ruby because they
    essentially allow people to make their
    own syntax, and he'd like all Ruby to be readable by anyone who knows Ruby.

    I also recall an issue with really powerful macros being a lot more
    complex in Algol style languages than
    they are in Lisp style languages.

    In other words, you're probably out of luck as far as macros go in Ruby,
    unless there's some non-standard
    extention I don't know about (quite possible). You might look at
    ruby-talk.org to see what's gone on in the
    past with the macro discussions.

    - Dan


    Dan Doel Guest

  5. #4

    Default Re: macros

    T. Onoma wrote:
    >
    >has anyone given any thought to having macros in ruby?
    >
    Ruby isn't a compiled language so you wouldn't gain a whole lot with some
    sort of macro language in addition to what ruby already offers. You have
    eval and instance_eval in Ruby, both of which accept strings as parameters
    so you essentially have the ability to create your own macros:

    def add_foo_method(o, name, result)
    o.instance_eval %{
    def #{name}
    #{result}
    end
    }
    end

    o = Object.new
    add_foo_method(o, :coolness_level, 10)
    o.coolness_level #=> 10

    def new_class(name, code)
    eval %{
    class #{name}
    #{code}
    end
    }
    end

    new_class "A", %{
    def hi
    puts "hello world!"
    end
    }

    a = A.new
    a.hi #=> "hello world!"

    You can abuse this technique, but it has it's place.
    ___________________
    John Long
    [url]www.wiseheartdesign.com[/url]



    John W. Long Guest

  6. #5

    Default Re: macros

    > Is ("/Path/To/%s/Directory" % [variable]) usable to you?
    > You can store string itself in some place, and then just do
    > (str % [variable])
    limited, but would work in one case. thanks!

    -t0

    T. Onoma Guest

  7. #6

    Default Re: macros

    Hi!

    * T. Onoma; 2003-11-11, 13:12 UTC:
    > perhaps there's another way to do what i want, but i'm not sure. i
    > keep reusing the same snippit of code over and over in this one
    > class that includes a few variables, a dumbed-down example:
    >
    > "/Path/To/#{variable}/Directory"
    You can (ab)use the C preprocessor in the following manner:

    $ cat trick_rb.c

    #define sniplet(a) ("/Path/To/" + a + "/Directory")
    puts sniplet('some')

    $ gcc -E trick_rb.c > trick.rb
    $ cat trick.rb
    # 1 "trick_rb.c"

    puts ("/Path/To/" + 'some' + "/Directory")

    $ ruby trick.rb

    /Path/To/some/Directory

    Josef 'Jupp' Schugt
    --
    .-------.
    message > 100 kB? / | |
    sender = spammer? / | R.I.P.|
    text = spam? / ___| |___

    Josef 'Jupp' SCHUGT Guest

  8. #7

    Default Re: macros

    On Wed, 12 Nov 2003 03:58:43 +0900, Josef 'Jupp' SCHUGT wrote:
    >
    > You can (ab)use the C preprocessor in the following manner:
    >
    > $ cat trick_rb.c
    >
    > #define sniplet(a) ("/Path/To/" + a + "/Directory")
    > puts sniplet('some')
    >
    > $ gcc -E trick_rb.c > trick.rb
    > $ cat trick.rb
    > # 1 "trick_rb.c"
    >
    > puts ("/Path/To/" + 'some' + "/Directory")
    >
    > $ ruby trick.rb
    >
    > /Path/To/some/Directory
    >
    If you're going to go _that_ route, why not use M4?
    Tim Hunter Guest

  9. #8

    Default Re: macros

    * Tim Hunter; Tue, 11 Nov 2003 23:00:39 GMT
    > On Wed, 12 Nov 2003 03:58:43 +0900, Josef 'Jupp' SCHUGT wrote:
    > If you're going to go _that_ route [and use the C preprocessor],
    > why not use M4?
    I don't know if M4 is available for all platforms that are supported
    by Ruby but I know for sure that a C compiler is available for all
    platforms that Ruby runs on.

    Josef 'Jupp' Schugt
    Josef 'Jupp' Schugt Guest

  10. #9

    Default Re: macros

    On Mon, Nov 17, 2003 at 10:27:53AM +0900, Josef 'Jupp' Schugt wrote:
    > * Tim Hunter; Tue, 11 Nov 2003 23:00:39 GMT
    >
    > > On Wed, 12 Nov 2003 03:58:43 +0900, Josef 'Jupp' SCHUGT wrote:
    > > If you're going to go _that_ route [and use the C preprocessor],
    > > why not use M4?
    >
    > I don't know if M4 is available for all platforms that are supported
    > by Ruby but I know for sure that a C compiler is available for all
    > platforms that Ruby runs on.
    it could have been cross-compiled ;-)

    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    Microsoft is not the answer.
    Microsoft is the question.
    NO (or Linux) is the answer.
    -- Taken from a .signature from someone from the UK, source unknown


    Mauricio Fernández Guest

  11. #10

    Default Re: macros

    Hi!

    * Mauricio Fernández; 2003-11-17, 21:48 UTC:
    > On Mon, Nov 17, 2003 at 10:27:53AM +0900, Josef 'Jupp' Schugt wrote:
    > > * Tim Hunter; Tue, 11 Nov 2003 23:00:39 GMT
    > > I don't know if M4 is available for all platforms that are supported
    > > by Ruby but I know for sure that a C compiler is available for all
    > > platforms that Ruby runs on.
    >
    > it could have been cross-compiled ;-)
    [Jupp, sitting at his computer]
    Jupp: Ruby has been compiled so there must be a C compiler.
    [Jupp types above statement]
    Jupp: Waitamoment, Ruby could have been cross-compiled...
    [Jupp utters humming sounds]
    Jupp: No, the statement is correct. Not because there *must* be a
    compiler but because there actually *is* a compiler.

    Do know of a platform that for which Ruby is available but for that
    no C compiler exists (native C compilers for DOS definitely exist, I
    use Pacific C - cute program by the way).

    Josef 'Jupp' Schugt
    --
    .-------.
    message > 100 kB? / | |
    sender = spammer? / | R.I.P.|
    text = spam? / ___| |___

    Josef 'Jupp' SCHUGT Guest

  12. #11

    Default Re: macros

    On Tue, Nov 18, 2003 at 09:04:47AM +0900, Josef 'Jupp' SCHUGT wrote:
    > Hi!
    >
    > * Mauricio Fernández; 2003-11-17, 21:48 UTC:
    > > On Mon, Nov 17, 2003 at 10:27:53AM +0900, Josef 'Jupp' Schugt wrote:
    > > > * Tim Hunter; Tue, 11 Nov 2003 23:00:39 GMT
    > > > I don't know if M4 is available for all platforms that are supported
    > > > by Ruby but I know for sure that a C compiler is available for all
    > > > platforms that Ruby runs on.
    > >
    > > it could have been cross-compiled ;-)
    >
    > Jupp: No, the statement is correct. Not because there *must* be a
    > compiler but because there actually *is* a compiler.
    If there might be no compiler, you cannot know *for sure* ;)
    > Do know of a platform that for which Ruby is available but for that
    > no C compiler exists (native C compilers for DOS definitely exist, I
    > use Pacific C - cute program by the way).
    Don't know any --- but I haven't been exposed to many different
    platforms... such a beast might exist, somewhere.

    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    Microsoft is not the answer.
    Microsoft is the question.
    NO (or Linux) is the answer.
    -- Taken from a .signature from someone from the UK, source unknown


    Mauricio Fernández Guest

  13. #12

    Default Re: macros

    On Wed, Nov 19, 2003 at 03:32:45AM +0900, Mauricio Fernández wrote:
    > On Tue, Nov 18, 2003 at 09:04:47AM +0900, Josef 'Jupp' SCHUGT wrote:
    > If there might be no compiler, you cannot know *for sure* ;)
    >
    > > Do know of a platform that for which Ruby is available but for that
    > > no C compiler exists (native C compilers for DOS definitely exist, I
    > > use Pacific C - cute program by the way).
    DJGPP is another DOS compiler (to be fair, DOS+DPMI). I've seen Ruby
    compiled with it, years ago :-)

    Regards,

    Michael



    Michael Neumann Guest

  14. #13

    Default Re: macros

    Hi,

    Michael Neumann <mneumann@ntecs.de> writes:
    > DJGPP is another DOS compiler (to be fair, DOS+DPMI). I've seen Ruby
    > compiled with it, years ago :-)
    Even now, the djgpp version exists:
    [url]http://ftp.ruby-lang.org/pub/ruby/binaries/djgpp/1.8/[/url]

    --
    eban


    WATANABE Hirofumi Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139