Ruby tips #223 Nov 2016
It’s a second part of my post series about ruby tips. Today you’ll learn more about:
- Special values
CAUTION: These tips are not equally feat to production. Lots of them are just interesting solutions which you’ll not find anywhere else. Use them at your own risk.
In ruby you can find security levels for your code.
The Ruby security level is represented by the
$SAFE global variable.
The value ranges from minimum value 0 to maximum value 4.
|0||No checking of the use of externally supplied (tainted) data is performed. This is Ruby’s default mode.|
|>= 1||Ruby disallows the use of tainted data by potentially dangerous operations.|
|>= 2||Ruby prohibits the loading of program files from globally writable locations.|
|>= 3||All newly created objects are considered tainted.|
|>= 4||Ruby effectively partitions the running program in two. Nontainted objects may not be modified. Typically, this will be used to create a sandbox: the program sets up an environment using a lower$SAFE level, then resets $SAFE to 4 to prevent subsequent changes to that environment.|
In ruby you can find regexp for emails. If you want to use it, you have to require
library and call
DATA variable in ruby allows us to access the text at the end of our file
listed after the
__END__ block. This can be surprisingly useful, for instance if we
need to extract information from a rather large data blob.
Return array with all symbols in env.
#to_s (I don’t know why you need to use this but it’s a funny method I think).
Range to array
You can use
* operator for converting range to array.
You probably know already how to use
* in method definition argument list. But
** is less known.
Also you can call this method like
#gsub with hash args
Non string and symbol keys
This method returns a two element array
#cover? and time
Ranges in case statements
That’s all. I hope it’ll be useful for you. In next part we’re gonna talking about:
- Tracing Ruby Code
- memory usage
Happy hacking! 🚀