Is it possible to overload ! operator in Ruby language?
yes, you can overload this ! in Ruby 1.9, see here http://phrogz.net/programmingruby/language.html#table_18.4 , in this url they had given what are the operators, which are overloaded in Ruby
Yes, in Ruby 1.9 ! is a method and can be overridden.
Source
Related
I currently have a piece of code that looks like:
if match = request.path.match(/\A\/(?<slug>(?!admin|assets)\w+)/)
match[:slug]
end
Is there a way to use the safe navigation operator (introduced in 2.3.0) to avoid this if conditional?
Just use the ordinary (non-sugar) form.
request.path.match(/\A\/(?<slug>(?!admin|assets)\w+)/)&.[](:slug)
For better readability I would pick #dig instead of the #[].
Like,
request.path.match(/\A\/(?<slug>(?!admin|assets)\w+)/)&.dig(:slug)
instead of
request.path.match(/\A\/(?<slug>(?!admin|assets)\w+)/)&.[](:slug)
For readability, I prefer using the match's named_captures method which is available from ruby 1.9.1, with the safe operator which is available from ruby 2.3.0.
request.path.match(/\A\/(?<slug>(?!admin|assets)\w+)/)&.named_captures&.dig('slug')
You can send any method, so using safe-browsing operator this will be:
request.path.match(/\A\/(?<slug>(?!admin|assets)\w+)/)&.send(:[], :slug)
What is the _char variable attached to the end of the .each method called?
variable.to_s.each_char do |char|
I am new to Ruby and I saw this for the first time and I don't know what its called (or what it does or how it works). Can someone please point me in the right direction?
It is a different method named each_char, not a special syntax.
In current versions of Ruby there is no String#each method. String#each_char is for enumerating characters of a string (though they are themselves strings). String#chars is the same as String#each_char.
What is the name of the method corresponding to the ternary operator? By name I mean :+ for addition, :== for equality, etc.
I want to override the ternary operator to build a proxy class (same idea as Javascript proxies) but I can't seem to find the name for this.
There are two names that this is known by, if you are talking about the ? : operator, and that is ternary operator or conditional operator.
But it is not a method, as you can see in this table.
You would need to go to Ruby Source itself to override the behavior. Probably not what you would want to do.
I'm pretty sure it's just known as the ternary operator. Usually people know what you mean when you say that, and I've never heard or seen another name, even during research.
If you mean symbol, I'd call it:
?:
(Question mark, Colon)
The question is: Can I define my own custom operator in Ruby, except for the ones found in
"Operator Expressions"?
For example: 1 %! 2
Yes, custom operators can be created, although there are some caveats. Ruby itself doesn't directly support it, but the superators gem does a clever trick where it chains operators together. This allows you to create your own operators, with a few limitations:
$ gem install superators19
Then:
require 'superators19'
class Array
superator "%~" do |operand|
"#{self} percent-tilde #{operand}"
end
end
puts [1] %~ [2]
# Outputs: [1] percent-tilde [2]
Due to the aforementioned limitations, I couldn't do your 1 %! 2 example. The Documentation has full details, but Fixnums can't be given a superator, and ! can't be in a superator.
No. You can only define operators already specified in ruby, +,-,!,/,%, etc. (you saw the list)
You can see for yourself this won't work
def HI
def %!
puts "wow"
end
end
This is largely due to the fact that the syntax parser would have to be extended to accept any code using your new operator.
As Darshan mentions this example alone may not be enough to realize the underlying problem. Instead let us take a closer look at how the parser could possibly handle some example code using this operator.
3 %! 0
While with my spacing it may seem obvious that this should be 3.%!(0) without spacing it becomes harder to see.
3%! can also be seen as 3.%(0.!) The parser has no idea which to chose. Currently, there is no way easy way to tell it. Instead, we could possibly hope to override the meaning of 3.%(0.!) but this isn't exactly defining a new operator, as we are still only limited to ruby's parsable symbols
You probably can't do this within Ruby, but only by modifying Ruby itself. I think modifying parse.y would be your best bet. parse.y famtour
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why doesn't Ruby support i++ or i— for fixnum?
Why is there no increment operator in Ruby?
e.g.
i++
++i
Is the ++ operator used for something else? Is there a real reason for this?
Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse. More importantly, ++x or --x will do nothing! In fact, they behave as multiple unary prefix operators: -x == ---x == -----x == ...... To increment a number, simply write x += 1.
Taken from "Things That Newcomers to Ruby Should Know
" (archive, mirror)
That explains it better than I ever could.
EDIT: and the reason from the language author himself (source):
++ and -- are NOT reserved operator in Ruby.
C's increment/decrement operators are in fact hidden assignment. They affect variables, not objects. You cannot accomplish assignment via method. Ruby uses +=/-= operator instead.
self cannot be a target of assignment. In addition, altering the value of integer 1 might cause severe confusion throughout the program.
From a posting by Matz:
(1) ++ and -- are NOT reserved
operator in Ruby.
(2) C's increment/decrement
operators are in fact hidden
assignment.
They affect variables, not objects. You cannot accomplish
assignment via method. Ruby uses +=/-= operator instead.
(3) self cannot be a target of
assignment. In addition, altering
the value of integer 1 might cause severe confusion throughout
the program.
matz.
I don't think that notation is available because—unlike say PHP or C—everything in Ruby is an object.
Sure you could use $var=0; $var++ in PHP, but that's because it's a variable and not an object. Therefore, $var = new stdClass(); $var++ would probably throw an error.
I'm not a Ruby or RoR programmer, so I'm sure someone can verify the above or rectify it if it's inaccurate.