From: "Jesús Gabriel y Galán" Date: 2010-06-02T23:46:48+09:00 Subject: Re: having a class method called only one time ? 2010/6/2 Une Bévue : > Une Bévue wrote: > >> Array HOST_IP = [] # the array let me make the constant "mutable" > > it seems to work like that : > > class HFSFile > >  HOST_IP = [] > >  ... > >  def host_ip >    return HOST_IP.first unless HOST_IP.empty? >    HOST_IP << host_ip_get >    return @h[:host_ip] >  end > >  ... > >  private > >  def host_ip_get >    $stderr.puts "'host_ip_get' was called!" # called 1 time for 2 > instantiations. >    @h[:host_ip] = '' >    while @h[:host_ip].empty? >      @h[:host_ip] = `curl -s http://whatismyip.org`.chomp >    end >    return @h[:host_ip] >  end > >  ... > > end > -- I think it looks cleaner using the idiom Tony described: class HFSFile def host_ip @host_ip ||= host_ip_get end private def host_ip_get $stderr.puts "'host_ip_get' was called!" # called 1 time for 2 instantiations. host_ip = '' while host_ip.empty? host_ip = `curl -s http://whatismyip.org`.chomp end host_ip end end Jesus.