This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ipaddr' | |
IPAddr.new("172.16.0.0/12").private? | |
=> true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SecureRandom.alphanumeric | |
=> "i2jZWXwSN4123yk2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
str = "- hello world -" | |
str.delete_prefix("- ") | |
=> "hello world -" | |
str.delete_suffix(" -") | |
=> "- hello world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'- Do you know whether this string'.start_with?(/^- /) | |
=> true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Integer.sqrt(24) | |
=> 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%w[ant bear cat].all?(/t/) | |
=> false | |
[1, 2i, 3.14].all?(Numeric) | |
=> true | |
[1, 2, 3].all?(1..3) | |
=> true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dir.glob("*", base: "/lib") | |
=> ["x86_64-linux-gnu", "cpp", "init", "terminfo", "systemd", "lsb", "udev"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3].append(4) | |
=> [1, 2, 3, 4] | |
[1, 2, 3].prepend(0) | |
=> [0, 1, 2, 3] |