Professional Web Applications Themes

two questions - Ruby

Hi all, i'm new to ruby and i find it very exciting, but i've two little question that i cannot find answers for. First: how can i use a variable name within a regex? In other words, i explain it with some perl code: #begin use strict; print "give me a string: "; chomp (my $str = <stdin>); #that's the italian version of mary poppin's #saying (-: my $line = "supercalifragilistichespiralidoso"; print "$1\n" if $line =~ /($str)\w+/; #end Now, as you can see from this stupid code, if i enter "super" as $str, it prints out "super". But because i've ...

  1. #1

    Default two questions

    Hi all, i'm new to ruby and i find it very exciting, but i've two little
    question that i cannot find answers for.

    First: how can i use a variable name within a regex? In other words, i
    explain it with some perl code:

    #begin
    use strict;

    print "give me a string: ";
    chomp (my $str = <stdin>);

    #that's the italian version of mary poppin's
    #saying (-:
    my $line = "supercalifragilistichespiralidoso";
    print "$1\n" if $line =~ /($str)\w+/;
    #end

    Now, as you can see from this stupid code, if i enter "super" as $str,
    it prints out "super".
    But because i've to use the construct #{...} to interpolate a variable
    (in ruby), how can i use the trick??

    Second: this is a question on the object orientation consistency of ruby
    (i'm not doubt that ruby is consistent, but i'm only a newbie that
    search for answers...).
    If i declare an instance variale as attr_accessor, i can avoid to write
    getter/setter methods, but how can i surely give a value to a variable
    without writing the relative method??
    I explain with an example:

    class MyTime
    attr_accessor :hours, :minutes

    def initialize
    ....
    ....
    end

    ...other methods...
    end

    Now, without the setter methods for hours (or minutes) i can assign a
    totally inconsistent value like this:

    t = MyTime.new
    t.minutes = 75 # that make no sense at all!

    when with a setter method i can control the input like this:

    #that's only an example...i know that this method is
    #totally bad!

    def minutes=(value)
    if value > 60
    gap = value - 60
    minutes = gap
    hours = hours + 1
    else
    minutes = value
    end
    end

    How can i avoid this inconsistency without a setter method
    implementation?? attr_accessor is useless in this case? (it's so
    convenient!! (-: )

    I hope that my questions are clear, sorry for my poor english!
    If you have some links that talk about this topics, please tell me!
    Thanks a lot!

    caligari.

    caligari Guest

  2. #2

    Default Re: two questions


    On Feb 4, 2004, at 3:50 PM, caligari wrote:
     

    Interpolating is done the same way, it doesn't matter whether you are
    using strings or regexen. just enclose your variable in #{..} and stick
    it where you want it. Here's how it would go in your example:

    #that's the italian version of mary poppin's
    #saying (-:
    my $line = "supercalifragilistichespiralidoso";
    print "$1\n" if $line =~ /(#{$str})\w+/;
    #end

     

    Well... No. The only way you can check the validity of the data being
    set by the setter is to write your own setter method.

    Cheers,
    Mark



    Mark Guest

  3. #3

    Default Re: two questions

    il Wed, 04 Feb 2004 23:49:02 GMT, caligari <it> ha
    scritto::
     

    ciao :)
     

    dunno perl, but possibly you mean something like this: [/ref]
    => "spira" [/ref]
    yes
    => nil

    the #{stuff} is what is used in ruby for strings interpolation, but
    works in regexen too :)


     

    you can't.
    attr_* avoids you to write
    def myattr
    myattr
    end
    or
    def myattr=(x)
    myattr=x
    end

    that you could be forced to write cause in ruby every instance var is
    private.
    But I'm afraid you still need to tell ruby how to check something if
    you actually need that.
    gabriele Guest

  4. #4

    Default Re: two questions

     
    >
    > Well... No. The only way you can check the validity of the data being
    > set by the setter is to write your own setter method.[/ref]

    In the case of hours and minutes, I might do something like this:

    class Time
    attr_accessor :hours, :minutes
    def hours=(h)
    hours = h
    _validate
    end
    def minutes=(m)
    minutes = m
    _validate
    end
    def _validate
    # raise error if values are outside range
    end
    end

    That way, you're advertising the basic fact that hours and minutes are
    'attributes' (which is good for RDoc output, for example), but you're also
    getting the behaviour you want.

    In general, though, I prefer to make objects set-once, read-only. That
    way, you are more assured of their correctness. That's a programming
    practice you may want to consider.

    class Time
    attr_reader :hours, :minutes
    def initialize(h, m)
    hours, minutes = h, m
    _validate
    end
    def _validate, etc. ...
    end

    Cheers,
    Gavin




    Gavin Guest

  5. #5

    Default Re: two questions

    Hi Caligari,
     

    Basically, you can treat the regex source like a normal string. Like
    so:

    irb(main):001:0> str = "supercali"
    => "supercali"
    irb(main):002:0> sub = 'erca'
    => "erca"
    irb(main):003:0> str =~ /#{ sub }/
    => 3

    (BTW, irb is a good quick way to test out a lot of these little
    questions. Many times you'll be surprised by how Ruby just plain works
    the way you think it should!)
     

    Unfortunately, I don't believe there's any way to get around the fact
    that you'll need some sort of validation or processing after the
    minutes value is set. For what it's worth, you could use attr_reader
    to auto-create the reader and then write the setter by hand.

    Francis
    Francis Guest

Similar Threads

  1. New-to-MX questions
    By kennethp in forum Macromedia Freehand
    Replies: 1
    Last Post: February 11th, 09:17 PM
  2. () questions
    By Matija in forum PERL Miscellaneous
    Replies: 18
    Last Post: September 26th, 04:57 AM
  3. Questions about QT
    By Michael Uman in forum Mac Programming
    Replies: 1
    Last Post: September 1st, 07:30 PM
  4. 2 questions :) - portal questions
    By John Weinshel in forum FileMaker
    Replies: 3
    Last Post: July 14th, 04:37 AM
  5. MSN & XP Questions
    By GT in forum Windows XP/2000/ME
    Replies: 1
    Last Post: July 12th, 11:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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