Skip to content

Instantly share code, notes, and snippets.

@nisevi
Last active April 26, 2019 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nisevi/d2cffea4951a1553fb84530ebad4b176 to your computer and use it in GitHub Desktop.
Save nisevi/d2cffea4951a1553fb84530ebad4b176 to your computer and use it in GitHub Desktop.
Algunos métodos de ruby sobre diferentes objetos
a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!"} #=> ["a!", "b!", "c!", "d!"]
a.map.with_index {|x, i| x * i} #=> ["", "b", "cc", "ddd"]
a #=> ["a", "b", "c", "d"]
[ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
[ "a", "b", "c" ].compact! #=> nil
[1,2,3,4,5].select {|num| num.even? } #=> [2, 4]
1234.integer? #=> true
-12345.abs #=> 12345
36.lcm(60) #=> 180
(-1).next #=> 0
1.succ #=> 2
5.times {|i| print i, " " } #=> 0 1 2 3 4
"Ho! " * 3 #=> "Ho! Ho! Ho! "
"Ho! " * 0 #=> ""
a = "hello "
a << "world" #=> "hello world"
a << 33 #=> "hello world!"
"hello".capitalize #=> "Hello"
"HELLO".capitalize #=> "Hello"
"123ABC".capitalize #=> "123abc"
"hello".center(4) #=> "hello"
"hello".center(20) #=> " hello "
"hello".center(20, '123') #=> "1231231hello12312312"
"hEllO".downcase #=> "hello"
"hEllO".upcase #=> "HELLO"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment