Skip to content

Instantly share code, notes, and snippets.

# http://ruby-doc.org/core-2.5.0/IO.html#method-i-gets
# Ruby >= 2.4
irb(main):001:0> gets(chomp: true)
hello world
=> "hello world"
@pointcom
pointcom / ipaddr.rb
Created January 3, 2018 22:19
Ruby 2.5
require 'ipaddr'
IPAddr.new("172.16.0.0/12").private?
=> true
SecureRandom.alphanumeric
=> "i2jZWXwSN4123yk2"
str = "- hello world -"
str.delete_prefix("- ")
=> "hello world -"
str.delete_suffix(" -")
=> "- hello world"
@pointcom
pointcom / string-start_with.rb
Last active January 4, 2018 11:15
Ruby 2.5
'- Do you know whether this string'.start_with?(/^- /)
=> true
Integer.sqrt(24)
=> 4
h = { a: 1, b: 2, c: 3 }
=> {:a=>1, :b=>2, :c=>3}
h.transform_keys {|k| k.to_s }
=> {"a"=>1, "b"=>2, "c"=>3}
h.transform_keys(&:to_s)
=> {"a"=>1, "b"=>2, "c"=>3}
h.transform_keys.with_index {|k, i| "#{k}.#{i}" }
%w[ant bear cat].all?(/t/)
=> false
[1, 2i, 3.14].all?(Numeric)
=> true
[1, 2, 3].all?(1..3)
=> true
@pointcom
pointcom / dir-glob.rb
Created January 3, 2018 21:17
Ruby 2.5
Dir.glob("*", base: "/lib")
=> ["x86_64-linux-gnu", "cpp", "init", "terminfo", "systemd", "lsb", "udev"]
[1, 2, 3].append(4)
=> [1, 2, 3, 4]
[1, 2, 3].prepend(0)
=> [0, 1, 2, 3]