Implications of implementing to_int and to_str in Ruby

I has a class that provides a string value and an int value (exit code and command exit, respectively). In addition to exhibiting through to_sand to_i, I also use to_strand to_int, for example:

class Status
  def to_s
    @output
  end
  alias :to_str :to_s

  def to_i
    @status.exitstatus
  end
  alias :to_int :to_i
end

My idea is to be able to use this object in as many situations as possible. provided that it can be bound to a string or int, this increases usability. For example, I can combine an object with a string:

a_string = "Output was: " + results

(I wanted to use this as an example of int-enforcement, but Fixnum. + Doesn't like it, so it actually doesn't work :)

an_int = 1 + results

, , , , , "". : " to_s/to_i, /int, to_str/to_int, /int ".

, "" int. :

  • /. : String. + Status , Status.to_str.
  • , . (.. , ) , , , . ( "do" " /int ".)
  • " /int " . , , Float.to_int . , , , to_int . , , : Float ( ), "" . Float ( ), , ( "" , ).

, : - (.. ) to_str to_int?


: W , -. : to_str/to_int, to_s/to_i? ( , to_str to_s)

, Jörg Array.join to_s, to_str. ? Array.join separator.to_s , (: , ..) . Ruby ?

+5
2

/. : String#+ Status , Status#to_str.

, Ruby. :

a_string = "Output was: #{results}"

& trade;, - to_s .

, . (.. , ) , , , . ( "do" " /int ".)

, " /int ", . IOW: ", " - " /int " - .

, "Status IS-A Integer" ( to_int), , , , . " 42 "? ? ?

" ". #to_s. , OTOH, , , .

" /int " . , , Float#to_int . , , , to_int . , , : Float ( ), "" . Float ( ), , ( "" , ).

, , : .

, , , , . "Keine Gleichheit im Unrecht" ( " " ). , Equaliy, , . , OJ .

, , Ruby (, , ), , : -)

Float#to_int . Float Integer. , .. Integer#to_float , : Ruby, Integer , Float . Fixnum#to_float, , Integer Fixnum BigInteger , , , #to_float " " .

, to_x to_xyz, Array#join: , . , to_s to_str . , to_s to_str , .

( Float#to_int , .)


: Ruby #coerce. , , 1 + a_status , Status#coerce. , , .

+8

, , - Status, , .

, :

class Status

  ...

  def message
    @output
  end

  def exit_status
    @status.exitstatus
  end

end

a_string = "Output was: " + results.message
a_int    = 1 + results.exit_status

, , IMHO.

+4

All Articles