Skip to main content
Post Made Community Wiki by Dennis
added 423 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20

Length 19 (by a guest)

[1, 2].each{|i|p i}

Ruby supports for loops, but the .each method is preferred. The code in brackets is a block, essentially a single-use anonymous function. Blocks can take parameters, which are enclosed in pipe characters. The p is a shortcut for puts [something].inspect. The .inspect method is a variation of .to_s which provides more details about the object.

Length 18

Length 18

Length 19 (by a guest)

[1, 2].each{|i|p i}

Ruby supports for loops, but the .each method is preferred. The code in brackets is a block, essentially a single-use anonymous function. Blocks can take parameters, which are enclosed in pipe characters. The p is a shortcut for puts [something].inspect. The .inspect method is a variation of .to_s which provides more details about the object.

Length 18

play fair now
Source Link
Calvin's Hobbies
  • 90k
  • 46
  • 339
  • 555

Length 20

{true=>?Y,false=>?N}

This snippet is an example of creating a hash (or associative array) with key true to Y character, and false to N character. This statement could be assigned to a variable then indexed with boolean, for example:

sure = {true => 'Yes, indeed!' ,false => 'No way!!!'}
x = 123
sure[x > 5]    # Yes, indeed!
sure[x == 120] # No way!!!

Length 19

[1,4,5].map{|x|x*x}

This is an example on how to create an array, then square it, the output would be a new array [1,16,25]. The synonym of map method is collect.

Length 18

Length 20

{true=>?Y,false=>?N}

This snippet is an example of creating a hash (or associative array) with key true to Y character, and false to N character. This statement could be assigned to a variable then indexed with boolean, for example:

sure = {true => 'Yes, indeed!' ,false => 'No way!!!'}
x = 123
sure[x > 5]    # Yes, indeed!
sure[x == 120] # No way!!!

Length 19

[1,4,5].map{|x|x*x}

This is an example on how to create an array, then square it, the output would be a new array [1,16,25]. The synonym of map method is collect.

Length 18

Length 18

deleted 9 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20

Length 20 (better type it now better than forgot)

%i[I love you too~!]too]

This is an example on how to create an array of symbols, the result would be: [:I, :love, :you, :'too~!']too]

'12'1.345'[34'[/(\.\d+)/]

This is an example on creating snippet then parse it using regular expression. This regular expression means get any dot \. that followed by one or more (+) digit (\d) then capture it. The result of this snippet is a new string containing .34534.

Length 20 (better type it now better than forgot)

%i[I love you too~!]

This is an example on how to create an array of symbols, the result would be: [:I, :love, :you, :'too~!']

'12.345'[/(\.\d+)/]

This is an example on creating snippet then parse it using regular expression. This regular expression means get any dot \. that followed by one or more (+) digit (\d) then capture it. The result of this snippet is a new string containing .345.

Length 20

%i[I love you too]

This is an example on how to create an array of symbols, the result would be: [:I, :love, :you, :too]

'1.34'[/(\.\d+)/]

This is an example on creating snippet then parse it using regular expression. This regular expression means get any dot \. that followed by one or more (+) digit (\d) then capture it. The result of this snippet is a new string containing .34.

added 422 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 422 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 8 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 583 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 293 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 135 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 432 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 325 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 199 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 199 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 211 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 15 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 298 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 562 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
added 272 characters in body
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading
Source Link
Kokizzu
  • 1.5k
  • 11
  • 20
Loading