In [ ]:
Copied!
# Display Ruby version and information
puts "Ruby version: #{RUBY_VERSION}"
puts "Ruby platform: #{RUBY_PLATFORM}"
puts "Ruby engine: #{RUBY_ENGINE}"
# Display Ruby version and information
puts "Ruby version: #{RUBY_VERSION}"
puts "Ruby platform: #{RUBY_PLATFORM}"
puts "Ruby engine: #{RUBY_ENGINE}"
Ruby Features¶
Ruby is excellent for:
- Web Development (Ruby on Rails)
- Scripting and Automation
- DevOps and Configuration Management
- Rapid Prototyping
In [ ]:
Copied!
# Example: Working with classes and objects
class ProgrammingLanguage
attr_accessor :name, :type, :year_created
def initialize(name, type, year)
@name = name
@type = type
@year_created = year
end
def age
Time.now.year - @year_created
end
def to_s
"#{@name} (#{@type}) - Created in #{@year_created}, #{age} years old"
end
end
# Create language objects
languages = [
ProgrammingLanguage.new("Ruby", "Dynamic", 1995),
ProgrammingLanguage.new("Python", "Dynamic", 1991),
ProgrammingLanguage.new("Julia", "Dynamic", 2012),
ProgrammingLanguage.new("R", "Statistical", 1993)
]
puts "Programming Languages:"
languages.each { |lang| puts lang }
# Example: Working with classes and objects
class ProgrammingLanguage
attr_accessor :name, :type, :year_created
def initialize(name, type, year)
@name = name
@type = type
@year_created = year
end
def age
Time.now.year - @year_created
end
def to_s
"#{@name} (#{@type}) - Created in #{@year_created}, #{age} years old"
end
end
# Create language objects
languages = [
ProgrammingLanguage.new("Ruby", "Dynamic", 1995),
ProgrammingLanguage.new("Python", "Dynamic", 1991),
ProgrammingLanguage.new("Julia", "Dynamic", 2012),
ProgrammingLanguage.new("R", "Statistical", 1993)
]
puts "Programming Languages:"
languages.each { |lang| puts lang }
In [ ]:
Copied!
# Example: Ruby's elegant syntax
# Array operations
numbers = [1, 2, 3, 4, 5]
puts "Original array: #{numbers}"
puts "Doubled: #{numbers.map { |n| n * 2 }}"
puts "Even numbers: #{numbers.select(&:even?)}"
puts "Sum: #{numbers.reduce(:+)}"
# String manipulation
text = "ruby is awesome"
puts "\nString operations:"
puts "Capitalized: #{text.capitalize}"
puts "Reversed: #{text.reverse}"
puts "Word count: #{text.split.length}"
# Example: Ruby's elegant syntax
# Array operations
numbers = [1, 2, 3, 4, 5]
puts "Original array: #{numbers}"
puts "Doubled: #{numbers.map { |n| n * 2 }}"
puts "Even numbers: #{numbers.select(&:even?)}"
puts "Sum: #{numbers.reduce(:+)}"
# String manipulation
text = "ruby is awesome"
puts "\nString operations:"
puts "Capitalized: #{text.capitalize}"
puts "Reversed: #{text.reverse}"
puts "Word count: #{text.split.length}"
Ruby Blocks and Iterators¶
In [ ]:
Copied!
# Example: Blocks and yield
def measure_time
start_time = Time.now
yield if block_given?
end_time = Time.now
puts "Execution time: #{(end_time - start_time) * 1000} ms"
end
# Using the block
measure_time do
# Simulate some work
sum = 0
10000.times { |i| sum += i }
puts "Sum calculated: #{sum}"
end
# Custom iterator
class Fibonacci
def self.up_to(max)
a, b = 0, 1
while a <= max
yield a
a, b = b, a + b
end
end
end
puts "\nFibonacci numbers up to 100:"
Fibonacci.up_to(100) { |f| print "#{f} " }
puts
# Example: Blocks and yield
def measure_time
start_time = Time.now
yield if block_given?
end_time = Time.now
puts "Execution time: #{(end_time - start_time) * 1000} ms"
end
# Using the block
measure_time do
# Simulate some work
sum = 0
10000.times { |i| sum += i }
puts "Sum calculated: #{sum}"
end
# Custom iterator
class Fibonacci
def self.up_to(max)
a, b = 0, 1
while a <= max
yield a
a, b = b, a + b
end
end
end
puts "\nFibonacci numbers up to 100:"
Fibonacci.up_to(100) { |f| print "#{f} " }
puts
In [ ]:
Copied!
# Example: Metaprogramming
class DynamicClass
# Define methods dynamically
[:hello, :goodbye, :welcome].each do |method_name|
define_method(method_name) do |name|
"#{method_name.to_s.capitalize}, #{name}!"
end
end
end
obj = DynamicClass.new
puts obj.hello("World")
puts obj.goodbye("Friend")
puts obj.welcome("Ruby Developer")
# Example: Metaprogramming
class DynamicClass
# Define methods dynamically
[:hello, :goodbye, :welcome].each do |method_name|
define_method(method_name) do |name|
"#{method_name.to_s.capitalize}, #{name}!"
end
end
end
obj = DynamicClass.new
puts obj.hello("World")
puts obj.goodbye("Friend")
puts obj.welcome("Ruby Developer")
Data Visualization in Ruby¶
While Ruby doesn't have as many data visualization libraries as Python, you can still create visualizations:
In [ ]:
Copied!
# Example: Simple ASCII chart
def ascii_bar_chart(data)
max_value = data.values.max
max_label_length = data.keys.map(&:length).max
data.each do |label, value|
bar_length = (value.to_f / max_value * 30).round
bar = "#" * bar_length
printf "%-#{max_label_length}s | %s %d\n", label, bar, value
end
end
language_popularity = {
"Python" => 100,
"Ruby" => 60,
"Julia" => 30,
"R" => 50
}
puts "Language Popularity (Jupyter Kernel Usage):"
ascii_bar_chart(language_popularity)
# Example: Simple ASCII chart
def ascii_bar_chart(data)
max_value = data.values.max
max_label_length = data.keys.map(&:length).max
data.each do |label, value|
bar_length = (value.to_f / max_value * 30).round
bar = "#" * bar_length
printf "%-#{max_label_length}s | %s %d\n", label, bar, value
end
end
language_popularity = {
"Python" => 100,
"Ruby" => 60,
"Julia" => 30,
"R" => 50
}
puts "Language Popularity (Jupyter Kernel Usage):"
ascii_bar_chart(language_popularity)
Installing Ruby Gems¶
You can install Ruby gems directly from a notebook cell:
In [ ]:
Copied!
# Example of installing gems (commented out to avoid actual installation)
# system('gem install json')
# system('gem install httparty')
# system('gem install nokogiri')
puts "Use system('gem install <gem_name>') to install Ruby gems"
# Example of installing gems (commented out to avoid actual installation)
# system('gem install json')
# system('gem install httparty')
# system('gem install nokogiri')
puts "Use system('gem install ') to install Ruby gems"
Summary¶
The Ruby kernel (IRuby) provides:
- Full access to Ruby's elegant syntax
- Object-oriented programming features
- Metaprogramming capabilities
- Access to Ruby gems ecosystem
- Great for web development and scripting
While less common than Python for data science, Ruby in Jupyter is excellent for:
- Teaching Ruby programming
- Rapid prototyping
- DevOps scripting
- Web API exploration