Sha256: 1b4913660f503eadc74677cb0cad9769fe01ac1b5bfe3b3a6aea0e5da5f16f60
Contents?: true
Size: 1.66 KB
Versions: 1
Compression:
Stored size: 1.66 KB
Contents
# frozen_string_literal: true require 'json' require 'debug' # https://ruby-doc.org/core-3.0.3/File.html # Read JSON file # https://ruby-doc.org/stdlib-3.0.3/libdoc/json/rdoc/JSON.html # Parse JSON content to Hash # https://github.com/ruby/debug #Cargo toda la data de swapi en la variable JSONDATA como hash =begin JSONDATA = JSON.load_file('./swapi.json').each_with_object({}) do |(key, value), hash| hash[key] = value end =end JSONDATA = File.expand_path('swapi.json', __dir__) DATA = JSON.load_file(JSONDATA).each_with_object({}) do |(key, value), hash| hash[key] = value end #En esta clase voy a querer acceder a esa data dependiendo de la key que me pase # y mostrar el atributo que se pida según el método llamado class Person attr_accessor :data def initialize(data) @data = data end def name @data['name'] end def birth_year @data['birth_year'] end def eye_color @data['eye_color'] end def hair_color @data['hair_color'] end def skin_color @data['skin_color'] end def id @data['id'] end def height @data['height'] end def mass @data['mass'] end def gender @data['gender'] end def vehicles def self.all debugger end end class Starship attr_accessor :data def initialize(data) @data = data end def star @data['starship'] end end class Vehicle attr_accessor :data def initialize(data) @data = data end def vehicle @data['vehicles'] end end puts "Hi! I'm Luke Skywalker, I'm your SWAPI helper!"
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
sw_info-0.0.1 | lib/sw_info.rb |