How to convert a decimal number to an integer only if the decimal places are 0
def lazy_decimal(num)
if (num.round - num) == 0
num.to_i
else
num
end
end
Extracting the filename of a url, that can have additional params
/\/((?:[\w\-\_]+\.\w{3,4}))(?:\?.+)?$/
or vs ||
i never use 'or' so i found this interesting.
just for fun defining ruby modules inside classes
Usually you would rather define classes inside modules, but if you are left with a class and you want a module namespaced....
suung@q2:~$ suung@q2:~$ irb irb(main):001:0> class Foo irb(main):002:1> module Bar irb(main):003:2> def baz irb(main):004:3> puts 'baz' irb(main):005:3> end irb(main):006:2> end irb(main):007:1> end => nil irb(main):008:0> lass Baz NameError: uninitialized constant Baz from (irb):8 from :0 irb(main):009:0> class Baz irb(main):010:1> include Foo::Bar irb(main):011:1> end => Baz irb(main):012:0> Baz.new.baz baz => nil irb(main):013:0>
Something is a valid calculation parameter (number so to say)
irb(main):013:0> "22.2".match /^\d+(\.\d+)?$/ => #<MatchData "22.2" 1:".2"> irb(main):014:0> "22.".match /^\d+(\.\d+)?$/ => nil irb(main):015:0> ".22".match /^\d+(\.\d+)?$/ => nil irb(main):016:0> "2.2".match /^\d+(\.\d+)?$/ => #<MatchData "2.2" 1:".2"> irb(main):017:0> "2..2".match /^\d+(\.\d+)?$/ => nil irb(main):018:0> "2.2.2".match /^\d+(\.\d+)?$/ => nil
Faraday yet another (different) ruby http client library
https://github.com/technoweenie/faraday
It's 'inspired by rack'
First view seems to be overkill for me, but i think it's generall a good approach, to have the RestClients (RestClient is actually what I use most) be more configurable in terms, where i can hook in functionality.
Faraday ships in the Oauth2 gem, i didn't try it alone yet.
pry - alternative to irb and making debugging any ruby application (including rails) fun
Definitely check out this railscasts: http://railscasts.com/episodes/280-pry-with-rails
- walk into ruby objects ( cd Array; self # => Array; cd new; self #=> [] )
pry(main)> cd Array pry(Array):1> self => Array pry(Array):1> cd new pry([]):2> self => []
- ls -m shows the current objects methods
pry(main)> cd Array pry(Array):1> ls -m [:[], :allocate, :new, :superclass, :try_convert, :yaml_tag]
reload-method reloads the file containing the method specified, edit-methods open up your code editor with the file containing the method (cursor at the correct method) thus, when you use pry for debugging, you can play with a method, edit it, reload it, play with it again.
help lists all available commands :)
Command List: -- help This menu. install-command Install a disabled command. toggle-color Toggle syntax highlighting. simple-prompt Toggle the simple prompt. pry-version Show Pry version. import-set Import a command set reload-method Reload the source file that contains the specified method reset Reset the REPL to a clean state. ri View ri documentation. e.g `ri Array#each` show-doc Show the comments above METH. Type `show-doc --help` for more info. Aliases: ? stat View method information and set _file_ and _dir_ locals. Type `stat --help` for more info. gist-method Gist a method to github. Type `gist-method --help` for more info. gem-install Install a gem and refresh the gem cache. gem-cd Change working directory to specified gem's directory. gem-list List/search installed gems. (Optional parameter: a regexp to limit the search) ls Show the list of vars and methods in the current scope. Type `ls --help` for more info. cd Start a Pry session on VAR (use `cd ..` to go back and `cd /` to return to Pry top-level) nesting Show nesting information. jump-to Jump to a Pry session further up the stack, exiting all sessions below. exit End the current Pry session. Accepts optional return value. Aliases: quit, back exit-all End all nested Pry sessions. Accepts optional return value. Aliases: !!@ exit-program End the current program. Aliases: quit-program, !!! !pry Start a Pry session on current self; this even works mid-expression. whereami Show the code context for the session. (whereamishows extra lines of code around the invocation line. Default: 5) ! Clear the input buffer. Useful if the parsing process goes wrong and you get stuck in the read loop. show-input Show the contents of the input buffer for the current multi-line expression. amend-line Amend a line of input in multi-line mode. Type `amend-line --help` for more information. Aliases % play Play back a string or a method or a file as input. Type `play --help` for more information. hist Show and replay Readline history. Type `hist --help` for more info. Aliases: history . All text following a '.' is forwarded to the shell. shell-mode Toggle shell mode. Bring in pwd prompt and file completion. cat Show output of file FILE. Type `cat --help` for more information. show-method Show the source for METH. Type `show-method --help` for more info. Aliases: $, show-source show-command Show the source for CMD. Type `show-command --help` for more info. edit Invoke the default editor on a file. Type `edit --help` for more info edit-method Edit a method. Type `edit-method --help` for more info.
Thanks!
Ruby: A tiny benchmark comparing two ways to convert a string in a symbol
Ruby 1.8.7
ruby-1.8.7-p334 :008 > Benchmark.measure { 1000000.times do "foo".to_sym end }
=> #<Benchmark::Tms:0xb74c7e64 @real=0.429145097732544, @utime=0.42, @cstime=0.0, @total=0.42, @cutime=0.0, @label="", @stime=0.0>
ruby-1.8.7-p334 :009 > Benchmark.measure { 1000000.times do :"foo" end } => #<Benchmark::Tms:0xb7537340 @real=0.128454208374023, @utime=0.13, @cstime=0.0, @total=0.13, @cutime=0.0, @label="", @stime=0.0>
Ruby 1.9.2
ruby-1.9.2-p180 :005 > Benchmark.measure { 1000000.times do "foo".to_sym end }
=> 0.610000 0.000000 0.610000 ( 0.662960)
ruby-1.9.2-p180 :006 > Benchmark.measure { 1000000.times do :"foo" end } => 0.100000 0.000000 0.100000 ( 0.105112)
Everything about system(), exec(), popen() and other ways to execute external commands
| method |
stdout? |
stderr? |
stdin? |
real-time? | comments | process id? | exit code? |
|---|---|---|---|---|---|---|---|
exec("command") |
no | no | no | yes |
Simple, non-interactive invocation; replaces current process with new process (in other words, any Ruby code after the exec will not be executed!) |
NA | NA |
system("command") |
no | no | no | yes | Simple, non-interactive invocation; waits till execution is done; outputs both stdout and stderr as normal | NA |
$?.exitstatus |
result = `command` |
yes |
no, unless you do 2>&1 |
no | buffered—output is returned only when the command has finished/exited | Same, only it capture the output of that process. | NA |
$?.exitstatus |
pipe = IO.popen("command", "r") |
yes |
no, unless you do 2>&1 |
no | yes—can even read a char or a line at a time, if you want | Interactive control of other process (write to its stdin, and then read from its stdout) |
pipe.pid |
$?.exitstatus |
pipe = IO.popen("command", "w+") |
yes |
no, unless you do 2>&1 |
yes | "" | "" | "" | "" |
Open3.popen3("command") |
yes | yes | yes | yes |
Very similar to IO.popen. |
||
exec("command") if fork.nil? |
NA | NA | NA | yes | Starts a child process running concurrently (in the "background"). |
http://whynotwiki.com/Ruby_/_Process_management#How_do_I_execute_an_external_program.3F
How to get debug information out of Net::HTTP
call
.set_debug_output $stderr
on the request object.