Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,4 @@
def foo() p "foo" end
foo #=> "foo"
foo() #=> "foo"

View file

@ -0,0 +1,13 @@
# return value substance
i = 3
p 1 + i #=> 4 1.+(i)
p i < 5 #=> true i.<(5)
p 2 ** i #=> 8 2.**(i)
p -i #=> -3 i.-@()
a = [1,2,3]
p a[0] #=> 1 a.[](0)
a[2] = "0" # a.[]=(2,"0")
p a << 5 #=> [1, 2, "0", 5] a.<<(5)
p a & [4,2] #=> [2] a.&([4,2])
p "abcde"[1..3] #=> "bcd" "abcde".[](1..3)
p "%2d %4s" % [1,"xyz"] #=> " 1 xyz" "%2d %4s".%([1,"xyz"])

View file

@ -0,0 +1,5 @@
def foo arg; p arg end # one argument
foo(1) #=> 1
foo "1" #=> "1"
foo [0,1,2] #=> [0, 1, 2] (one Array)

View file

@ -0,0 +1,6 @@
def foo(x=0, y=x, flag=true) p [x,y,flag] end
foo #=> [0, 0, true]
foo(1) #=> [1, 1, true]
foo(1,2) #=> [1, 2, true]
foo 1,2,false #=> [1, 2, false]

View file

@ -0,0 +1,4 @@
def foo(*args) p args end
foo #=> []
foo(1,2,3,4,5) #=> [1, 2, 3, 4, 5]

View file

@ -0,0 +1,3 @@
def foo(id:0, name:"", age:0) p [id, name, age] end
foo(age:22, name:"Tom") #=> [0, "Tom", 22]

View file

@ -0,0 +1,12 @@
def foo(a,b) a + b end
bar = foo 10,20
p bar #=> 30
p foo("abc","def") #=> "abcdef"
# return multiple values
def sum_and_product(a,b) return a+b,a*b end
x,y = sum_and_product(3,5)
p x #=> 8
p y #=> 15

View file

@ -0,0 +1,12 @@
puts "OK!" # Kernel#puts
raise "Error input" # Kernel#raise
Integer("123") # Kernel#Integer
rand(6) # Kernel#rand
throw(:exit) # Kernel#throw
# method which can be seen like a reserved word.
attr_accessor # Module#attr_accessor
include # Module#include
private # Module#private
require # Kernel#require
loop { } # Kernel#loop

View file

@ -0,0 +1,14 @@
class Array
def sum(init=0, &blk)
if blk
inject(init){|s, n| s + blk.call(n)}
else
inject(init){|s, n| s + n}
end
end
end
ary = [1,2,3,4,5]
p ary.sum #=> 15
p ary.sum(''){|n| (-n).to_s} #=> "-1-2-3-4-5"
p (ary.sum do |n| n * n end) #=> 55

View file

@ -0,0 +1,6 @@
def foo(a,b,c) p [a,b,c] end
args = [1,2,3]
foo *args #=> [1, 2, 3]
args = [1,2]
foo(0,*args) #=> [0, 1, 2]