forked from turingschool/methods_cfu_am0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilt_in_methods.rb
110 lines (93 loc) · 5.92 KB
/
built_in_methods.rb
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
# frozen_string_literal: true
# SECTION 1: Calling methods on string or integer objects.
# Run each line of code below (either from this file or in IRB individually).
# Then, in a ruby comment, write 1-2 sentences describing what is happening, using ALL the involved vocabulary terms
# you've learned in this lesson so far.
# EXAMPLE
# The downcase method is called on the string object "Hello World"
# No arguments are passed; downcase has one clear job which is to lowercase all letters that exist in the String
# The return value is "hello world"
'Hello World'.downcase
# The include? method is called on the string object "Hello World"
# The argument "Hello" is passed; include? will check if "Hello World" contains "Hello"
# The return value is true
'Hello World'.include?('Hello')
# The end_with? method is called on the string object "Hello World"
# The argument "Hello" is passed; end_with? will check if "Hello World" ends with "Hello"
# The return value is false
'Hello World'.end_with?('Hello')
# The end_with? method is called on the string object "Hello World"
# The argument "rld" is passed; end_with? will check if "Hello World" ends with "rld"
# The return value is true
'Hello World'.end_with?('rld')
# The even? method is called on the integer object 12
# No arguments are passed; even? has one clear job which is to check if the integer is even
# The return value is true
12.even?
# The next method is called on the integer object 18
# No arguments are passed; next has one clear job which is to find the next integer in the sequence
# The return value is 19
18.next
# SECTION 2: Calling methods on variables assigned to strings.
# Declare 2 variables assigned to string objects.
# Call a different built-in Ruby method on each of your variables.
# https://ruby-doc.org/core-3.1.0/String.html
# Include comments above each method call explaining the impact and return value of that method.
# EXAMPLE
# The start_with? method is called on the first_name variable, which stores the string object "Jeff".
# The start_with? method returns true if the data in the first_name variable starts with the argument passed in.
# In this example, the return value is true, because "Jeff" does start with "J".
# The puts command prints the return value of the start_with? method (true) to the console.
first_name = 'Jeff'
puts first_name.start_with?('J')
# The reverse method is called on the coder_status variable, which stores the string object "stressed".
# The reverse method returns the characters of the string in reverse order.
# In this example, the return value is desserts, because "stressed" backwards is "desserts".
# The puts command prints the return value of the reverse method (desserts) to the console.
coder_status = 'stressed'
puts coder_status.reverse
# The length method is called on the long_word variable, which stores the string object
# "supercalifragilisticexpialidocious".
# The length method returns an integer whose value is equal to the amount of characters in the string.
# In this example, the return value is 34, because "supercalifragilisticexpialidocious" has 34 characters.
# The puts command prints the return value of the length method (34) to the console.
long_word = 'supercalifragilisticexpialidocious'
puts long_word.length
# SECTION 3: Calling methods on variables assigned to integers.
# Declare 2 variables assigned to integer objects.
# Call a different built-in Ruby method on each of your variables.
# https://ruby-doc.org/core-3.1.0/Integer.html
# Include comments above each method call explaining the impact and return value of that method.
# The abs method is called on the negative_number variable, which stores the integer object -42.
# The abs method returns the absolute value of the integer passed in.
# In this example, the return value is 42, because the absolute value of -42 is 42.
# The puts command prints the return value of the abs method (42) to the console.
negative_number = -42
puts negative_number.abs
# The even? method is called on the odd_number variable, which stores the integer object 13.
# The even? method returns true if the data in the odd_number variable is even.
# In this example, the return value is false, because 13 is odd.
# The puts command prints the return value of the even? method (false) to the console.
odd_number = 13
puts odd_number.even?
# SECTION 4: Calling methods on variables assigned to arrays.
# Declare 2 variables assigned to arrays.
# Call a different built-in Ruby method on each of your variables.
# https://ruby-doc.org/core-3.1.0/Array.html
# Include comments above each method call explaining the impact and return value of that method.
# The any? method is called on the potluck_dishes variable, which stores the array object
# ['potato salad', 'deviled eggs', 'chicken tenders', 'cheesecake'].
# The any? method returns true if the data in the potluck_dishes variable contains the argument passed in.
# In this example, the return value is true, because potluck_dishes does contain a string with "chicken".
# The puts command prints the return value of the any? method (true) to the console.
potluck_dishes = ['potato salad', 'deviled eggs', 'chicken tenders', 'cheesecake']
puts potluck_dishes.any?(/chicken/)
# The /chicken/ uses slashes rather than quotes because it is a regular expression rather than a string. With quotes,
# any? would only search for 'chicken' as a whole string and would return false. This regular expression searches for
# chicken within each string in the array and finds the chicken within the 'chicken tenders' string, returning true.
# The count method is called on the somenumbers variable, which stores an array of integers (and a float for fun).
# The count method returns an integer of the amount of items that are equal to the argument passed in.
# In this example, the return value is 2, because there are 2 entries that are equal to 0 (0 and 0.0).
# The puts command prints the return value of the count method (2) to the console.
some_numbers = [0, 1, 2, 0.0]
puts some_numbers.count(0)