another addition to tomslib/rubylib:
class Module
def attr_cast(names_cast)
names_cast.each { |name, cast|
class_eval <<-EOS
def #{name}
@#{name}
end
def #{name}=(arg)
@#{name} = arg.#{cast}
end
EOS
}
end
end
try it out:
attr_cast :avar => :to_s
will create:
def avar
@avar
end
def avar=(arg)
@avar=arg.to_s
end
enjoy,
-transami
Wow, that’s cool. But, should it be named somthing like:
attr_accessor_cast
or
attr_accessor_with_cast
···
On Monday, 6 January 2003 at 10:30:54 +0900, Tom Sawyer wrote:
another addition to tomslib/rubylib:
class Module
def attr_cast(names_cast)
names_cast.each { |name, cast|
class_eval <<-EOS
def #{name}
@#{name}
end
def #{name}=(arg)
@#{name} = arg.#{cast}
end
EOS
}
end
end
–
Jim Freeze
The meta-Turing test counts a thing as intelligent if it seeks to
devise and apply Turing tests to objects of its own creation.
– Lew Mammel, Jr.
thanks, Jim. it is esspecially usefull for cgi apps, since all the cgi
parameters come in as strings. you are probably right about the name although
it seems so long winded to say ‘attr_accessor_with_cast’ but its not so bad
since you can do things like:
att_accessor_with_cast :a1 => :to_s,
:a2 => :to_i,
:a3 => :to_f
which of your two suggestions go you think best? and i will change it to that.
-transami
···
On Sunday 05 January 2003 07:20 pm, Jim Freeze wrote:
Wow, that’s cool. But, should it be named somthing like:
attr_accessor_cast
or
attr_accessor_with_cast
Those both being annoyingly long, I would prefer an implementation
that allowed:
attr_accessor :x, :y, :name => :to_s, :age => :to_i
That is, :x and :y receive the normal attr_accessor treatment, while
:name and :age get casting as well.
You wouldn’t be able to put “non-cast” arguments after “cast”
arguments, because, well, that’s the way Ruby inline hash arguments
work, but that draws no complaint from me.
Gavin
···
On Monday, January 6, 2003, 1:20:06 PM, Jim wrote:
Wow, that’s cool. But, should it be named somthing like:
attr_accessor_cast
or
attr_accessor_with_cast
yea, that works well. unless there’s some unforseen wrong doing here i’ll
modify my code to work as you suggest. no need for another method at all!
very nice.
hmm…just occured to me, how will the code handle it if there are only hash
arguments? in other words i’m not so sure what the methods args should be.
suggestion?
thanks,
-transami
···
On Sunday 05 January 2003 09:30 pm, Gavin Sinclair wrote:
Those both being annoyingly long, I would prefer an implementation
that allowed:
attr_accessor :x, :y, :name => :to_s, :age => :to_i
That is, :x and :y receive the normal attr_accessor treatment, while
:name and :age get casting as well.
You wouldn’t be able to put “non-cast” arguments after “cast”
arguments, because, well, that’s the way Ruby inline hash arguments
work, but that draws no complaint from me.
Those both being annoyingly long, I would prefer an implementation
that allowed:
attr_accessor :x, :y, :name => :to_s, :age => :to_i
That is, :x and :y receive the normal attr_accessor treatment, while
:name and :age get casting as well.
I think adding this functionality to attr_accessor is ok, but I would
go ahead and alias attr_accessor_with_cast as well.
hmm…just occured to me, how will the code handle it if there are only hash
arguments? in other words i’m not so sure what the methods args should be.
suggestion?
def f(*a)
p a
end
f(5,6,1=>2) #=> [5, 6, {1=>2}]
f(5,6,1=>2,3=>4) #=> [5, 6, {1=>2, 3=>4}]
f(1=>2,3=>4,5,6) #=> SyntaxError: compile error
···
On Monday, 6 January 2003 at 17:48:18 +0900, Tom Sawyer wrote:
On Sunday 05 January 2003 09:30 pm, Gavin Sinclair wrote:
–
Jim Freeze
Better dead than mellow.
It will handle fine. This (untested) is like a skeleton:
def attr_accessor(*args)
args.each do |arg|
if arg.is_a Hash
arg.each do |name, cast|
# Create accessor methods with ‘name’ and ‘cast’
end
else
# Create normal accessor methods
end
end
end
Naturally, you need attr_reader and attr_writer to work as intended,
so they are the building blocks for attr_accessor.
Muck around in irb or with some small programs to see for yourself
how optional hashes are processed, for example the following
invocation:
foo 1, 2, ‘a’ => 5, ‘b’ => -7, :x => :y
Gavin
···
On Monday, January 6, 2003, 7:48:18 PM, Tom wrote:
attr_accessor :x, :y, :name => :to_s, :age => :to_i
yea, that works well. unless there’s some unforseen wrong doing here i’ll
modify my code to work as you suggest. no need for another method at all!
very nice.
hmm…just occured to me, how will the code handle it if there are only hash
arguments? in other words i’m not so sure what the methods args should be.
suggestion?
thanks for the starter code
so should attr_accessor call on attr_reader and attr_writer? also i am
curoius, where can i find out exactly what attr_reader, writter, and accessor
currently do?
···
On Monday 06 January 2003 04:55 am, Gavin Sinclair wrote:
Naturally, you need attr_reader and attr_writer to work as intended,
so they are the building blocks for attr_accessor.
–
tom sawyer, aka transami
transami@transami.net
.''.
.''. . *''* :_\/_: .
:_\/_: _\(/_ .:.*_\/_* : /\ : .'.:.'.
.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | … : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.
Naturally, you need attr_reader and attr_writer to work as intended,
so they are the building blocks for attr_accessor.
thanks for the starter code
so should attr_accessor call on attr_reader and attr_writer? also i am
I think that would be the ‘correct’ way to do it. That way, you don’t
violate the DRY principle (Don’t Repeat Yourself).
curoius, where can i find out exactly what attr_reader, writter, and accessor
currently do?
The code Gavin supplied is pretty much all there is.
You can google ruby-talk with
+“class Module” +attr_reader
and find several examples.
···
On Tuesday, 7 January 2003 at 10:06:22 +0900, Tom Sawyer wrote:
On Monday 06 January 2003 04:55 am, Gavin Sinclair wrote:
–
Jim Freeze
here you go, in full! (by the way the old attr_accessor does not depend on
attr_reader or attr_writer. go figure. this new one does.) let me know if you
see any problems with this.
alias attr_reader_basic attr_reader
def attr_reader(*args)
if args.last.kind_of?(Hash) or args.last.kind_of?(Array)
attr_reader_with_cast *(args.pop)
end
attr_reader_basic *args
end
def attr_reader_with_cast(*args)
args.each { |name, cast|
class_eval <<-EOS
def #{name}()
@#{name}.#{cast}
end
EOS
}
end
alias attr_writer_basic attr_writer
def attr_writer(*args)
if args.last.kind_of?(Hash) or args.last.kind_of?(Array)
attr_writer_with_cast *(args.pop)
end
attr_writer_basic *args
end
def attr_writer_with_cast(*args)
args.each { |name, cast|
class_eval <<-EOS
def #{name}=(arg)
@#{name} = arg.#{cast}
end
EOS
}
end
alias attr_accessor_basic attr_accessor
def attr_accessor(*args)
attr_reader *args
attr_writer *args
end
def attr_accessor_with_cast(*args)
attr_writer_with_cast *args
attr_reader_with_cast *args
end
-transami
THERE SEEMS TO BE A BUG. HAVEN’T FIGURED IT OUT YET THOUGH. ANYONE SEE IT?
···
On Monday 06 January 2003 11:00 pm, Tom Sawyer wrote:
here you go, in full! (by the way the old attr_accessor does not depend on
attr_reader or attr_writer. go figure. this new one does.) let me know if
you see any problems with this.
alias attr_reader_basic attr_reader
def attr_reader(*args)
if args.last.kind_of?(Hash) or args.last.kind_of?(Array)
attr_reader_with_cast *(args.pop)
end
attr_reader_basic *args
end
def attr_reader_with_cast(*args)
args.each { |name, cast|
class_eval <<-EOS
def #{name}()
@#{name}.#{cast}
end
EOS
}
end
alias attr_writer_basic attr_writer
def attr_writer(*args)
if args.last.kind_of?(Hash) or args.last.kind_of?(Array)
attr_writer_with_cast *(args.pop)
end
attr_writer_basic *args
end
def attr_writer_with_cast(*args)
args.each { |name, cast|
class_eval <<-EOS
def #{name}=(arg)
@#{name} = arg.#{cast}
end
EOS
}
end
alias attr_accessor_basic attr_accessor
def attr_accessor(*args)
attr_reader *args
attr_writer *args
end
def attr_accessor_with_cast(*args)
attr_writer_with_cast *args
attr_reader_with_cast *args
end
-transami
–
tom sawyer, aka transami
transami@transami.net
.''.
.''. . *''* :_\/_: .
:_\/_: _\(/_ .:.*_\/_* : /\ : .'.:.'.
.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | … : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.
All fixed. new code. (ever so slight changes):
alias attr_reader_basic attr_reader
alias attr_writer_basic attr_writer
alias attr_accessor_basic attr_accessor
def attr_reader(*args)
if args.last.kind_of?(Hash) or args.last.kind_of?(Array)
attr_reader_with_cast(args.pop)
end
attr_reader_basic(*args)
end
def attr_reader_with_cast(harg)
harg.each { |name, cast|
class_eval <<-EOS
def #{name}()
@#{name}.#{cast}
end
EOS
}
end
def attr_writer(*args)
if args.last.kind_of?(Hash) or args.last.kind_of?(Array)
attr_writer_with_cast(args.pop)
end
attr_writer_basic(*args)
end
def attr_writer_with_cast(harg)
harg.each { |name, cast|
class_eval <<-EOS
def #{name}=(arg)
@#{name} = arg.#{cast}
end
EOS
}
end
def attr_accessor(*args)
attr_reader(*args)
attr_writer(*args)
end
def attr_accessor_with_cast(harg)
attr_reader_with_cast(harg)
attr_writer_with_cast(harg)
end
Is this logically right? You;d want different casts for readers and
writers, I’d think.
martin
···
Tom Sawyer transami@transami.net wrote:
def attr_accessor_with_cast(harg)
attr_reader_with_cast(harg)
attr_writer_with_cast(harg)
end
What happens when harg is an Array?
···
On Tue, Jan 07, 2003 at 03:16:40PM +0900, Tom Sawyer wrote:
All fixed. new code. (ever so slight changes):
alias attr_reader_basic attr_reader
alias attr_writer_basic attr_writer
alias attr_accessor_basic attr_accessor
def attr_reader(*args)
if args.last.kind_of?(Hash) or args.last.kind_of?(Array)
attr_reader_with_cast(args.pop)
end
attr_reader_basic(*args)
end
def attr_reader_with_cast(harg)
harg.each { |name, cast|
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Q: What’s the big deal about rm, I have been deleting stuff for years? And
never lost anything… oops!
A: …
– From the Frequently Unasked Questions
perhaps that should be removed. i had thought one could do this:
attr_accessor [ [:avar, :to_s], ...]
but that’s rather pointless isn’t it? i had only thought of allowing it b/c
the same code works with it as does for Hash, i.e. harg.each { |name, cast|
… }
···
On Tuesday 07 January 2003 01:14 am, Mauricio Fernández wrote:
def attr_reader(*args)
if args.last.kind_of?(Hash) or args.last.kind_of?(Array)
attr_reader_with_cast(args.pop)
end
attr_reader_basic(*args)
end
def attr_reader_with_cast(harg)
harg.each { |name, cast|
What happens when harg is an Array?
–
tom sawyer, aka transami
transami@transami.net
.''.
.''. . *''* :_\/_: .
:_\/_: _\(/_ .:.*_\/_* : /\ : .'.:.'.
.‘’.: /\ : ./)\ ‘:’* /\ * : ‘…’. -=:o:=-
:/:‘.:::. | ’ ‘’ * ‘.'/.’ (/’.‘:’.’
: /\ : ::::: = / -= o =- /)\ ’ *
‘…’ ‘:::’ === * /\ * .‘/.'. ‘._____
* | … : |. |’ .—"|
* | _ .–’| || | _| |
* | .-‘| __ | | | || |
.-----. | |’ | || | | | | | || |
__’ ’ /“\ | '-.”". ‘-’ ‘-.’ '` |.
It’s more error-prone than ‘=>’: even though it represents the same
number of keypresses, it’s anyway another ‘[’ to match…
At any rate, w/ or w/o Array, this casting attr_reader is great!
···
On Tue, Jan 07, 2003 at 08:51:58PM +0900, Tom Sawyer wrote:
On Tuesday 07 January 2003 01:14 am, Mauricio Fernández wrote:
def attr_reader(*args)
if args.last.kind_of?(Hash) or args.last.kind_of?(Array)
attr_reader_with_cast(args.pop)
end
attr_reader_basic(*args)
end
def attr_reader_with_cast(harg)
harg.each { |name, cast|
What happens when harg is an Array?
perhaps that should be removed. i had thought one could do this:
attr_accessor [ [:avar, :to_s], …]
but that’s rather pointless isn’t it? i had only thought of allowing it b/c
the same code works with it as does for Hash, i.e. harg.each { |name, cast|
… }
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Not only Guinness - Linux is good for you, too.
– Banzai on IRC