But I was wondering if there was a more generic way of doing this
without specifically defining each variable in initialize? I think
ActiveRecord does something similar!
But I was wondering if there was a more generic way of doing this
without specifically defining each variable in initialize? I think
ActiveRecord does something similar!
How about:
def initialize(items)
items.each do |key,value|
instance_variable_set("@#{key}", value)
end
end
(possible with some checking to make sure they're what you want).
David
···
On Fri, 28 Aug 2009, Pete Moran wrote:
--
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Q: What's the best way to get a really solid knowledge of Ruby?
A: Come to our Ruby training in Edison, New Jersey, September 14-17!
Instructors: David A. Black and Erik Kastner
More info and registration: http://rubyurl.com/vmzN
But I was wondering if there was a more generic way of doing this
without specifically defining each variable in initialize? I think
ActiveRecord does something similar!
Many thanks for your assist!
class X
attr_reader :vtype, :description
def initialize(items)
self.vtype = items[:vtype]
self.description = items[:description]
end
def vtype=(val)
if ["Ecar", "Fcar", "Gcar"].include? val @vtype = val
else
puts "Error" @vtype = nil
end
end
def description=(descr)
if descr.length > 20
puts "Error"
else @description = descr
end
end
end
Averting my eyes, I'd prefer to keep my clothes on myself! In other
words I'll grin and BEAR it! <G>
Seriously, David Black already gave you pretty much the response I
would have, although my first steps in refining it a bit might be to
make the hash arg optional:
def initialize(items={}
items.each do |key,value|
instance_variable_set("@#{key}", value)
end
end
Another thing variation would be to use those accessors so as to not
add 'accidental' instance variables
def initialize(items={}
items.each do |key,value|
send("#{key}=", value) rescue raise "unknown attribute #{key}"
end
end
And as the King of Siam said, etcetera, etcetera, etcetera!
···
On Fri, Aug 28, 2009 at 7:03 AM, Pete Moran<pete@zoborg.com> wrote:
I am a total newbie to Ruby, so please bare with me.
I guess the concern would be the possibility of adding additional
accessors. Using attr_accessor inside the class seems to keep me from
updating the variables.
···
On Fri, Aug 28, 2009 at 6:03 AM, Pete Moran<pete@zoborg.com> wrote:
Hi there,
I am a total newbie to Ruby, so please bare with me.
I want to be able to be able to initialise a Class with a hash. For
instance If I have the class
class Vehicle
attr_accessor :vtype, :description, :vehicle, :capacity
end
But I was wondering if there was a more generic way of doing this
without specifically defining each variable in initialize? I think
ActiveRecord does something similar!
On Fri, Aug 28, 2009 at 10:06 AM, Rick DeNatale<rick.denatale@gmail.com> wrote:
Another thing variation would be to use those accessors so as to not
add 'accidental' instance variables
Why do I always see typos just AFTER I hit send??? s/thing //
Run your code through Ruby and you'll see another
David
--
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Q: What's the best way to get a really solid knowledge of Ruby?
A: Come to our Ruby training in Edison, New Jersey, September 14-17!
Instructors: David A. Black and Erik Kastner
More info and registration: http://rubyurl.com/vmzN
Another thing variation would be to use those accessors so as to not
add 'accidental' instance variables
Why do I always see typos just AFTER I hit send??? s/thing //
Run your code through Ruby and you'll see another
David
Thanks for your responses - much appreciated!
Is this the David A. Black who wrote Ruby for Rails? if so wow a real
honour! I recently finished your book (and enjoyed it).
Coming from a Perl backgrounds its been interesting getting up to speed
with Ruby/Rails although I think I am just at the beginning of the
journey.. so much to learn!
···
On Fri, 28 Aug 2009, Rick DeNatale wrote:
On Fri, Aug 28, 2009 at 10:06 AM, Rick DeNatale<rick.denatale@gmail.com> wrote:
Another thing variation would be to use those accessors so as to not
add 'accidental' instance variables
Why do I always see typos just AFTER I hit send??? s/thing //
Run your code through Ruby and you'll see another
David
Thanks for your responses - much appreciated!
Is this the David A. Black who wrote Ruby for Rails? if so wow a real
honour! I recently finished your book (and enjoyed it).
The hono(u)r is all mine I'm glad you liked the book. It has been,
to use the only word I've been able to think of to describe the
process, "repurposed" into a just-Ruby book, The Well-Grounded
Rubyist, which you might also like.
Coming from a Perl backgrounds its been interesting getting up to speed
with Ruby/Rails although I think I am just at the beginning of the
journey.. so much to learn!
I'm not at the beginning of the journey but after the beginning it's
all middle -- so I'm somewhere in the middle, and enjoying it greatly.
David
···
On Fri, 28 Aug 2009, Pete Moran wrote:
On Fri, 28 Aug 2009, Rick DeNatale wrote:
On Fri, Aug 28, 2009 at 10:06 AM, Rick DeNatale<rick.denatale@gmail.com> wrote: