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 = );

#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.

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 = );

#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??

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

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??

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

···

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

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.

ciao :slight_smile:

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

dunno perl, but possibly you mean something like this:

a=‘spira’
=> “spira”
puts ‘yes’ if ‘supercalifragilistichespiralidoso’=~ /#{a}/
yes
=> nil

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

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??

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.

···

il Wed, 04 Feb 2004 23:49:02 GMT, caligari il_piccione@blu.it ha scritto::

Hi Caligari,

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

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!)

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??

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

First: how can i use a variable name within a regex?

Use the #{var} syntax.

myvar = “a noun”
mysentence = “This is a noun”

puts “true” if ( mysentence =~ /#{myvar}/ ) #returns true

HTH,

Zach

···

Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.576 / Virus Database: 365 - Release Date: 1/30/2004

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??

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.

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