[Ruby] Active Record and Array error

Victor Cosby victorcosby at gmail.com
Thu Jun 21 14:45:51 PDT 2007


Ryan,

I agree with everything about the design and idiom comments, but in
your solution won't the index be mismatched if you've rejected one or
more owners? For example, if owner1 is nil you would get
Owner1=<owner2>.

Perhaps...

i=0
[owner1, owner2, owner3].map{|o| i+=1; (o.nil? or o.strip.empty?) ?
nil : "Owner#{i}\=#{o}"}.compact

Or if you're in Rails you can use blank? to test for nil, empty
strings, or white space only strings.

[owner1, owner2, owner3].map{|o| i+=1; o.blank? ? nil :
"Owner#{i}\=#{o}" }.compact

-Victor

On 6/21/07, Ryan Davis <ryand-ruby at zenspider.com> wrote:
>
> On Jun 21, 2007, at 11:55 , Neil Moomey wrote:
>
> >     owner_array << "Owner3="+self.Owner3 unless self.Owner3.nil? ||
> > self.Owner3.strip == ""
>
> ...
>
> > 60:              <%  @anchorage_prop.owners.each do |q| %>
>
> Array#<< returns self, but if your last conditional doesn't trigger,
> it'll return nil. Return your array explictly.
>
> Better:
>
> def owners
>    owners = [owner1, owner2, owner3].reject { |o| o.nil? or
> o.strip.empty? }
>    i = 0
>    owners.map { |o| i += 1; "Owner#{i}=#{o}" }
> end
>
> Also, stylistically, you're writing Java or C# code. Stick to ruby
> naming/casing idioms: self.owner1
>
> Finally, I'm gonna guess you have a design problem. Owner1-3 has bad
> design smell (esp being strings). You'd probably be better off
> normalizing a bit more.
> _______________________________________________
> Ruby at zenspider.com - Seattle.rb non-commercial list
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby
>


More information about the Ruby mailing list