group on hash
'
def group_by_hash hash, value
hash.group_by do |k,v|
v > value ? "Big" : "Small"
end
end
marks = {"Chair" => 30, "Table" => 40, "Bed" => 60, "stool" => 20}
group_by_hash(marks, 30)
Community contributions, tips, and corrections to the documentation. (1708 notes)
'
def group_by_hash hash, value
hash.group_by do |k,v|
v > value ? "Big" : "Small"
end
end
marks = {"Chair" => 30, "Table" => 40, "Bed" => 60, "stool" => 20}
group_by_hash(marks, 30)
For generate a token, make this:
def generate_token
self.token = SecureRandom.uuid
end
SecureRandom return a number random and the uuid make this number be unique. This a good idea for use in shopping cart, for example
You can specify default value as a fail-back if a hash doesn't have a given key.
{a: false}.fetch(:b, true) => true {a: false}.fetch(:a, true) => false
It is useful especially in the case where you want to set default value for a method.
def initialize(args) @foo = args.fetch(:foo, 1)...
You can pass an proc for o callback with the conditions
before_action :initial_value, only: [:index, :show], unless: -> { @foo.nil? }
before_action :initial_value, only: [:index, :show], if: -> { @foo }
You can pass an proc for o callback with the conditions
after_action :initial_value, only: [:index, :show], unless: -> { @foo.nil? }
after_action :initial_value, only: [:index, :show], if: -> { @foo }
If you wanna show "year" and "month" only, you can use "order" to do it:
select_date(Date.current, order: [:year, :month])
That's it.
This method first searches the table for ANY FIRST RECORD, not the one matching given attributes. If no record is found at all, it creates one using the specified attributes. This might be misunderstood in many cases.
When Errno::EEXIST is raised, it indicates there are permission issues, rather than an existing item.
Good suggestion about using Marshal to avoid cycles, astgtciv, but what about the security risks of doing that?
See http://ruby-doc.org/core-2.2.2/Marshal.html where it states:
By design, ::load can deserialize almost any class loaded into the Ruby process. In many cases this can lead to remot...
It's worth noting that this method is also implemented by other classes like Proc, Range and even String.
Most of these are missing proper examples, but you can find some useful examples here:
If your integration test is checking for correct behavior of a redirect to the request referer, you can set the referring path in the headers hash with syntax like:
patch update_user_role_path, { user: {role: "vip"} }, { 'HTTP_REFERER' => user_url }
assert_redirected_to user_url
#new_record? will not check if the record has been destroyed
#persisted? will also check if the record has not been destroyed
As Jebin reported, we are not getting the value in array format when we set the hidden_field_tag with an Array variable, instead it is a String that we are getting.
@rubynooby: #find and #detect are aliases of the same underlying method. You can use them interchangeably to provide additional readability to your code (find an element to use it or detect if an element is present to do something).
http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-find
The official Ruby documentation is available for this here http://ruby-doc.org/stdlib-2.2.3/libdoc/rss/rdoc/RSS.html
But the library that will be most helpful to you is called Feedjira: http://feedjira.com/
See this for deprecated https://github.com/rails/rails/commit/36253916b0b788d6ded56669d37c96ed05c92c5c
use
def something
return @_var if defined? @_var
# more code
end
Actually, @tarasevich is right on this. Let's have a look at your own example:
[["1","2"],["3","4"]].flat_map {|i| i[0] } # => ["1", "3"]
[["1","2"],["3","4"]].map {|i| i[0] }.flatten # => ["1", "3"]
[["1","2"],["3","4"]].flatten.map {|i| i[0] } # => ["1", "2", "3", "4"]
You are...
@tarasevich noted that
a.flat_map(&b) works exactly like a.map(&b).flatten!(1)
This is backwards because map and flatten are not always interchangeable in order. Mapping over the example array only gives you 2 items. This can result in significant differences depending on what you're d...
Works as expected for non bang methods
> a={x:1, y:2, z:3}
=> {:x=>1, :y=>2, :z=>3}
> a.slice(:y)
=> {:y=>2}
> a.except(:y)...