Skip to content

Longest Word Method in Ruby #4030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions archive/r/ruby/longest-word.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Source on Block Arguments: https://ruby-for-beginners.rubymonstas.org/blocks/arguments.html

def longest_word_length(sentence)
# Converts string into an array of words
words = sentence.split(" ")

# Stores the word that has the longest length
longest_length = 0

# Iterate over each word
words.each do |word|
if word.length > longest_length
# Update the longest word length
longest_length = word.length
end
end
return longest_length
end

# Test and Output
sentence = "Swiftly running foxes"
output = longest_word_length(sentence)
puts "Longest word length: #{output}"

# Test 2
sentence2 = "Intricately interwoven \nphilosophical contemplations"
output2 = longest_word_length(sentence2)
puts "Longest word length: #{output2}"
Comment on lines +19 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this. We already have tests in our workflow. Also, please refer to the project infomation for details on the requirements. The program is supposed to take a single command-line argument.

Loading