Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
How to convert laravel array to ruby hash access

I have an array that store some data like this and this is the exact input I'm using

{
    "accepted" => {
        "number_of_order" => {
            "condition" => "between_number", "value" => "0&5"
        }
    }, "removed" => {
        
    }
}

Now I'm trying to convert this array into ruby hash access like this and this is my expected output it's not what I'm getting now

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    condition: between_number
    value: 0&5
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}

But I'm unable to convert the array in ruby hash access. I also try YAML dump Yaml::dump($array); to convert into YAML then ruby hash access but nothing works.

Basically, I'm trying to do reverse of this question

How to get data from ruby hash access to laravel 8?

You can refer above link I convert this ruby hash into a laravel array but now I also wanted to convert this array into ruby hash.

My current code for converting an array to a ruby hash

$jsonEncoded = '{"accepted":{"number_of_order":{"consition":"between_number","value":"0&5"}}}';

$array = json_decode($jsonEncoded, true);

dump(Yaml::dump($array));

My current code for converting ruby hash to array

$filter_data = str_replace("!ruby/hash:ActiveSupport::HashWithIndifferentAccess", "", $filter_data);
$yamlContents = Yaml::parse($filter_data);
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

To convert hash to yaml in ruby we can use yaml library

require 'yaml'

data = {
    "accepted" => {
        "number_of_order" => {
            "condition" => "between_number", "value" => "0&5"
        }
    }, "removed" => {
        
    }
}

puts data.to_yaml

To convert associated array to yaml in php we can use Symfony Yaml

use Symfony\Component\Yaml\Yaml;

$jsonEncoded = '{"accepted":{"number_of_order":{"consition":"between_number","value":"0&5"}}}';

$array = json_decode($jsonEncoded, true);

print Yaml::dump($array);

To load php output yaml in ruby same yaml library can be used

require 'yaml'
yaml_str = ... # output generate from Symfony Yaml::dump
yaml_hash = YAML.load(yaml_str)

To load ruby output yaml in php we can use the same Symfony Yaml

$yaml_str = ...; # output generate from ruby rhash.to_yaml
$yaml_associated_array = Yaml.parse(yaml_str);

Load ruby hash to php

$data = "
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    condition: between_number
    value: 0&5
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}
";

print $data;

$data = str_replace("---", "", preg_replace("/!ruby.*Access/", "", $data));
print $data;

$result = Yaml::parse($data);
var_dump($result);

Same can be achieved in ruby

data = %Q(
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    condition: between_number
    value: 0&5
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}
)

result = str.gsub(/!ruby.*Access/, '').gsub("--- ", "")

puts result

puts YAML.load(result)

Note: We can also use other libraries

over 4 years ago · Santiago Trujillo Relatório

0

I just found the answer to my queustion and I'm sharing it with you all.

Laravel Code:

$jsonEncoded = '{"accepted":{"first_order_number":{"consition":"between_date","value":"2021-11-28 00:00:00&2021-11-22 00:00:00"}},"removed":{}}';
    
$process = new Process(['ruby', 'convert_hashActiveSupport.rb'],null,null, $jsonEncoded);

$process->run();
    
// executes after the command finishes
if (!$process->isSuccessful()) {
   throw new ProcessFailedException($process);
}
    
$data = $process->getOutput();

Ruby Code:

require 'yaml'
require 'json'
require "active_support/core_ext/hash/indifferent_access"

yaml_str = "#{ ARGF.read }"

parsed_output = JSON.parse(yaml_str)
yaml_output   = parsed_output.with_indifferent_access.to_yaml
puts yaml_output

Package required in laravel:

composer require symfony/process

Packages required in ruby:

gem install activesupport
gem install 'yaml'

And here I'm getting my expected output:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  first_order_date: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    consition: between_date
    value: 2021-11-28 00:00:00&2021-11-22 00:00:00
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda