I have a class that could look something like this
class MyClass
attr_accessor :x, :y, :z
def math(a, b, c)
end
end
I want to be able to pass in strings to this class and have everything
converted to fixnums (or floats). Do I need to manually do the
setters for x, y, and z and convert a, b, c?
On 8/3/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
Hi,
I have a class that could look something like this
class MyClass
attr_accessor :x, :y, :z
def math(a, b, c)
end
end
I want to be able to pass in strings to this class and have everything
converted to fixnums (or floats). Do I need to manually do the
setters for x, y, and z and convert a, b, c?
Wondering if it would be useful to have functions like
attr_accessor_int :a_int, :another_int
attr_accessor_string :a_string
and then dynamically create the following methods
def a_int=(x); @a_int = x.to_int; end
def another_int=(x); @another_int = x.to_int; end
def a_string=(x); @a_string = x.to_s; end;
···
On 8/3/05, Austin Ziegler <halostatue@gmail.com> wrote:
On 8/3/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> Hi,
>
> I have a class that could look something like this
>
> class MyClass
> attr_accessor :x, :y, :z
>
> def math(a, b, c)
> end
> end
>
> I want to be able to pass in strings to this class and have everything
> converted to fixnums (or floats). Do I need to manually do the
> setters for x, y, and z and convert a, b, c?
On 8/5/05, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
Joe Van Dyk wrote:
> Wondering if it would be useful to have functions like
> attr_accessor_int :a_int, :another_int
> attr_accessor_string :a_string
>
> and then dynamically create the following methods
>
> def a_int=(x); @a_int = x.to_int; end
> def another_int=(x); @another_int = x.to_int; end
> def a_string=(x); @a_string = x.to_s; end;
>
You might want to use #to_i rather than #to_int, so that strings are
converted. Or maybe not, depending on what you want...
irb(main):001:0> "1".to_int
NoMethodError: undefined method `to_int' for "1":String
from (irb):1
irb(main):002:0> "1".to_i
=> 1