Ruby Class setter -


i have difficulties understanding setter , how apply value in use.

in below example,

  def time_string=(seconds)     @time_string = calculator(seconds)   end 

how can seconds' value setter?

thanks lot

class timer   def seconds     @seconds = 0   end    def time_string     @time_string   end    def seconds=(t)     @seconds = t   end    def time_string=(seconds)     @time_string = calculator(seconds)   end    def calculator(x)     array = []     sec = (x % 60).to_s     min = ((x / 60 ) % 60).to_s     hour = (x / 3600).to_s     temp = [hour, min, sec]     temp.map {|i| i.length < 2 ? array.push("0#{i}") : array.push("#{i}") }     array.join(":")  end  end 

i think you're trying accomplish. first, calculate code...why? why not use methods in time class? second, there not such thing setters in ruby. in ruby method. technically, call setter method acts setter. in code, both methods seconds= , seconds act setter (the first sets @seconds return value of calculator call, , second method sets @seconds 0).

you can't seconds value setter method, purpose of setter method is, after all, set something. if want something, use getter method. , it's pretty simple create getter method, change seconds method this:

def seconds      @seconds # value of @seconds now, don't set 0 end 

ruby can create method automatically in background 1 line added inside class, , line attr_reader :seconds. each time see in ruby code, assume ruby in background generate similar seconds method above. in same fashion, if want ruby automatically generate setter method code this:

def seconds=(t)     @seconds = t end 

then use attr_writer :seconds. these attr_reader , attr_writer methods exist because making setter , getter methods common shortens program significantly, if have, say, 10 instance variables (you have @seconds , @time_string , have instance variables minutes, hours, days, etc. lot of methods getting/setting instance variables!). in same fashion, create getter method automatically @time_string well:

def time_string     @time_string end 

but not setter method because set logic @time_string kinda different method ruby create (attr_reader , attr_writer create simple getter/setter methods), if type attr_writer :time_string, ruby create method in background:

def time_string=(some_value)   @time_string = some_value end 

which not want. can simplify code lot, here's version:

class timer   attr_reader :seconds, :time_string   attr_writer :seconds    def time_string=(seconds)     @time_string = calculator(seconds)   end    def calculator(x)     time.at(x).utc.strftime("%h:%m:%s")   end  end  = timer.new a.seconds = 30 p a.seconds #=> 30 a.time_string = 50 p a.time_string #=> "00:00:50" 

you further simplify code using attr_accessor seconds, recommend this excellent answer explains more these shortcuts.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -