Mass Assignment on Clas

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

I want to be able to do

Vehicle.new(:vtype => 'ECAR', :description => 'Great Car', :vehicle =>
'Ford Focus', :capacity => '10');

Now I could do something like

def initialize(items)
     @vtype = items[:vtype]
     @description = items[:description]
     @vehicle = items[:vehicle]
     @capacity = items[:capacity]
# puts items.inspect
   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!

Many thanks for your assist!

···

--
Posted via http://www.ruby-forum.com/.

Hi --

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

I want to be able to do

Vehicle.new(:vtype => 'ECAR', :description => 'Great Car', :vehicle =>
'Ford Focus', :capacity => '10');

Now I could do something like

def initialize(items)
    @vtype = items[:vtype]
    @description = items[:description]
    @vehicle = items[:vehicle]
    @capacity = items[:capacity]
# puts items.inspect
  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!

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

Pete Moran 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

I want to be able to do

Vehicle.new(:vtype => 'ECAR', :description => 'Great Car', :vehicle =>
'Ford Focus', :capacity => '10');

Now I could do something like

def initialize(items)
     @vtype = items[:vtype]
     @description = items[:description]
     @vehicle = items[:vehicle]
     @capacity = items[:capacity]
# puts items.inspect
   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!

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

x = X.new(:vtype => "Ecar", :description => "Great Car")
puts x.vtype
puts x.description

···

--
Posted via http://www.ruby-forum.com/\.

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.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Just playing devil's advocate, what about using OpenStruct?

require 'ostruct'

class Vehicle < OpenStruct

end

v1 = Vehicle.new(:vtype => 'ECAR', :description => 'Great Car', :vehicle =>
'Ford Focus', :capacity => '10')

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

I want to be able to do

Vehicle.new(:vtype => 'ECAR', :description => 'Great Car', :vehicle =>
'Ford Focus', :capacity => '10');

Now I could do something like

def initialize(items)
@vtype = items[:vtype]
@description = items[:description]
@vehicle = items[:vehicle]
@capacity = items[:capacity]
# puts items.inspect
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!

Many thanks for your assist!

Why do I always see typos just AFTER I hit send??? s/thing //

···

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

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Hi --

···

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 :slight_smile:

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

Actually two.

Balancing parentheses is the hobgoblin of little minds! <G>

···

On Fri, Aug 28, 2009 at 10:13 AM, David A. Black<dblack@rubypal.com> wrote:

Hi --

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 :slight_smile:

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

David A. Black wrote:

Hi --

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 :slight_smile:

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:

--
Posted via http://www.ruby-forum.com/\.

Pete Moran wrote:

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

Yes, as well as the recently published "The Well Grounded Rubyist".

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

David A. Black wrote:

Hi --

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 :slight_smile:

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 :slight_smile: 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:

--
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Ruby/Rails training, mentoring, consulting, code-review
Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2\)

September Ruby training in NJ has been POSTPONED. Details to follow.