I mean
some: true
vs
:some => true
I have problem with compatibility my Rails version and Ruby version and I have to know in which version appeared only : instead =>.
I don't know how to find this kind of info by Google.
This is a feature introduced into Ruby 1.9:
{ example: 'key' }
# => { :example => 'key' }
This is similar to how JavaScript and other languages define their dictionary-type structures. The keys generated this way are always Symbol-type.
It's also possible to mix and match:
variable = :foo
{ example: 'key', 'string' => 'stored', variable => 'thing' }
# => {:example=>"key", "string"=>"stored", :foo=>"thing"}
This is a good thing because the x: approach is more limited. If you want dots in your keys, for example, you'll need to use the older style.
Related
Just wondering if something like:
# frozen_string_literal: true
exists but for Array and Hash?
The goal is not having to .freeze every single of those within the same globals file.
I didn't find any library that monkey patches default ruby classes like Array or Hash. But I found an interesting gem immutable-ruby that may fit your needs
Simple example
require "immutable/hash"
person = Immutable::Hash[name: "Simon", gender: :male]
# => Immutable::Hash[:name => "Simon", :gender => :male]
and you cannot just modify values of it, cause it is immutable. You can perform some actions on that hash, but new copy will be returned to you
friend = person.put(:name, "James") # => Immutable::Hash[:name => "James", :gender => :male]
person # => Immutable::Hash[:name => "Simon", :gender => :male]
friend[:name] # => "James"
person[:name] # => "Simon"
Found a way to handle it without using another gem using only vscode and rubocop :
Install the rubocop extension on vscode
Open your .vscode/settings.json
Append those rules :
{
"editor.formatOnSave": true,
"editor.formatOnSaveTimeout": 5000,
"ruby.format": "rubocop"
}
save
enjoy
Thanks to Tom Lord for the hint.
So I have this complicated XML and want it to parse it to array and send by Savon to the server. The question is how can I parse parameters?
<soapenv:Header>
<add:MessageID
xmlns:add="http://www.w3.org">sdhuf78dd67-8932
</add:MessageID>
<add:Action
xmlns:add="http://www.w3.org/2005">http://google/FMP
</add:Action>
<add:To
xmlns:add="http://www.w3.org/2005/08/addressing">https://no1.testbla.com/1HAD9ANA1
</add:To>
<link:TransactionFlowLink
xmlns:link="http://google/2010/06/"/>
<oas:Security
xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<oas:UsernameToken oas1:Id="UsernameToken-1"
xmlns:oas1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<oas:Username>AHOJHOLA</oas:Username>
<oas:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">Nonce</oas:Nonce>
<oas:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">HashedPassword</oas:Password>
<oas1:Created>CurrentGMTDateAndTime</oas1:Created>
</oas:UsernameToken>
</oas:Security>
<AMA_SecurityHostedUser
xmlns="http://xml.amfds.com/2010/06">
<UserID AgentDutyCode="DA" RequestorType="BO" PseudoCityCode="HIATRA67" POS_Type="5"/>
</AMA_SecurityHostedUser>
</soapenv:Header>
I know how to parse for example add:Action without parameter:
"add:Action" => "http://google/FMP"
And I know that parameter should be written with # prefix.
But I dont know how to write it together. Is this right?
"add:Action" => {
"#xmlns:add" => "http://www.w3.org/2005",
"http://google/FMP"
},etc.
To find out this information, you need to go and have a look at the Gyoku gem: the gem that Savon uses to translate Ruby hashes into XML. Specifically, the documentation on using explicit XML attributes. Looking at that, we can get the XML you're looking for with the following hash:
{
"add:Action" => {
"#xmlns:add" => "http://www.w3.org/2005",
:content! => "http://google/FMP"
}
}
We can test this in IRB directly with Gyoku:
irb> require 'gyoku'
# => true
irb> Gyoku.xml({"add:Action" => { "#xmlns:add" => "http://www.w3.org/2005", :content! => "http://google/FMP" } })
# => "<add:Action xmlns:add=\"http://www.w3.org/2005\">http://google/FMP</add:Action>"
To describe a hash for carrierwave configurations, I write like:
{
provider: 'AWS',
aws_access_key_id: ENV["aws_access_key_id"],
aws_secret_access_key: ENV["aws_secret_access_key"],
}
However, tutorials seem the advice to write:
{
:provider => 'AWS'
:aws_access_key_id => ENV["aws_access_key_id"],
:aws_secret_access_key => ENV["aws_secret_access_key"],
}
What is the difference between the two? Is there good reason to use one over the other?
There is no difference.
nitz#comp:~$ irb
irb(main):001:0> {a:1}
=> {:a=>1}
This is a new syntax for specifying hashes with keys that are symbols, which is the "normal" way (as far as I can see) of defining hashes.
Also see What are the benefits of the new hash syntax in Ruby 1.9?
It's the new syntax for ruby 1.9+, just a syntactic sugar, that's all.
http://breakthebit.org/post/8453341914/ruby-19-and-the-new-hash-syntax
I prefer to use the new as older syntax might get deprecated in near future.
This "JSON" syntax was added in ruby 1.9 http://effectif.com/ruby/update-your-project-for-ruby-19-hash-syntax
The only difference is that you can't do things like dashes with it:
:'foo-moo' => 2
I have some large hashes (>10⁵ keys) with interlocking structures. They're stored on disk as YAML. I'd like to avoid duplication by using anchors and references in the YAML, but I haven't been able to figure out if there's a way to do it implicitly in the hash such that the #to_yaml method will label the anchor nodes properly.
Desired YAML:
---
parent1:
common-element-1: &CE1
complex-structure-goes: here
parent2:
uncomment-element-1:
blah: blah
<<: *CE1
Ruby code:
hsh = {
'parent1' => {
'common-element-1' => {
'complex-structure-goes' => 'here',
},
'parent2' => {
'uncommon-element-1' => {
'blah' => 'blah',
},
'<<' => '*CE1',
},
}
The reference is quite straightforward -- but how to embed the &CE1 anchor in the 'common-element-1' item in the Ruby hash?
I want to work as much as possible with native Ruby primitive types (like Hash) rather than mucking about with builders and emitters and such -- and I definitely don't want to write the YAML manually!
I've looked at Read and write YAML files without destroying anchors and aliases? and its relative, among other places, but haven't found an answer yet -- at least not that I've understood.
Thanks!
If you use the same Ruby object, the YAML library will set up references for you:
> common = {"ohai" => "I am common"}
> doc = {"parent1" => {"id" => 1, "stuff" => common}, "parent2" => {"id" => 2, "stuff" => common}}
> puts doc.to_yaml
---
parent1:
id: 1
stuff: &70133422893680
ohai: I am common
parent2:
id: 2
stuff: *70133422893680
I'm not sure there's a straightforward way of defining Hashes that are subsets of each other, though. Perhaps tweaking your structure a bit would be warranted?
I recently asked a question (heres the link) regarding opening a link in a new page. I found there were two answers.
:target => '_blank'
target: "_blank"
I was hoping someone could explain the difference between single and double quotations and why both of the above work. I understand the second option is only achievable on more recent versions of rails (I'm using 3.2.2).
basically, this is Ruby's hash, ( for more info, please refer to "#mu is too shot"'s resource )
# always works (works both in Ruby 1.8 and 1.9)
:target => '_blank'
# works in 1.9 only
target: "_blank"
both of them can assign hash's elements.
however the code below is incorrect.
# this is incorrect
target: = "_blank"
1.9.2-p290 :009 > { :name => "soundar" } == { name: "soundar" }
=> true