-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversion_data_types.rb
More file actions
110 lines (88 loc) · 5.22 KB
/
Copy pathconversion_data_types.rb
File metadata and controls
110 lines (88 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Conversion of data types
# In Ruby, you can convert between different data types using various methods. Here are some common conversions:
# To convert a string to an integer, you can use the `to_i` method. This will convert the string to an integer, and if the string does not contain a valid number, it will return 0.
str = "123"
num = str.to_i
puts num # Output: 123
# To convert a string to a float, you can use the `to_f` method. This will convert the string to a floating-point number, and if the string does not contain a valid number, it will return 0.0.
str = "3.14"
float_num = str.to_f
puts float_num # Output: 3.14
# To convert an integer to a string, you can use the `to_s` method. This will convert the integer to its string representation.
num = 456
str_num = num.to_s
puts str_num # Output: "456"
# To convert a float to a string, you can also use the `to_s` method.
float_num = 2.718
str_float = float_num.to_s
puts str_float # Output: "2.718"
# To convert an array to a string, you can use the `join` method, which concatenates the elements of the array into a single string with an optional separator.
arr = ["Hello", "World"]
str_arr = arr.join(" ") # Join with a space as a separator
puts str_arr # Output: "Hello World"
# To convert a hash to a string, you can use the `to_s` method, which will give you a string representation of the hash.
hash = { name: "Alice", age: 30 }
str_hash = hash.to_s
puts str_hash # Output: "{:name=>\"Alice\", :age=>30}"
# To convert a string to an array, you can use the `split` method, which divides the string into an array based on a specified delimiter (default is whitespace).
str = "apple,banana,cherry"
arr_str = str.split(",") # Split by comma
puts arr_str.inspect # Output: ["apple", "banana", "cherry"]
# To convert a string to a hash, you can use the `eval` method if the string is in a valid hash format. However, be cautious when using `eval`, as it can execute arbitrary code if the input is not controlled.
str = "{ name: 'Bob', age: 25 }"
hash_str = eval(str)
puts hash_str.inspect # Output: {:name=>"Bob", :age=>25}
# To convert an array to a hash, you can use the `to_h` method if the array contains pairs of elements (key-value pairs). For example:
arr = [[:name, "Charlie"], [:age, 28]]
hash_arr = arr.to_h
puts hash_arr.inspect # Output: {:name=>"Charlie", :age=>28}
# To convert a hash to an array, you can use the `to_a` method, which will convert the hash into an array of key-value pairs (as sub-arrays).
hash = { name: "Dave", age: 35 }
arr_hash = hash.to_a
puts arr_hash.inspect # Output: [[:name, "Dave"], [:age, 35]]
# To convert a string to a symbol, you can use the `to_sym` method. This will convert the string to a symbol, which is a lightweight, immutable identifier.
str = "example"
sym = str.to_sym
puts sym # Output: :example
# To convert a symbol to a string, you can use the `to_s` method.
sym = :example
str_sym = sym.to_s
puts str_sym # Output: "example"
# To convert a boolean to a string, you can also use the `to_s` method.
bool = true
str_bool = bool.to_s
puts str_bool # Output: "true"
# To convert a string to a boolean, you can use a simple conditional check. For example, you can consider "true" (case-insensitive) as true and anything else as false.
# There is no built-in method to directly convert a string to a boolean, but you can achieve this by using a simple conditional check. For example, you can consider "true" (case-insensitive) as true and anything else as false:
str = "true"
bool_str = str.downcase == "true"
puts bool_str # Output: true
str = "false"
bool_str = str.downcase == "true"
puts bool_str # Output: false
# To convert a number to a boolean, you can consider 0 as false and any non-zero number as true.
# There is no built-in method to directly convert a number to a boolean, but you can achieve this by using a simple conditional check. For example, you can consider 0 as false and any non-zero number as true:
num = 0
bool_num = num != 0
puts bool_num # Output: false
num = 5
bool_num = num != 0
puts bool_num # Output: true
# To convert a boolean to a number, you can use the `?` operator to return 1 for true and 0 for false.
# There is no built-in method to directly convert a boolean to a number, but you can achieve this using a simple conditional expression. For example, you can use the ternary operator to return 1 for true and 0 for false:
bool = true
num_bool = bool ? 1 : 0
puts num_bool # Output: 1
bool = false
num_bool = bool ? 1 : 0
puts num_bool # Output: 0
# To convert a range to an array, you can use the `to_a` method, which will convert the range into an array of its elements.
range = 1..5
arr_range = range.to_a
puts arr_range.inspect # Output: [1, 2, 3, 4, 5]
# To convert an array to a range, you can use the `min` and `max` methods to find the minimum and maximum values in the array, and then create a range using those values.
# There is no built-in method to directly convert an array to a range, but you can achieve this by using the `min` and `max` methods to find the minimum and maximum values in the array, and then create a range using those values. For example:
arr = [3, 1, 4, 2]
range_arr = arr.min..arr.max
puts range_arr.inspect # Output: 1..4
# For more information read the official documentation on Ruby's core classes and methods