ruby
The Ruby Programming Language
In C and many other languages, there is a continue
keyword that, when used inside of a loop, jumps to the next iteration of the loop. Is there any equivalent of this continue
keyword in Ruby?
Source: (StackOverflow)
I just updated to rails 4.0.2 and I'm getting this warning:
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Is there any security issue in setting it to false?
Source: (StackOverflow)
I tend to use before blocks and set instance variables in them and then use them across my examples, but recently I came upon let()
. According to rspec docs, it is used to
... to define a memoized helper method. The value will be cached across multiple calls in the same example but not across examples.
My question is how is this different from using instance variables in before blocks? And also when should you use let()
vs before()
?
Source: (StackOverflow)
I find myself repeatedly looking for a clear definition of the differences of nil?
, blank?
, and empty?
in Ruby on Rails. Here's the closest I've come:
blank?
objects are false, empty, or a whitespace string. For example, ""
, " "
, nil
, []
, and {}
are blank.
nil?
objects are instances of NilClass.
empty?
objects are class-specific, and the definition varies from class to class. A string is empty if it has no characters, and an array is empty if it contains no items.
Is there anything missing, or a tighter comparison that can be made?
Source: (StackOverflow)
I am trying to understand the difference between these four methods. I know by default that ==
calls the method equal?
which returns true when both operands refer to exactly the same object.
===
by default also calls ==
which calls equal?
... okay, so if all these three methods are not overridden, then I guess
===
, ==
and equal?
do exactly the same thing?
Now comes eql?
. What does this do (by default)? Does it make a call to the operand's hash/id?
Why does Ruby have so many equality signs? Are they supposed to differ in semantics?
Source: (StackOverflow)
I don't use the RI or RDoc output from the gems I install in my machine or in the servers I handle (I use other means of documentation).
Every gem I install installs RI and RDoc documentation by default, because I forget to set --no-ri --no-rdoc
.
Is there a way to make those two flags the default?
Source: (StackOverflow)
I want to know if there is a much cleaner way of doing this. Basically, I want to pick a random element from an array of variable length. Normally, I would do it like this:
myArray = ["stuff", "widget", "ruby", "goodies", "java", "emerald", "etc" ]
item = myArray[rand(myarray.length)]
Is there something that is more readable / simpler to replace the second line? Or is that the best way to do it. I suppose you could do myArray.shuffle.first
, but I only saw #shuffle
a few minutes ago on SO, I haven't actually used it yet.
Source: (StackOverflow)
I have an array of integers.
For example:
array = [123,321,12389]
Is there any nice way to get the sum of them?
I know, that
sum = 0
array.each { |a| sum+=a }
would work.
Source: (StackOverflow)
How can I check whether a variable is defined in Ruby? Is there an isset
-type method available?
Source: (StackOverflow)
I am having a hard time understanding attr_accessor
s in Ruby, can someone explain them to me? I have done tons of Google searches, just can't understand them fully.
Source: (StackOverflow)