var = 'four'
a = %W{one two three
println a ["one", "two", "three", "four"]
require 'json'
h = {a:1, b:2, c:3}
p h {:a=>1, :b=>2, :c=>3}
h = JSON.parse(h.to_json)
p h {"a"=>1, "b"=>2, "c"=>3}
key = 'the-key'
h = {key => 'the-value'}
println h {"the-key"=>"the-value"}
key.chop!
p key the-ke
println h {"the-key"=>"the-value"}
MyConstant = 'red'
MyConstant.gsub! /^.*$/, 'blue'
p MyConstant blue
MyConstant.freeze
MyConstant.gsub! /^.*$/, 'green' can't modify frozen String
MyConstant = 'red'.freeze
class Top
def hi(name)
puts 'here in Top ' + name
end
end
class Bottom < Top
def hi(name)
super
puts 'here in Bottom ' + name
end
end
Bottom.new.hi('kevin')
here in Top kevin
here in Bottom kevin
a = 'outside'
1.times do
puts a outside
end
puts a outside
1.times do
b = 'inside'
puts b inside
end
puts b undefined local variable or method `b' for main:Object (NameError)
c = 'outside'
1.times do |;c|
c = 'inside'
puts c inside
end
puts c outside
class MySingletonArray
@data = []
class << self
def [](*indices)
@data[*indices]
end
def []=(*indices, value)
@data[*indices] = value
end
end
end
MySingletonArray[1] = :one
MySingletonArray[2] = :two
MySingletonArray[3] = :three
p MySingletonArray[1] :one
p MySingletonArray[2] :two
p MySingletonArray[3] :three
p MySingletonArray[1..3] [:one, :two, :three]
begin
a = "in 'block'"
end
puts a "in block"
1.times do
b = "in 'block'"
end
puts b undefined local variable or method `b' for main:Object (NameError)
def add_it(x)
$gAdd = x
def add_it(x)
$gAdd + x
end
end
add_it(5)
puts add_it(1) 6
puts add_it(2) 7
puts add_it(3) 8
def myf(args)
println args
end
myf(a:1, b:2) {:a=>1, :b=>2}
def myf(*args)
println args
end
myf(a:1, b:2) {:a=>1, :b=>2}
def myf(a:undefined, b:undefined)
println a, b
end
myf(a:1, b:2) 1 2
class MyClass; end
class << MyClass
def hello
puts :hello
end
end
p MyClass.hello hello
obj = MyClass.new
class << obj
def goodbye
puts :goodbye
end
end
obj.goodbye goodbye
class One
def self.macro
puts 'in macro'
end
end
class Two < One
macro
end
class MyClass
def self.new
end
end
MyClass.new nil
module MyMod
def hello
puts :hello
end
end
class MyClass
include MyMod
end
MyClass.new.hello hello
def ten_times
10.times do |i|
if yield(i)
puts "Caller likes
end
end
end
ten_times do |number|
next(true) if number ==7
end
myproc = Proc.new do
return :goodbye
end
p myproc.call unexpected return (LocalJumpError)
myproc = Proc.new do
next :goodbye
end
p myproc.call :goodbye
def func
raise 'my error'
puts 'in func'
rescue StandardError => e
puts 'in rescue ' + e.message
raise
else
puts 'runs if nothing raised'
ensure
puts 'will always run'
end
raise 'whoops' rescue puts 'caught it' caught it
Integer('1.1') invalid value for Integer(): "1.1" (ArgumentError)
v = Integer('1.1') rescue Float('1.1')
puts v 1.1
x=0; accum = []
begin
x += 1
raise if x < 5
rescue
accum << x
retry
end
println accum 1 2 3 4
a = %w{one two three}
class Symbol
def to_proc(*args)
Proc.new do |obj, *args|
obj.send(self, *args)
end
end
end
class String
def splitter
self.split ''
end
end
p a.map(&:splitter)
class MyClass
end
singleton = class << MyClass
def self.hello
puts "defined in singleton"
end
self
end
singleton.hello
module MyMod
def hello
puts 'hello'
super
end
end
class MyClass
prepend MyMod
def hello
puts 'hello again'
end
end
MyClass.new.hello
hello
hello again
module MyMod
def here
puts :here
end
end
obj = []
obj.extend MyMod
obj.here here
class MyClassExtended
extend MyMod
end
MyClassExtended.here here
class MyClassIncluded
include MyMod
end
MyClassIncluded.new.here here
module AttrLogger
def attr_logger(x)
attr_accessor x
define_method(") do |val|
puts "assigned
instance_variable_set("@, val)
end
end
end
class MyClass
extend AttrLogger
attr_logger :var
end
obj = MyClass.new
obj.var = 2 assigned 2
p obj.var 2
module MyMod
def my_instance_method
puts 'in my_instance_method'
end
module ClassMethods
def my_class_method
puts "in my_class_method"
end
end
def self.included(host_class)
host_class.extend(ClassMethods)
end
end
class MyClass
include MyMod
end
MyClass.my_class_method in my_class_method
MyClass.new.my_instance_method in my_instance_method
my_class = Class.new do
def hello
puts :hello
end
end
my_class.new.hello hello
my_class = Class.new(String) do
def up
self.upcase!
end
end
my_str = my_class.new('this is a string')
my_str.up
p my_str THIS IS A STRING
def SayHi(name)
Class.new do
@name = name.capitalize
class << self
attr_accessor :name
end
def speak
puts "hi
end
end
end
SayHi('kevin').new.speak hi Kevin