ruby
0 answers
I found this regular expression to match 2 or more digits in Ruby and then replace it:
a = %w(4 1 1 0 4 4 0 0 4 4 4)
a.join.gsub(/4{2,}/,'ab')
Output would be
Read more »
Created by Anonymous
18 weeks 3 days ago
Tags:
String comparison needs another section to understand:
There are 2 equal comparison conditionals for ruby strings as follows:
a. == returns true if two objects are strings.
b. eql? returns true if two strings are equal in length and content.
Ruby String Comparison
#!/usr/bin/ruby
#File: rubystringcomparisontest.rb
newstring = String.new("string comparison test")
newstring = newstring.downcase
newstring1 = String.new("string comparison test 2")
puts "#{newstring}"
puts "#{newstring1}"
puts newstring.eql?(newstring1)
Ruby hashes are equivalent to Java Hashmaps and allows us to store information as key-value pair in contrast to arrays in which we can have only integer keys or indices. Thus, Ruby hashes are generalization of arrays.
Ruby Hash Example
#!/usr/bin/ruby
# File: rubyhash.rb
address = {
"First name" => "Anthony",
"Last name" => "Moody",
"Street" => "101 LA Street",
"City" => "CA",
"Country" => "US"
}
puts address['City']
Output of the above code would be CA.
Ruby Hashes Iteration through Foreach
Ruby arrays are indexed as integers and starts at 0. Ruby arrays are collection of objects and can be refered by an integer index. Ruby arrays can hold any kind of object like String, Array, Hash etc and are dynamic in nature which means they need to be fixed when initializing.
Ruby Arrays Creation
#!/usr/bin/ruby
# File: arrayexample.rb
num1 = Array.new #initializes array with no fixed size.
num2 = [ "0", "1", "2", "3", "4" ] #creates an array with values 0,1,2,3,4
num3 = Array.new(4) #creates an array with size 4.
Ruby Arrays Example 1
Ruby Ifelse operator is a standard conditional operator that allows us to take a certain action based on certain condition.
Ruby IfElse Example
#!/usr/bin/ruby
# Filename: ifelse.rb
ruby_var = 1
if ruby_var == 2
puts "i am two"
else
puts "i am one"
Ruby If-ElseIf Example
#!/usr/bin/ruby
# Filename: ifelse.rb
ruby_var = 1
if ruby_var == 2
puts "i am two"
elseif ruby_var == 3
puts "i am three"
else
puts "i am one"
Conditions in Ruby
Ruby Comments
As seen in example Ruby Hello World, comments in ruby are done with pound (#) sign.
#!/usr/bin/ruby
# This is a comment1
# This is a comment2
# This is a comment3
We can have multiple line comments in Ruby as follows using =begin and =end.
#!/usr/bin/ruby
=begin
Ruby comment begins here
Ruby comment here
Ruby comment here
Ruby comment ends here
=end
Ruby is an interpreted language. It is object oriented. The code it requires to run is minimal and therefore, it is fast to write.
Ruby "Hello World"
#!/usr/bin/ruby
# Filename: helloworld.rb
puts "Hello World"
In the above code, puts is similar to print in other languages. First line tells the program where is ruby executable installed.
Ruby "Simple Arithmetic" Example
#!/usr/bin/ruby
# Filename: arithmetic.rb
num = 2 * 2
ruby_string_var = num.to_s
puts "Result after converting number to string = " + ruby_string_var
Ruby while loop is similar in syntax with other interpreted scripting languages.
Ruby While Loop Syntax
while cond
code to be executed
end
Ruby While Loop Example
ruby_variable = 0
while ruby_variable < 10
ruby_variable++;
puts ruby_variable
end