Ruby: 100 Simple Codes

beginner-friendly collection of easy-to-understand Ruby examples.



Each code snippet is designed to help you learn programming concepts step by step, from basic syntax to simple projects. Perfect for students, self-learners, and anyone who wants to practice Ruby in a fun and practical way.


Codes:

1. Hello World

2. Add Two Numbers

3. Check Even or Odd

4. Loop Through Numbers

5. Simple Method

6. Array Example

7. Hash Example

8. Class and Object

9. Check If Number Is Positive

10. Simple Calculator

===

11. Subtract Two Numbers

12. Multiply Two Numbers

13. Divide Two Numbers

14. Find Remainder

15. Compare Two Numbers

16. Case Statement

17. While Loop

18. Until Loop

19. Check If String Includes Word

20. Reverse a String

===

21. Capitalize a String

22. Convert String to Uppercase

23. Convert String to Lowercase

24. Get String Length

25. Split String into Words

26. Join Array into String

27. Sort an Array

28. Find Maximum in Array

29. Find Minimum in Array

30. Check if Array Includes Value

===

31. Remove Duplicates from Array

32. Push Item to Array

33. Pop Item from Array

34. Check if Hash Has Key

35. Check if Hash Has Value

36. Iterate Over Hash

37. Convert String to Integer

38. Convert String to Float

39. Convert Integer to String

40. Check if Number is Zero

===

41. Create a Range

42. Check if Value in Range

43. Get Current Time

44. Format Time

45. Sleep for 2 Seconds

46. Generate Random Number

47. Repeat a String

48. Check If Variable is Nil

49. Ternary Operator

50. Define Constant

===

51. Rescue from Error

52. Check Data Type

53. Loop with Times

54. Loop with Upto

55. Loop with Downto

56. Map an Array

57. Select from Array

58. Reject from Array

59. Reduce Array to Sum

60. Symbol vs String

===

61. Convert String to Symbol

62. Check if File Exists

63. Read a File

64. Write to a File

65. Append to a File

66. Delete a File

67. Check if Directory Exists

68. Create a Directory

69. List Files in Directory

70. Delete a Directory

===

71. Check if String is Empty

72. Count Items in Array

73. Create an Empty Hash

74. Merge Two Hashes

75. Nested Hash Example

76. Define a Class with Initialize

77. Check if Object is Instance of Class

78. Check if Number is Negative

79. Count Characters in String

80. Swap Case in String

===

81. Repeat Code with Loop and Break

82. Next in Loop

83. Find Index in Array

84. Flatten Nested Array

85. Delete from Array

86. Compact Array (Remove nils)

87. Check if All Items Match Condition

88. Check if Any Item Matches Condition

89. Get Unique Characters from String

90. Use Constants in Class

===

91. Yield with Block

92. Default Parameter in Method

93. Global Variable

94. Class Variable

95. Check If Method Exists

96. Get First and Last from Array

97. Create Range with Step

98. Random Sample from Array

99. Sort Hash by Key

100. Sort Hash by Value

===