What does ||= do in Ruby 1.9.2?
It assigns []
to params["user][:role_ids]
if params["user][:role_ids]
is nil
or another falsy value...
Otherwise, it retains the original value of params["user][:role_ids]
Example
variable = nil
variable ||= "string"
puts variable # "string"
variable2 = "value"
variable2 ||= "string"
puts variable2 # "value"
What happened with Ruby 1.9.2?
If you're talking about updates to deal with CVE-2013-4073, then Ruby-lang.org says:
All users are recommended to upgrade to Ruby 2.0.0-p247, 1.9.3-p448 or
1.8.7-p374.
Presumably, anyone using MRI Ruby 1.9.2 is able to use MRI Ruby 1.9.3.
Why does the ?a command in ruby 1.9.2 does not return ASCII code
In Ruby 1.8 "foo"[0]
returned the character code at index 0 instead of the character string at index 0. As part of support for international strings with various encodings (instead of as an array of bytes), Ruby 1.9 changed this behavior to return the single-character string at the specified index.
Along with this change, ?a
was changed to evaluate as a single-character string as well. Presumably this was so that libraries with code like this...
if my_string[0] == ?a
...would continue to work. If you want character code value for the first character of a string, use String#ord
:
puts "It's a boy!" if my_string[0].ord == 89
For a lot more answer, see this stackoverflow question.
What will the major/minor differences be between ruby 1.9.2 and ruby 2.0?
Two features that are already implemented in YARV, and which will most likely end up in Ruby 2.0, are traits (mix
) and Module#prepend
.
The mix
method, unlike the current include
method, takes a list of modules, and mixes all of them in at the same time, making sure that they have no conflicting methods. It also gives you a way to easily resolve conflicts, if e.g. two modules you want to mix in define the same method. So, basically, while the include
method allows you to treat a module as a mixin, the mix
method allows you to treat a module as a trait.
Module#prepend
mixes a module into a class or module, again just like include
does, but instead of inserting it into the inheritance chain just above the class, it inserts is just below the class. This means that methods in the module can override methods in the class, and they can delegate to the overriden methods with super
, both of which is not possible when using include
. This basically makes alias_method_chain
obsolete.
One feature that has been discussed for a couple of months (or 10 years, depending on how you count), are Refinements. There has been discussion for over 10 years now to add a way to do scoped, safe monkey patching in Ruby. I.e. a way where I can monkey patch a core class, but only my code sees that monkey patch, other code doesn't. For many years, the frontrunner for that kind of safe monkey patching were Selector Namespaces, however more recently, Classboxes have been getting a lot of attention, and even more recently, a prototype implementation and specification of Refinements, a variant of Classboxes, was put forward.
Generally speaking, the big theme of Ruby 2.0 is scalability: scaling up to bigger teams, bigger codebases, bigger problem sizes, bigger machines, more cores. But also scaling down to smaller machines like embedded devices.
The three features I mentioned above are for scaling to bigger teams and bigger codebases. Some proposed features for scaling to bigger problem sizes and more cores are parallel collections and parallel implementations of Enumerable
methods such as map
, as well as better concurrency abstractions such as futures, promises, agents, actors, channels, join patterns or something like that.
What does ||= mean?
It is an assignment operator which means: or assign this value to a variable.
So if you did something like x ||= y
this meansx || x = y
so if x is nil or false set x to be the value of y.
Ruby 1.9.2 syntax error
Have you tried removing the space between create
and (
? If Ruby doesn't realize that you want to call a method, you can't leave off the {}
, since that's reserved for handling a hash as last argument in a method.
>> def foo(h) ; end #=> nil
>> foo (:foo => "bar")
SyntaxError: (irb):2: syntax error, unexpected tASSOC, expecting ')'
foo (:foo => "bar")
^
(irb):2: syntax error, unexpected ')', expecting $end
[snip]
>> foo(:foo => "bar") #=> nil
Ruby 1.9.2 - multiple splat argument issue
Example of using splat arguments:
def sum(*nums)
sum = 0
nums.each do |num|
sum += num
end
sum
end
puts sum(1,2,3)
Notice how the arguments are specified directly, not inside []
.
If the method defined a second splat argument, one couldn't determine when the first one ends and the second begins.
Does ruby 1.9.2 have an is_a? function?
There's not a built in function to say if a string is effectively an integer, but you can easily make your own:
class String
def int
Integer(self) rescue nil
end
end
This works because the Kernel method Integer()
throws an error if the string can't be converted to an integer, and the inline rescue nil
turns that error into a nil.
Integer("1") -> 1
Integer("1x") -> nil
Integer("x") -> nil
and thus:
"1".int -> 1 (which in boolean terms is `true`)
"1x".int -> nil
"x".int -> nil
You could alter the function to return true
in the true cases, instead of the integer itself, but if you're testing the string to see if it's an integer, chances are you want to use that integer for something! I very commonly do stuff like this:
if i = str.int
# do stuff with the integer i
else
# error handling for non-integer strings
end
Although if the assignment in a test position offends you, you can always do it like this:
i = str.int
if i
# do stuff with the integer i
else
# error handling for non-integer strings
end
Either way, this method only does the conversion once, which if you have to do a lot of these, may be a significant speed advantage.
[Changed function name from int?
to int
to avoid implying it should return just true/false.]
Related Topics
How to Make a Post Request with Open-Uri
Get Sidekiq to Execute a Job Immediately
Do I Have to Manually Uninstall All Dependent Gems
How to Overwrite a Printed Line in the Shell with Ruby
Notices for Sequence After Running Migration in Rails on Postgresql Application
Homebrew Installation on MAC Os X Failed to Connect to Raw.Githubusercontent.Com Port 443
Rails Form Validation Conditional Bypass
How to Express Infinity in Ruby
Differencebetween 'Try' and '&.' (Safe Navigation Operator) in Ruby
Create Custom HTML Helpers in Ruby on Rails
What Does "Shadowing" Mean in Ruby
How to Solve Insecure World Writable Dir /Usr in Path,Mode 040777 Warning on Ruby
Testing If a Hash Has Any of a Number of Keys
Ruby: Creating a Sandboxed Eval