Решение на Трета задача от Йончо Йончев

Обратно към всички решения

Към профила на Йончо Йончев

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 0 неуспешни тест(а)

Код

module RBFS
class File
attr_accessor data
def initialize(data=nil)
@data=data
end
def data_type
case @data
when NilClass then :nil
when String then :string
when TrueClass , FalseClass then :boolean
when Float , Integer then :number
when Symbol then :symbol
end
end
def self.parse(string_data)
key, value = string_data.split(':')
case data_type(@data)
when 'nil' then new(nil)
when 'string' then new(value)
when 'boolean' then new(value == 'true')
when 'symbol' then new(value.to_sym)
when 'number' then Integer(value)
end
end
def serialize
case @data
when NilClass then "nil:"
when String then "string:#{@data}"
when TrueClass , FalseClass then "boolean:#{@data}"
when Symbol then "symbol:#{@data}"
when Integer , Float then "number:#{@data}"
end
end
end
end

Лог от изпълнението

/tmp/d20141111-26053-14ltkv8/solution.rb:4:in `<class:File>': undefined local variable or method `data' for RBFS::File:Class (NameError)
	from /tmp/d20141111-26053-14ltkv8/solution.rb:3:in `<module:RBFS>'
	from /tmp/d20141111-26053-14ltkv8/solution.rb:1:in `<top (required)>'
	from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:867:in `require'
	from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:867:in `block in setup_load_path_and_require'
	from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:867:in `each'
	from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:867:in `setup_load_path_and_require'
	from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/configuration_options.rb:25:in `configure'
	from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:21:in `run'
	from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:80:in `run'
	from /data/rails/evans-2014/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'

История (1 версия и 0 коментара)

Йончо обнови решението на 10.11.2014 16:33 (преди около 10 години)

+module RBFS
+
+ class File
+ attr_accessor data
+
+ def initialize(data=nil)
+ @data=data
+ end
+
+ def data_type
+ case @data
+ when NilClass then :nil
+ when String then :string
+ when TrueClass , FalseClass then :boolean
+ when Float , Integer then :number
+ when Symbol then :symbol
+ end
+ end
+
+ def self.parse(string_data)
+ key, value = string_data.split(':')
+ case data_type(@data)
+ when 'nil' then new(nil)
+ when 'string' then new(value)
+ when 'boolean' then new(value == 'true')
+ when 'symbol' then new(value.to_sym)
+ when 'number' then Integer(value)
+ end
+ end
+
+ def serialize
+ case @data
+ when NilClass then "nil:"
+ when String then "string:#{@data}"
+ when TrueClass , FalseClass then "boolean:#{@data}"
+ when Symbol then "symbol:#{@data}"
+ when Integer , Float then "number:#{@data}"
+ end
+ end
+ end
+end