colon placement in Ruby 2.2+ [duplicate] - ruby

This question already has answers here:
Is there any difference between the `:key => "value"` and `key: "value"` hash notations?
(5 answers)
Closed 7 years ago.
I see colons used two different ways in Ruby
:controller => 'pages'
and then
action: => 'home'
I found an explanation here: http://goo.gl/ZKxKVK
it seems that the position doesn't matter, could someone clarify this?

Mostly it doesn't matter. Since Ruby 1.9 we can use more short form:
h = { a: 1, b: 2}
But there are some situations where you have to use the longest form, e.g.:
h = {1 => 'a', 2 => 'b'}
h = {"One Two" => 1}

action: => 'home' is not valid syntax.
It should be action: 'home' or :action => 'home'.
These are equivalent. They generate:
{:action=>'home'}

Related

why there is a column ":" in front of key in ruby [duplicate]

This question already has answers here:
Is there any difference between the `:key => "value"` and `key: "value"` hash notations?
(5 answers)
Closed 1 year ago.
aaa = { :one => "eins", :two => "zwei", :three => "drei" }
bbb = { one: "eins", two: "zwei", three: "drei" }
above are valid ruby code. at line 1, why there is ":" before "one"? What's the meaning?
It's called Symbol, you could think of it as a special string. Symbols are often used as hash keys.
You could check out more on Ruby Symbols vs. Strings
In ruby hash is an association of key and value.
my_hash = { :key => "value" }
or
my_hash = { key: "value" }
more informations here : https://launchschool.com/books/ruby/read/hashes

Call property programmatically in Ruby [duplicate]

This question already has answers here:
How to call methods dynamically based on their name? [duplicate]
(5 answers)
Closed 5 years ago.
I have a Ruby project where I programmatically get the names of keys in a hash I need to access. I can access the fields I need in the following way:
current_content = entry.fields[property_name.to_sym]
However, it seems that some content can only be accessed with a property syntax:
m.title_with_locales = {'en-US' => 'US Title', 'nl' => 'NL Title'}
Since I don't know "title" ahead of time, how can I make a call programmatically? Ex:
m.${property_name}_with_locales = {'en-US' => 'US Title', 'nl' => 'NL Title'}
You can use #send to programmatically access properties:
m.send("#{property_name}_with_locales")
# => { 'en-US' => 'US Title', ... }
If you need to access a setter and pass in values, you can do:
m.send("#{property_name}_with_locales=", { 'whatever' => 'value' })
beside send as #gwcodes had written, there are also, eval and call.
2.3.1 :010 > a
=> [1, 2, 3]
2.3.1 :011 > a.send("length")
=> 3
2.3.1 :012 > a.method("length").call
=> 3
2.3.1 :013 > eval "a.length"
=> 3
as shown on this blog post call is a bit faster than send.

Difference between 11_223 and 11223? [duplicate]

This question already has answers here:
What does the underscore mean in literal numbers?
(4 answers)
Closed 8 years ago.
What is difference and why did someone write number with underscore?
irb(main):001:0> a = 11_223
=> 11223
irb(main):002:0> b = 11223
=> 11223
irb(main):003:0> a == b
=> true
irb(main):004:0> a === b
=> true
irb(main):005:0> 11_223 === 11223
=> true
When you write 1 million in numbers, you would usually do:
1,000,000
and not:
1000000
To make it more readable.
You can do the same thing in Ruby with an underscore:
1_000_000
Ruby can't use the ,, because that's already used in other things (like function arguments), so the "strange" underscore character is used.

How does this definition for a hash work? [duplicate]

This question already has answers here:
Is there any difference between the `:key => "value"` and `key: "value"` hash notations?
(5 answers)
Closed 8 years ago.
I found code with hash assignment such as follows:
#defeat = {r: :s, p: :r, s: :p}
# => {:r=>:s, :p=>:r, :s=>:p}
Why are the keys for this hash generated as symbols? Is this a short form of doing this?
defeat[:r] = :s
defeat[:p] = :r
defeat[:s] = :p
Is there a name for this style of Hash?
A Hash can be easily created by using its implicit form:
grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
Hashes allow an alternate syntax form when your keys are always symbols. Instead of
options = { :font_size => 10, :font_family => "Arial" }
You could write it as:
options = { font_size: 10, font_family: "Arial" }
Now in your example #defeat = {r: :s, p: :r, s: :p}, all keys are symbol. That's why your example Hash is a valid construct, which has been introduced since 1.9.
When you use the hash style {key: value} you're actually declaring a symbol for the key. Like Arup's example, {:key => value} is the same thing with the implicit form. So anytime you use : instead of => in a hash, you are creating a symbol as the key.
In your example, you're creating symbols for both your key and your value.
{key: :value } # both are symbols

How do I create a hash from a querystring in ruby? [duplicate]

This question already has answers here:
Parse a string as if it were a querystring in Ruby on Rails
(5 answers)
Closed 9 years ago.
I want to create a hash from a querystring. This is my method:
def qs2h(querystring)
hashes = querystring.split('&').inject({}) do |result,query|
k,v = query.split('=')
if !v.nil?
result.merge(k.to_sym => v)
elsif !result.key?(k)
result.merge(k.to_sym => true)
else
result
end
end
hashes
end
qs2h('a=1&b=2&c=3&d') #=> {:a => "1", :b => "2", :c => "3", :d => true}
Is there any simpler method to do this in ruby?
Use CGI::parse:
CGI.parse('a=1&b=2&c=3&d')
# => {"a"=>["1"], "b"=>["2"], "c"=>["3"], "d"=>[]}
Hash[CGI.parse('a=1&b=2&c=3&d').map {|key,values| [key.to_sym, values[0]||true]}]
# => {:a=>"1", :b=>"2", :c=>"3", :d=>true}

Resources