Difference between :target => '_blank' and target: = "_blank" - ruby

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

Related

Freeze arrays and hashes by default?

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.

Difference between `:provider => ' value '` and `provider: 'value'`

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

In which version of ruby appeared ':' instead '=>'?

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.

Rhomobile Migration from Rho3.1 to Rho 4.0 Alert Msg. Error

I am trying to migrate my rho application from 3.1 to 4.0. In 3.1 i have defined alert using Alert.show_popup :title => "Please Wait", :message => "Fetching Data..." But as specified in documentation now i have changed it to the
dataPopProps = Hash.new
dataPopProps['message'] = "Fetching Data...";
dataPopProps['title'] = "Please Wait";
Rho::Notification.showPopup(dataPopProps)
But i am still getting the same error.
Error: Button list has been incorrectly defined. DIalog will not Launch
Any help will be great.
Try like this,
dataPopProps = Hash.new
dataPopProps['message'] = "Fetching Data...";
dataPopProps['title'] = "Please Wait";
dataPopProps['buttons'] = ["Ok"]
Rho::Notification.showPopup(dataPopProps)
For future reference, the official docs are often incorrect, so working with Rhodes can be frustrating. However, the example listed here seems to be a good solution. Note that the example for Notification is in Javascript.
Here is an elegant way to write this in Ruby:
dataPopProps = {
'message' => 'Fetching Data...',
'title' => 'Please Wait',
'buttons' => [{ :id => 'no', :title => 'no' }]
}
Semicolons are eggregiously unecessary in Ruby, and you can clean up your code by using literals instead of Hash.new.

Ruby's Mechanize balks when selecting a radio button (maybe because its name is capitalized) but Perl's WWW::Mechanize works fine

I'm trying to submit a form with Ruby's Mechanize gem. This form has a set of radio buttons named "KeywordType". The individual buttons are named something like rdoAny, rdoAll and rdoPhrase. With Perl's WWW:Mechanize it works just fine:
my $result = $agent->submit_form(
form_number => 1,
fields => {
txtKeywords => 'foo bar baz',
lstLocationCode => '2100',
lstONETMajorGroup => '0',
KeywordType => 'rdoAny'
},
button => 'btnSearch'
);
but Ruby balks when I do this:
result = page.form_with(:id => 'frmSearch') do |field|
field.txtKeywords = 'foo bar baz'
field.lstLocationCode = '2100'
field.lstONETMajorGroup = '0'
field.KeywordType = 'rdoAny'
end.submit
This throws the error
"undefined method `KeywordType=' for #<Mechanize::Form:0x00000001c896e0> (NoMethodError)".
I've tried leaving out the KeywordType field, but then I just get sent back to the same page with no obvious error message. I've also tried doing things like field.radiobuttons.second.check and field.radiobuttons_with(:name => "KeywordType") to no avail.
And on a side note, is whatever's going on because Ruby sees a capitalized radiobutton name and thinks it's a constant?
Thanks.
Does this work?
field['KeywordType'] = 'rdoAny'
Edit: Oh, and I think you missed a part here:
result = page.form_with(:id => 'frmSearch') do |field|
should be (I think):
result = page.form_with(:id => 'frmSearch').fields.each do |field|
Gaah. The outdated version gremlin strikes again. "gem update mechanize" is now at least showing me the values for the two dropdowns.

Resources