mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-06 06:48:32 +00:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
3bfea3740c
122
asciidoc.html.markdown
Normal file
122
asciidoc.html.markdown
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
---
|
||||||
|
language: asciidoc
|
||||||
|
contributors:
|
||||||
|
- ["Ryan Mavilia", "http://unoriginality.rocks/"]
|
||||||
|
filename: asciidoc.md
|
||||||
|
---
|
||||||
|
|
||||||
|
AsciiDoc is a markup language similar to Markdown and it can be used for anything from books to blogs. Created in 2002 by Stuart Rackham the language is simple but it allows for a great amount of customization.
|
||||||
|
|
||||||
|
Document Header
|
||||||
|
|
||||||
|
Headers are optional and can't contain blank lines. It must be offset from content by at least one blank line.
|
||||||
|
|
||||||
|
Title Only
|
||||||
|
|
||||||
|
```
|
||||||
|
= Document Title
|
||||||
|
|
||||||
|
First sentence of document.
|
||||||
|
```
|
||||||
|
|
||||||
|
Title and Author
|
||||||
|
|
||||||
|
```
|
||||||
|
= Document Title
|
||||||
|
First Last <first.last@learnxinyminutes.com>
|
||||||
|
|
||||||
|
Start of this document.
|
||||||
|
```
|
||||||
|
|
||||||
|
Multiple Authors
|
||||||
|
|
||||||
|
```
|
||||||
|
= Document Title
|
||||||
|
John Doe <john@go.com>; Jane Doe<jane@yo.com>; Black Beard <beardy@pirate.com>
|
||||||
|
|
||||||
|
Start of a doc with multiple authors.
|
||||||
|
```
|
||||||
|
|
||||||
|
Revision Line (requires an author line)
|
||||||
|
|
||||||
|
```
|
||||||
|
= Doc Title V1
|
||||||
|
Potato Man <chip@crunchy.com>
|
||||||
|
v1.0, 2016-01-13
|
||||||
|
|
||||||
|
This article about chips is going to be fun.
|
||||||
|
```
|
||||||
|
|
||||||
|
Paragraphs
|
||||||
|
|
||||||
|
```
|
||||||
|
You don't need anything special for paragraphs.
|
||||||
|
|
||||||
|
Add a blank line between paragraphs to seperate them.
|
||||||
|
|
||||||
|
To create a line blank add a +
|
||||||
|
and you will recieve a line break!
|
||||||
|
```
|
||||||
|
|
||||||
|
Formatting Text
|
||||||
|
|
||||||
|
```
|
||||||
|
_underscore creates italics_
|
||||||
|
*asterisks for bold*
|
||||||
|
*_combine for extra fun_*
|
||||||
|
`use ticks to signify monospace`
|
||||||
|
`*bolded monospace*`
|
||||||
|
```
|
||||||
|
|
||||||
|
Section Titles
|
||||||
|
|
||||||
|
```
|
||||||
|
= Level 0 (may only be used in document's header)
|
||||||
|
|
||||||
|
== Level 1 <h2>
|
||||||
|
|
||||||
|
=== Level 2 <h3>
|
||||||
|
|
||||||
|
==== Level 3 <h4>
|
||||||
|
|
||||||
|
===== Level 4 <h5>
|
||||||
|
|
||||||
|
====== Level 5 <h6>
|
||||||
|
|
||||||
|
======= Level 6 <h7>
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Lists
|
||||||
|
|
||||||
|
To create a bulleted list use asterisks.
|
||||||
|
|
||||||
|
```
|
||||||
|
* foo
|
||||||
|
* bar
|
||||||
|
* baz
|
||||||
|
```
|
||||||
|
|
||||||
|
To create a numbered list use periods.
|
||||||
|
|
||||||
|
```
|
||||||
|
. item 1
|
||||||
|
. item 2
|
||||||
|
. item 3
|
||||||
|
```
|
||||||
|
|
||||||
|
You can nest lists by adding extra asterisks or periods up to five times.
|
||||||
|
|
||||||
|
```
|
||||||
|
* foo 1
|
||||||
|
** foo 2
|
||||||
|
*** foo 3
|
||||||
|
**** foo 4
|
||||||
|
***** foo 5
|
||||||
|
|
||||||
|
. foo 1
|
||||||
|
.. foo 2
|
||||||
|
... foo 3
|
||||||
|
.... foo 4
|
||||||
|
..... foo 5
|
||||||
|
```
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
||||||
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
@ -14,7 +14,7 @@ Clojure, there are implementations of EDN for many other languages.
|
|||||||
The main benefit of EDN over JSON and YAML is that it is extensible. We
|
The main benefit of EDN over JSON and YAML is that it is extensible. We
|
||||||
will see how it is extended later on.
|
will see how it is extended later on.
|
||||||
|
|
||||||
```Clojure
|
```clojure
|
||||||
; Comments start with a semicolon.
|
; Comments start with a semicolon.
|
||||||
; Anything after the semicolon is ignored.
|
; Anything after the semicolon is ignored.
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
||||||
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
contributors:
|
contributors:
|
||||||
- ["Mohammad Valipour", "https://github.com/mvalipour"]
|
- ["Mohammad Valipour", "https://github.com/mvalipour"]
|
||||||
lang: fa-ir
|
lang: fa-ir
|
608
fi-fi/ruby-fi.html.markdown
Normal file
608
fi-fi/ruby-fi.html.markdown
Normal file
@ -0,0 +1,608 @@
|
|||||||
|
---
|
||||||
|
language: ruby
|
||||||
|
filename: learnruby-fi.rb
|
||||||
|
contributors:
|
||||||
|
- ["David Underwood", "http://theflyingdeveloper.com"]
|
||||||
|
- ["Joel Walden", "http://joelwalden.net"]
|
||||||
|
- ["Luke Holder", "http://twitter.com/lukeholder"]
|
||||||
|
- ["Tristan Hume", "http://thume.ca/"]
|
||||||
|
- ["Nick LaMuro", "https://github.com/NickLaMuro"]
|
||||||
|
- ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"]
|
||||||
|
- ["Ariel Krakowski", "http://www.learneroo.com"]
|
||||||
|
- ["Dzianis Dashkevich", "https://github.com/dskecse"]
|
||||||
|
- ["Levi Bostian", "https://github.com/levibostian"]
|
||||||
|
- ["Rahil Momin", "https://github.com/iamrahil"]
|
||||||
|
- ["Gabriel Halley", "https://github.com/ghalley"]
|
||||||
|
- ["Persa Zula", "http://persazula.com"]
|
||||||
|
- ["Jake Faris", "https://github.com/farisj"]
|
||||||
|
translators:
|
||||||
|
- ["Oliver Vartiainen", "https://github.com/firoxer"]
|
||||||
|
lang: fi-fi
|
||||||
|
---
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
# Tässä yhden rivin kommentti
|
||||||
|
|
||||||
|
=begin
|
||||||
|
Tässä usean rivin kommentti
|
||||||
|
Näitä ei kylläkään käytetä
|
||||||
|
Joten käytetään vastedes vain yksirivisiä
|
||||||
|
=end
|
||||||
|
|
||||||
|
# Tärkeintä on muistaa, että Rubyssa kaikki pohjautuu olioihin.
|
||||||
|
|
||||||
|
# Luvutkin ovat olioita:
|
||||||
|
|
||||||
|
3.class #=> Fixnum
|
||||||
|
|
||||||
|
3.to_s #=> "3"
|
||||||
|
|
||||||
|
# Peruslaskutoimituksia:
|
||||||
|
1 + 1 #=> 2
|
||||||
|
8 - 1 #=> 7
|
||||||
|
10 * 2 #=> 20
|
||||||
|
35 / 5 #=> 7
|
||||||
|
2**5 #=> 32
|
||||||
|
5 % 3 #=> 2
|
||||||
|
|
||||||
|
# Bittioperaatioita:
|
||||||
|
3 & 5 #=> 1
|
||||||
|
3 | 5 #=> 7
|
||||||
|
3 ^ 5 #=> 6
|
||||||
|
|
||||||
|
# Laskutoimitukset ovat vain syntaksisokeria lukuolion laskumetodin kutsulle:
|
||||||
|
1.+(3) #=> 4
|
||||||
|
10.* 5 #=> 50
|
||||||
|
|
||||||
|
# Erityisarvotkin ovat olioita:
|
||||||
|
|
||||||
|
nil # vastaa joidenkin kielten "null"-arvoa
|
||||||
|
true # tosi
|
||||||
|
false # epätosi
|
||||||
|
|
||||||
|
nil.class #=> NilClass
|
||||||
|
true.class #=> TrueClass
|
||||||
|
false.class #=> FalseClass
|
||||||
|
|
||||||
|
# Samanvertaisuuden testaus:
|
||||||
|
1 == 1 #=> true
|
||||||
|
2 == 1 #=> false
|
||||||
|
|
||||||
|
# ...ja sama eriarvoisuudelle:
|
||||||
|
1 != 1 #=> false
|
||||||
|
2 != 1 #=> true
|
||||||
|
|
||||||
|
# "nil" ja "false" ovat ainoat epätodet arvot; kaikki muu ymmärretään todeksi:
|
||||||
|
!nil #=> true
|
||||||
|
!false #=> true
|
||||||
|
!0 #=> false
|
||||||
|
|
||||||
|
# Lisää vertailuoperaatioita:
|
||||||
|
1 < 10 #=> true
|
||||||
|
1 > 10 #=> false
|
||||||
|
2 <= 2 #=> true
|
||||||
|
2 >= 2 #=> true
|
||||||
|
|
||||||
|
# Kahdensuuntainen vertailuoperaattori:
|
||||||
|
1 <=> 10 #=> -1
|
||||||
|
10 <=> 1 #=> 1
|
||||||
|
1 <=> 1 #=> 0
|
||||||
|
|
||||||
|
# Logiikkaoperaattorit:
|
||||||
|
true && false #=> false
|
||||||
|
true || false #=> true
|
||||||
|
!true #=> false
|
||||||
|
|
||||||
|
# Merkkipohjaisten logiikkaoperaattorien vaihtoehtona on sanalliset muodot,
|
||||||
|
# joilla on hyvin matala presedenssi. Niillä voi muokata ohjelman kulkua
|
||||||
|
# esimerkiksi väitelausekkeita ketjuttaen.
|
||||||
|
|
||||||
|
# Metodia `do_something_else` kutsutaan vain, jos `do_something` onnistuu:
|
||||||
|
do_something() and do_something_else()
|
||||||
|
# Metodia `log_error` kutsutaan vain, jos `do_something` epäonnistuu:
|
||||||
|
do_something() or log_error()
|
||||||
|
|
||||||
|
# Merkkijonot ovat olioita:
|
||||||
|
|
||||||
|
'Tässä on merkkijono'.class #=> String
|
||||||
|
"Rajaavat lainausmerkit voivat olla yksin- tai kaksinkertaisia".class #=> String
|
||||||
|
|
||||||
|
täyte = 'sisällyttää muita merkkijonoja'
|
||||||
|
"Kaksinkertaisilla lainausmerkeillä voi #{täyte}"
|
||||||
|
#=> "Kaksinkertaisilla lainausmerkeillä voi sisällyttää muita merkkijonoja"
|
||||||
|
|
||||||
|
# Yksinkertaisia lainausmerkkejä kannattaa silti suosia, sillä kaksinkertaiset
|
||||||
|
# merkit saattavat aiheuttaa turhia kielensisäisiä tarkistuksia.
|
||||||
|
|
||||||
|
# Merkkijonoja voi yhdistellä toisiinsa:
|
||||||
|
'hello ' + 'world' #=> "hello world"
|
||||||
|
|
||||||
|
# ...mutta luvut vaativat ensin tyyppimuunnoksen:
|
||||||
|
'hello ' + 3 #=> TypeError: can't convert Fixnum into String
|
||||||
|
'hello ' + 3.to_s #=> "hello 3"
|
||||||
|
|
||||||
|
# Merkkijonoja voi soveltaa laskutoimituksiin... odotettavin seurauksin:
|
||||||
|
'hello ' * 3 #=> "hello hello hello "
|
||||||
|
|
||||||
|
# Merkkijonoa voi jatkaa toisella:
|
||||||
|
'hello' << ' world' #=> "hello world"
|
||||||
|
|
||||||
|
# Tulosteen luonti kera rivinvaihdon:
|
||||||
|
puts "I'm printing!"
|
||||||
|
#=> I'm printing!
|
||||||
|
#=> nil
|
||||||
|
|
||||||
|
# ...ja ilman rivinvaihtoa:
|
||||||
|
print "I'm printing!"
|
||||||
|
#=> I'm printing! => nil
|
||||||
|
|
||||||
|
# Muuttujien määrittely:
|
||||||
|
x = 25 #=> 25
|
||||||
|
x #=> 25
|
||||||
|
|
||||||
|
# Arvon asettaminen palauttaa arvon itsensä, joten usean muuttujan arvon
|
||||||
|
# yhtäaikainen määrittely käy vaivatta:
|
||||||
|
x = y = 10 #=> 10
|
||||||
|
x #=> 10
|
||||||
|
y #=> 10
|
||||||
|
|
||||||
|
# Muuttujien sanaerottimena käytetään alaviivaa:
|
||||||
|
snake_case = true
|
||||||
|
|
||||||
|
# Lisäksi Rubyssa suositaan ytimekkäitä nimiä:
|
||||||
|
path_to_project_root = '/good/name/'
|
||||||
|
path = '/bad/name/'
|
||||||
|
|
||||||
|
# Symbolit
|
||||||
|
|
||||||
|
# Symbolit ovat muuttumattomia, uudelleenkäytettäviä vakioita.
|
||||||
|
# Niitä käytetään merkkijonojen sijaan, kun tarkoitus on viitata arvoon,
|
||||||
|
# jolla on tietty, pysyvä merkitys:
|
||||||
|
|
||||||
|
:pending.class #=> Symbol
|
||||||
|
|
||||||
|
status = :pending
|
||||||
|
|
||||||
|
status == :pending #=> true
|
||||||
|
|
||||||
|
status == 'pending' #=> false
|
||||||
|
|
||||||
|
status == :approved #=> false
|
||||||
|
|
||||||
|
# Taulukot
|
||||||
|
|
||||||
|
# Tässä taulukko:
|
||||||
|
array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]
|
||||||
|
|
||||||
|
# Taulukko saa koostua erityyppisistä arvoista:
|
||||||
|
[1, 'hello', false] #=> [1, "hello", false]
|
||||||
|
|
||||||
|
# Taulukon alkioihin voi viitata järjestysnumerolla nollasta alkaen:
|
||||||
|
array[0] #=> 1
|
||||||
|
array.first #=> 1
|
||||||
|
array[12] #=> nil
|
||||||
|
|
||||||
|
# Kuten laskutoimituksissa nähty syntaksisokeri on myös taulukon alkioiden haku
|
||||||
|
# pohjimmiltaan vain taulukko-olioon kuuluvan "[]"-metodin kutsu:
|
||||||
|
array.[] 0 #=> 1
|
||||||
|
array.[] 12 #=> nil
|
||||||
|
|
||||||
|
# Haku käy myös lopustapäin:
|
||||||
|
array[-1] #=> 5
|
||||||
|
array.last #=> 5
|
||||||
|
|
||||||
|
# Alitaulukon haku käy indeksiparilla...
|
||||||
|
array[2, 3] #=> [3, 4, 5]
|
||||||
|
|
||||||
|
# ...tai määrittelemällä väli:
|
||||||
|
array[1..3] #=> [2, 3, 4]
|
||||||
|
|
||||||
|
# Taulukon voi kääntää:
|
||||||
|
a=[1,2,3]
|
||||||
|
a.reverse! #=> [3,2,1]
|
||||||
|
|
||||||
|
# Ja sitä voi jatkaa näin...
|
||||||
|
array << 6 #=> [1, 2, 3, 4, 5, 6]
|
||||||
|
|
||||||
|
# ...tai näin:
|
||||||
|
array.push(6) #=> [1, 2, 3, 4, 5, 6]
|
||||||
|
|
||||||
|
# Alkion olemassaolon tarkistus:
|
||||||
|
array.include?(1) #=> true
|
||||||
|
|
||||||
|
# Hashit eli assosiaatiotaulut ovat Rubyn tärkein avain-/arvoparirakenne.
|
||||||
|
# Hash luodaan aaltosulkeilla:
|
||||||
|
hash = { 'color' => 'green', 'number' => 5 }
|
||||||
|
|
||||||
|
hash.keys #=> ['color', 'number']
|
||||||
|
|
||||||
|
# Hash toimii erityisen nopeasti, kun haetaan arvoa avaimen perusteella:
|
||||||
|
hash['color'] #=> 'green'
|
||||||
|
hash['number'] #=> 5
|
||||||
|
|
||||||
|
# Jos hashistä ei löyty avainta vastaavaa arvoa, palautetaan nil-arvo:
|
||||||
|
hash['nothing here'] #=> nil
|
||||||
|
|
||||||
|
# Symbolihashin määrittelylle on oma syntaksinsa (alkaen Rubyn versiosta 1.9):
|
||||||
|
new_hash = { defcon: 3, action: true }
|
||||||
|
new_hash.keys #=> [:defcon, :action]
|
||||||
|
|
||||||
|
# Hashin avaimen ja arvon olemassaolon tarkistus:
|
||||||
|
new_hash.key?(:defcon) #=> true
|
||||||
|
new_hash.value?(3) #=> true
|
||||||
|
|
||||||
|
# Vinkki! Sekä taulukot että hashit sisältävät Enumerable-moduulin,
|
||||||
|
# johon kuuluu useita hyödyllisiä iterointimetodeja kuten .each, .map,
|
||||||
|
# .reduce ja .count
|
||||||
|
|
||||||
|
# Rakenteita
|
||||||
|
|
||||||
|
if true
|
||||||
|
'if statement'
|
||||||
|
elsif false
|
||||||
|
'else if, optional'
|
||||||
|
else
|
||||||
|
'else, also optional'
|
||||||
|
end
|
||||||
|
|
||||||
|
for counter in 1..5
|
||||||
|
puts "iteration #{counter}"
|
||||||
|
end
|
||||||
|
#=> iteration 1
|
||||||
|
#=> iteration 2
|
||||||
|
#=> iteration 3
|
||||||
|
#=> iteration 4
|
||||||
|
#=> iteration 5
|
||||||
|
|
||||||
|
# HUOMAA, että for-rakennetta kannattaa välttää, sillä Rubyssa suosittu
|
||||||
|
# each-metodi ajaa saman asian idiomaattisemmin. Each-metodi ottaa ainoana
|
||||||
|
# argumenttinaan lohkon. Lohkot toimivat pitkälti samoin kuin muiden kielten
|
||||||
|
# anonyymit funktiot, lambdat tai sulkeumat.
|
||||||
|
|
||||||
|
# Lukuvälit vastaavat each-metodiin, jolloin sille annettu lohko ajetaan
|
||||||
|
# kerran jokaiselle välin kokonaisluvulle.
|
||||||
|
# Lukuvälin each-rakenne lohkoineen näyttää tältä:
|
||||||
|
|
||||||
|
(1..5).each do |counter|
|
||||||
|
puts "iteration #{counter}"
|
||||||
|
end
|
||||||
|
#=> iteration 1
|
||||||
|
#=> iteration 2
|
||||||
|
#=> iteration 3
|
||||||
|
#=> iteration 4
|
||||||
|
#=> iteration 5
|
||||||
|
|
||||||
|
# Lohkoa ympäröivät do/end-avainsanat voi korvata myös aaltosulkeilla:
|
||||||
|
(1..5).each { |counter| puts "iteration #{counter}" }
|
||||||
|
|
||||||
|
# Lukuvälien lisäksi myös tietorakenteita voidaan iteroida each-metodilla:
|
||||||
|
array.each do |element|
|
||||||
|
puts "#{element} is part of the array"
|
||||||
|
end
|
||||||
|
hash.each do |key, value|
|
||||||
|
puts "#{key} is #{value}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Taulukoita voi iteroida metodilla each_with_index, jolloin lohko saa
|
||||||
|
# argumenteikseen sekä alkion että indeksin:
|
||||||
|
array.each_with_index do |element, index|
|
||||||
|
puts "#{element} is number #{index} in the array"
|
||||||
|
end
|
||||||
|
|
||||||
|
counter = 1
|
||||||
|
while counter <= 5 do
|
||||||
|
puts "iteration #{counter}"
|
||||||
|
counter += 1
|
||||||
|
end
|
||||||
|
#=> iteration 1
|
||||||
|
#=> iteration 2
|
||||||
|
#=> iteration 3
|
||||||
|
#=> iteration 4
|
||||||
|
#=> iteration 5
|
||||||
|
|
||||||
|
# Each-metodin lisäksi Rubyssa on useita muita iterointimetodeja kuten
|
||||||
|
# "map" ja "reduce". Näistä "map" kutsuttuna taulukolla ottaa argumentikseen
|
||||||
|
# lohkon, suorittaa sen kerran jokaiselle rakenteen jäsenelle, ja lopuksi
|
||||||
|
# palauttaa uuden taulukon, jonka jäsenet ovat lohkon suorituksen tuloksia.
|
||||||
|
|
||||||
|
array = [1, 2, 3, 4, 5]
|
||||||
|
doubled = array.map do |element|
|
||||||
|
element * 2
|
||||||
|
end
|
||||||
|
puts doubled
|
||||||
|
#=> [2,4,6,8,10]
|
||||||
|
puts array
|
||||||
|
#=> [1,2,3,4,5]
|
||||||
|
|
||||||
|
# Case-rakenne siirtää ohjelman kulun yhdelle monista määritellyistä poluista:
|
||||||
|
|
||||||
|
grade = 'B'
|
||||||
|
|
||||||
|
case grade
|
||||||
|
when 'A'
|
||||||
|
puts 'Way to go kiddo'
|
||||||
|
when 'B'
|
||||||
|
puts 'Better luck next time'
|
||||||
|
when 'C'
|
||||||
|
puts 'You can do better'
|
||||||
|
when 'D'
|
||||||
|
puts 'Scraping through'
|
||||||
|
when 'F'
|
||||||
|
puts 'You failed!'
|
||||||
|
else
|
||||||
|
puts 'Alternative grading system, eh?'
|
||||||
|
end
|
||||||
|
#=> "Better luck next time"
|
||||||
|
|
||||||
|
# Case-rakenteessa voidaan hyödyntää lukuvälejä:
|
||||||
|
grade = 82
|
||||||
|
case grade
|
||||||
|
when 90..100
|
||||||
|
puts 'Hooray!'
|
||||||
|
when 80...90
|
||||||
|
puts 'OK job'
|
||||||
|
else
|
||||||
|
puts 'You failed!'
|
||||||
|
end
|
||||||
|
#=> "OK job"
|
||||||
|
|
||||||
|
# Virheidenkäsittely:
|
||||||
|
begin
|
||||||
|
# Seuraava koodinpätkä aiheuttaa NoMemoryError-poikkeuksen
|
||||||
|
raise NoMemoryError, 'You ran out of memory.'
|
||||||
|
rescue NoMemoryError => exception_variable
|
||||||
|
puts 'NoMemoryError was raised', exception_variable
|
||||||
|
rescue RuntimeError => other_exception_variable
|
||||||
|
puts 'RuntimeError was raised now'
|
||||||
|
else
|
||||||
|
puts 'This runs if no exceptions were thrown at all'
|
||||||
|
ensure
|
||||||
|
puts 'This code always runs no matter what'
|
||||||
|
end
|
||||||
|
|
||||||
|
# Ylimmän näkyvyysalueen metodi näyttää itsenäiseltä funktiolta:
|
||||||
|
def double(x)
|
||||||
|
x * 2
|
||||||
|
end
|
||||||
|
|
||||||
|
# Funktiot (ja lohkot) palauttavat implisiittisesti
|
||||||
|
# viimeiseksi ajamansa lausekkeen arvon:
|
||||||
|
double(2) #=> 4
|
||||||
|
|
||||||
|
# Metodikutsun argumentteja ympäröivät kaarisulkeet voi jättää pois,
|
||||||
|
# kunhan koodi ei muutu monitulkintaiseksi:
|
||||||
|
|
||||||
|
double 3 #=> 6
|
||||||
|
|
||||||
|
double double 3 #=> 12
|
||||||
|
|
||||||
|
def sum(x, y)
|
||||||
|
x + y
|
||||||
|
end
|
||||||
|
|
||||||
|
# Argumentit erotetaan pilkuilla:
|
||||||
|
|
||||||
|
sum 3, 4 #=> 7
|
||||||
|
|
||||||
|
sum sum(3, 4), 5 #=> 12
|
||||||
|
|
||||||
|
# Kaikilla metodeilla on implisiittinen lohkoparametri,
|
||||||
|
# joka voidaan suorittaa yield-avainsanalla:
|
||||||
|
|
||||||
|
def surround
|
||||||
|
puts '{'
|
||||||
|
yield
|
||||||
|
puts '}'
|
||||||
|
end
|
||||||
|
|
||||||
|
surround { puts 'hello world' }
|
||||||
|
|
||||||
|
# {
|
||||||
|
# hello world
|
||||||
|
# }
|
||||||
|
|
||||||
|
# Metodille annetun lohkon voi nimetä parametrilistassa &-merkin avulla,
|
||||||
|
# minkä jälkeen se suoritetaan call-metodilla:
|
||||||
|
def guests(&block)
|
||||||
|
block.call 'some_argument'
|
||||||
|
end
|
||||||
|
|
||||||
|
# Metodille voi antaa vaihtelevan määrän muuttujia. Ne siirretään taulukkoon,
|
||||||
|
# jolle annetaan parametrilistassa nimi \*-merkin avulla
|
||||||
|
def guests(*array)
|
||||||
|
array.each { |guest| puts guest }
|
||||||
|
end
|
||||||
|
|
||||||
|
# Luokan määritys aloitetaan class-avainsanalla:
|
||||||
|
|
||||||
|
class Human
|
||||||
|
|
||||||
|
# Tässä luokkamuuttuja, joka on yhteinen kaikille luokan olioille:
|
||||||
|
@@species = 'H. sapiens'
|
||||||
|
|
||||||
|
# Alustusmetodin määrittely:
|
||||||
|
def initialize(name, age = 0)
|
||||||
|
# name-oliomuuttujan arvon asetus metodille annetun name-muuttujan mukaan:
|
||||||
|
@name = name
|
||||||
|
|
||||||
|
# Jos tätä metodia kutsuessa jätetään toinen argumentti (age) antamatta,
|
||||||
|
# saa se parametriluettelossa määritetyn arvon 0:
|
||||||
|
@age = age
|
||||||
|
end
|
||||||
|
|
||||||
|
# Tyypillinen oliomuuttujan arvon asettava metodi:
|
||||||
|
def name=(name)
|
||||||
|
@name = name
|
||||||
|
end
|
||||||
|
|
||||||
|
# Tyypillinen oliomuuttujan arvon palauttava metodi:
|
||||||
|
def name
|
||||||
|
@name
|
||||||
|
end
|
||||||
|
|
||||||
|
# Edelliset kaksi metodia voi ilmaista idiomaattisemmin myös näin:
|
||||||
|
attr_accessor :name
|
||||||
|
|
||||||
|
# Lisäksi arvon palauttavan ja asettavan metodin voi määritellä erikseen:
|
||||||
|
attr_reader :name
|
||||||
|
attr_writer :name
|
||||||
|
|
||||||
|
# Luokkametodeissa käytetään avainsanaa self erotuksena oliometodeista.
|
||||||
|
# Luokkametodia voi kutsua vain luokalla itsellään, ei olioilla:
|
||||||
|
def self.say(msg)
|
||||||
|
puts msg
|
||||||
|
end
|
||||||
|
|
||||||
|
def species
|
||||||
|
@@species
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Olion luonti:
|
||||||
|
|
||||||
|
jim = Human.new('Jim Halpert')
|
||||||
|
|
||||||
|
dwight = Human.new('Dwight K. Schrute')
|
||||||
|
|
||||||
|
# Olion metodien kutsuja:
|
||||||
|
jim.species #=> "H. sapiens"
|
||||||
|
jim.name #=> "Jim Halpert"
|
||||||
|
jim.name = "Jim Halpert II" #=> "Jim Halpert II"
|
||||||
|
jim.name #=> "Jim Halpert II"
|
||||||
|
dwight.species #=> "H. sapiens"
|
||||||
|
dwight.name #=> "Dwight K. Schrute"
|
||||||
|
|
||||||
|
# Luokkametodin kutsu:
|
||||||
|
Human.say('Hi') #=> "Hi"
|
||||||
|
|
||||||
|
# Muuttujan näkyvyysalueen voi määritellä etuliitteellä.
|
||||||
|
|
||||||
|
# $-alkuiset muuttujat ovat globaaleja:
|
||||||
|
$var = "I'm a global var"
|
||||||
|
defined? $var #=> "global-variable"
|
||||||
|
|
||||||
|
# @-alkuiset muuttujat kuuluvat oliolle,
|
||||||
|
# jonka näkyvyysalueella määrittely tehdään:
|
||||||
|
@var = "I'm an instance var"
|
||||||
|
defined? @var #=> "instance-variable"
|
||||||
|
|
||||||
|
# @@-alkuiset muuttujat kuuluvat vastaavasti näkyvyysalueensa luokalle:
|
||||||
|
@@var = "I'm a class var"
|
||||||
|
defined? @@var #=> "class variable"
|
||||||
|
|
||||||
|
# Isolla alkukirjaimella nimetyt muuttujat ovatkin vakioita:
|
||||||
|
Var = "I'm a constant"
|
||||||
|
defined? Var #=> "constant"
|
||||||
|
|
||||||
|
# Kuten odottaa saattaa, myös luokat itsessään ovat olioita.
|
||||||
|
# Siksi niille voi määritellä muuttujia, jotka ovat yhteisiä kaikille
|
||||||
|
# luokan ilmentymille ja perillisille.
|
||||||
|
|
||||||
|
# Tavallisen luokan määrittely:
|
||||||
|
|
||||||
|
class Human
|
||||||
|
@@foo = 0
|
||||||
|
|
||||||
|
def self.foo
|
||||||
|
@@foo
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.foo=(value)
|
||||||
|
@@foo = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Perillisluokan määrittely:
|
||||||
|
|
||||||
|
class Worker < Human
|
||||||
|
end
|
||||||
|
|
||||||
|
Human.foo # 0
|
||||||
|
Worker.foo # 0
|
||||||
|
|
||||||
|
Human.foo = 2 # 2
|
||||||
|
Worker.foo # 2
|
||||||
|
|
||||||
|
# Oliomuuttuja on kuitenkin olion oma eikä periydy:
|
||||||
|
|
||||||
|
class Human
|
||||||
|
@bar = 0
|
||||||
|
|
||||||
|
def self.bar
|
||||||
|
@bar
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.bar=(value)
|
||||||
|
@bar = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Doctor < Human
|
||||||
|
end
|
||||||
|
|
||||||
|
Human.bar # 0
|
||||||
|
Doctor.bar # nil
|
||||||
|
|
||||||
|
module ModuleExample
|
||||||
|
def foo
|
||||||
|
'foo'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Moduulien lisääminen luokkaan "include"-avainsanalla siirtää moduulin metodit
|
||||||
|
# luokan ilmentymille, kun taas "extend" avainsana siirtää metodit
|
||||||
|
# luokalle itselleen:
|
||||||
|
|
||||||
|
class Person
|
||||||
|
include ModuleExample
|
||||||
|
end
|
||||||
|
|
||||||
|
class Book
|
||||||
|
extend ModuleExample
|
||||||
|
end
|
||||||
|
|
||||||
|
Person.foo # => NoMethodError: undefined method `foo' for Person:Class
|
||||||
|
Person.new.foo # => 'foo'
|
||||||
|
Book.foo # => 'foo'
|
||||||
|
Book.new.foo # => NoMethodError: undefined method `foo'
|
||||||
|
|
||||||
|
# Callback-tyyppiset metodit suoritetaan moduulia sisällyttäessä:
|
||||||
|
|
||||||
|
module ConcernExample
|
||||||
|
def self.included(base)
|
||||||
|
base.extend(ClassMethods)
|
||||||
|
base.send(:include, InstanceMethods)
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def bar
|
||||||
|
'bar'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module InstanceMethods
|
||||||
|
def qux
|
||||||
|
'qux'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Something
|
||||||
|
include ConcernExample
|
||||||
|
end
|
||||||
|
|
||||||
|
Something.bar # => 'bar'
|
||||||
|
Something.qux # => NoMethodError: undefined method `qux'
|
||||||
|
Something.new.bar # => NoMethodError: undefined method `bar'
|
||||||
|
Something.new.qux # => 'qux'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Lisämateriaalia englanniksi
|
||||||
|
|
||||||
|
- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - Selaimessa tehtäviä harjoituksia tämän dokumentin hengessä
|
||||||
|
- [An Interactive Tutorial for Ruby](https://rubymonk.com/)
|
||||||
|
- [Official Documentation](http://www.ruby-doc.org/core-2.1.1/) - Virallinen dokumentaatio
|
||||||
|
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
|
||||||
|
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - Vanhempi, mutta [ilmainen painos](http://ruby-doc.com/docs/ProgrammingRuby/) on luettavissa netissä
|
||||||
|
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - Yhteisön luoma Ruby-tyyliopas
|
||||||
|
- [Try Ruby](http://tryruby.org) - Rubyn perusteet interaktiivisesti
|
@ -5,6 +5,7 @@ contributors:
|
|||||||
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
- ["Christophe THOMAS", "https://github.com/WinChris"]
|
||||||
lang: fr-fr
|
lang: fr-fr
|
||||||
---
|
---
|
||||||
|
|
||||||
HTML signifie HyperText Markup Language.
|
HTML signifie HyperText Markup Language.
|
||||||
C'est un langage (format de fichiers) qui permet d'écrire des pages internet.
|
C'est un langage (format de fichiers) qui permet d'écrire des pages internet.
|
||||||
C’est un langage de balisage, il nous permet d'écrire des pages HTML au moyen de balises (Markup, en anglais).
|
C’est un langage de balisage, il nous permet d'écrire des pages HTML au moyen de balises (Markup, en anglais).
|
||||||
@ -17,7 +18,7 @@ Comme tous les autres langages, HTML a plusieurs versions. Ici, nous allons parl
|
|||||||
Cet article porte principalement sur la syntaxe et quelques astuces.
|
Cet article porte principalement sur la syntaxe et quelques astuces.
|
||||||
|
|
||||||
|
|
||||||
```HTML
|
```html
|
||||||
<!-- Les commentaires sont entouré comme cette ligne! -->
|
<!-- Les commentaires sont entouré comme cette ligne! -->
|
||||||
|
|
||||||
<!-- #################### Les balises #################### -->
|
<!-- #################### Les balises #################### -->
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
filename: learnbrainfuck-fr.bf
|
filename: learnbrainfuck-fr.bf
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
@ -8,7 +8,7 @@ translators:
|
|||||||
lang: fr-fr
|
lang: fr-fr
|
||||||
---
|
---
|
||||||
|
|
||||||
```d
|
```c
|
||||||
// Commençons par un classique
|
// Commençons par un classique
|
||||||
module hello;
|
module hello;
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ D est activement développé par de nombreuses personnes très intelligents, gui
|
|||||||
[Andrei Alexandrescu](https://fr.wikipedia.org/wiki/Andrei_Alexandrescu).
|
[Andrei Alexandrescu](https://fr.wikipedia.org/wiki/Andrei_Alexandrescu).
|
||||||
Après cette petite introduction, jetons un coup d'oeil à quelques exemples.
|
Après cette petite introduction, jetons un coup d'oeil à quelques exemples.
|
||||||
|
|
||||||
```d
|
```c
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -75,7 +75,7 @@ On peut définir de nouveaux types avec les mots-clés `struct`, `class`,
|
|||||||
`union` et `enum`. Ces types sont passés au fonction par valeur (ils sont copiés)
|
`union` et `enum`. Ces types sont passés au fonction par valeur (ils sont copiés)
|
||||||
De plus, on peut utiliser les templates pour rendre toutes ces abstractions génériques.
|
De plus, on peut utiliser les templates pour rendre toutes ces abstractions génériques.
|
||||||
|
|
||||||
```d
|
```c
|
||||||
// Ici, 'T' est un paramètre de type. Il est similaire au <T> de C++/C#/Java.
|
// Ici, 'T' est un paramètre de type. Il est similaire au <T> de C++/C#/Java.
|
||||||
struct LinkedList(T) {
|
struct LinkedList(T) {
|
||||||
T data = null;
|
T data = null;
|
||||||
@ -140,7 +140,7 @@ une méthode qui peut se comporter comme une lvalue. On peut donc utiliser
|
|||||||
la syntaxe des structures classiques (`struct.x = 7`) comme si il
|
la syntaxe des structures classiques (`struct.x = 7`) comme si il
|
||||||
s'agissait de méthodes getter ou setter.
|
s'agissait de méthodes getter ou setter.
|
||||||
|
|
||||||
```d
|
```c
|
||||||
// Considérons une classe paramétrée avec les types 'T' et 'U'
|
// Considérons une classe paramétrée avec les types 'T' et 'U'
|
||||||
class MyClass(T, U) {
|
class MyClass(T, U) {
|
||||||
T _data;
|
T _data;
|
||||||
@ -212,7 +212,7 @@ de premier ordre, les fonctions `pure` et les données immuables.
|
|||||||
De plus, tout vos algorithmes fonctionelles favoris (map, reduce, filter)
|
De plus, tout vos algorithmes fonctionelles favoris (map, reduce, filter)
|
||||||
sont disponibles dans le module `std.algorithm`.
|
sont disponibles dans le module `std.algorithm`.
|
||||||
|
|
||||||
```d
|
```c
|
||||||
import std.algorithm : map, filter, reduce;
|
import std.algorithm : map, filter, reduce;
|
||||||
import std.range : iota; // construit un intervalle excluant la dernière valeur.
|
import std.range : iota; // construit un intervalle excluant la dernière valeur.
|
||||||
|
|
||||||
@ -242,7 +242,7 @@ est de type A, comme si c'était une méthode de A.
|
|||||||
J'aime le parallélisme. Vous aimez les parallélisme ? Bien sur que vous aimez ça
|
J'aime le parallélisme. Vous aimez les parallélisme ? Bien sur que vous aimez ça
|
||||||
Voyons comment on le fait en D !
|
Voyons comment on le fait en D !
|
||||||
|
|
||||||
```d
|
```c
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
import std.parallelism : parallel;
|
import std.parallelism : parallel;
|
||||||
import std.math : sqrt;
|
import std.math : sqrt;
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
---
|
---
|
||||||
language: make
|
language: make
|
||||||
contributors:
|
contributors:
|
||||||
- ["Robert Steed", "https://github.com/robochat"]
|
- ["Robert Steed", "https://github.com/robochat"]
|
||||||
translators:
|
translators:
|
||||||
- ["altaris", "https://github.com/altaris"]
|
- ["altaris", "https://github.com/altaris"]
|
||||||
filename: Makefile-fr
|
filename: Makefile-fr
|
||||||
lang: fr-fr
|
lang: fr-fr
|
||||||
---
|
---
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
---
|
---
|
||||||
|
|
||||||
language: Objective-C
|
language: Objective-C
|
||||||
contributors:
|
contributors:
|
||||||
- ["Eugene Yagrushkin", "www.about.me/yagrushkin"]
|
- ["Eugene Yagrushkin", "www.about.me/yagrushkin"]
|
||||||
@ -9,7 +8,6 @@ translators:
|
|||||||
- ["Yannick Loriot", "https://github.com/YannickL"]
|
- ["Yannick Loriot", "https://github.com/YannickL"]
|
||||||
filename: LearnObjectiveC-fr.m
|
filename: LearnObjectiveC-fr.m
|
||||||
lang: fr-fr
|
lang: fr-fr
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
L'Objective-C est un langage de programmation orienté objet réflexif principalement utilisé par Apple pour les systèmes d'exploitations Mac OS X et iOS et leurs frameworks respectifs, Cocoa et Cocoa Touch.
|
L'Objective-C est un langage de programmation orienté objet réflexif principalement utilisé par Apple pour les systèmes d'exploitations Mac OS X et iOS et leurs frameworks respectifs, Cocoa et Cocoa Touch.
|
||||||
@ -519,6 +517,7 @@ __unsafe_unretained NSArray *unsafeArray; // Comme __weak, mais la variable n'es
|
|||||||
// l'objet est supprimé
|
// l'objet est supprimé
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Lectures Complémentaires
|
## Lectures Complémentaires
|
||||||
|
|
||||||
[La Page Wikipedia de l'Objective-C](http://fr.wikipedia.org/wiki/Objective-C)
|
[La Page Wikipedia de l'Objective-C](http://fr.wikipedia.org/wiki/Objective-C)
|
||||||
|
@ -189,16 +189,16 @@ foo = add 10 -- foo is now a function that takes a number and adds 10 to it
|
|||||||
foo 5 -- 15
|
foo 5 -- 15
|
||||||
|
|
||||||
-- Another way to write the same thing
|
-- Another way to write the same thing
|
||||||
foo = (+10)
|
foo = (10+)
|
||||||
foo 5 -- 15
|
foo 5 -- 15
|
||||||
|
|
||||||
-- function composition
|
-- function composition
|
||||||
-- the operator `.` chains functions together.
|
-- the operator `.` chains functions together.
|
||||||
-- For example, here foo is a function that takes a value. It adds 10 to it,
|
-- For example, here foo is a function that takes a value. It adds 10 to it,
|
||||||
-- multiplies the result of that by 4, and then returns the final value.
|
-- multiplies the result of that by 4, and then returns the final value.
|
||||||
foo = (*4) . (+10)
|
foo = (4*) . (10+)
|
||||||
|
|
||||||
-- (5 + 10) * 4 = 60
|
-- 4*(10 + 5) = 60
|
||||||
foo 5 -- 60
|
foo 5 -- 60
|
||||||
|
|
||||||
-- fixing precedence
|
-- fixing precedence
|
||||||
@ -222,7 +222,7 @@ even . fib $ 7 -- false
|
|||||||
-- 5. Type signatures
|
-- 5. Type signatures
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
|
|
||||||
-- Haskell has a very strong type system, and everything has a type signature.
|
-- Haskell has a very strong type system, and every valid expression has a type.
|
||||||
|
|
||||||
-- Some basic types:
|
-- Some basic types:
|
||||||
5 :: Integer
|
5 :: Integer
|
||||||
@ -259,7 +259,7 @@ case args of
|
|||||||
_ -> putStrLn "bad args"
|
_ -> putStrLn "bad args"
|
||||||
|
|
||||||
-- Haskell doesn't have loops; it uses recursion instead.
|
-- Haskell doesn't have loops; it uses recursion instead.
|
||||||
-- map applies a function over every element in an array
|
-- map applies a function over every element in a list
|
||||||
|
|
||||||
map (*2) [1..5] -- [2, 4, 6, 8, 10]
|
map (*2) [1..5] -- [2, 4, 6, 8, 10]
|
||||||
|
|
||||||
@ -279,7 +279,7 @@ foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43
|
|||||||
-- This is the same as
|
-- This is the same as
|
||||||
(2 * (2 * (2 * 4 + 1) + 2) + 3)
|
(2 * (2 * (2 * 4 + 1) + 2) + 3)
|
||||||
|
|
||||||
-- foldl is left-handed, foldr is right-
|
-- foldl is left-handed, foldr is right-handed
|
||||||
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16
|
foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16
|
||||||
|
|
||||||
-- This is now the same as
|
-- This is now the same as
|
||||||
@ -318,7 +318,7 @@ Nothing -- of type `Maybe a` for any `a`
|
|||||||
-- it is not hard to explain enough to get going.
|
-- it is not hard to explain enough to get going.
|
||||||
|
|
||||||
-- When a Haskell program is executed, `main` is
|
-- When a Haskell program is executed, `main` is
|
||||||
-- called. It must return a value of type `IO ()`. For example:
|
-- called. It must return a value of type `IO a` for some type `a`. For example:
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = putStrLn $ "Hello, sky! " ++ (say Blue)
|
main = putStrLn $ "Hello, sky! " ++ (say Blue)
|
||||||
@ -361,7 +361,7 @@ sayHello = do
|
|||||||
-- You can think of a value of type `IO a` as representing a
|
-- You can think of a value of type `IO a` as representing a
|
||||||
-- computer program that will generate a value of type `a`
|
-- computer program that will generate a value of type `a`
|
||||||
-- when executed (in addition to anything else it does). We can
|
-- when executed (in addition to anything else it does). We can
|
||||||
-- store and reuse this value using `<-`. We can also
|
-- name and reuse this value using `<-`. We can also
|
||||||
-- make our own action of type `IO String`:
|
-- make our own action of type `IO String`:
|
||||||
|
|
||||||
action :: IO String
|
action :: IO String
|
||||||
@ -432,7 +432,7 @@ Hello, Friend!
|
|||||||
|
|
||||||
There's a lot more to Haskell, including typeclasses and monads. These are the
|
There's a lot more to Haskell, including typeclasses and monads. These are the
|
||||||
big ideas that make Haskell such fun to code in. I'll leave you with one final
|
big ideas that make Haskell such fun to code in. I'll leave you with one final
|
||||||
Haskell example: an implementation of quicksort in Haskell:
|
Haskell example: an implementation of a quicksort variant in Haskell:
|
||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
qsort [] = []
|
qsort [] = []
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
---
|
---
|
||||||
language: ruby
|
language: ruby
|
||||||
lang: hu-hu
|
lang: hu-hu
|
||||||
filenev: learnruby.rb
|
filename: learnruby-hu.rb
|
||||||
contributors:
|
contributors:
|
||||||
- ["David Underwood", "http://theflyingdeveloper.com"]
|
- ["David Underwood", "http://theflyingdeveloper.com"]
|
||||||
- ["Joel Walden", "http://joelwalden.net"]
|
- ["Joel Walden", "http://joelwalden.net"]
|
||||||
@ -13,7 +13,7 @@ contributors:
|
|||||||
- ["Dzianis Dashkevich", "https://github.com/dskecse"]
|
- ["Dzianis Dashkevich", "https://github.com/dskecse"]
|
||||||
- ["Levi Bostian", "https://github.com/levibostian"]
|
- ["Levi Bostian", "https://github.com/levibostian"]
|
||||||
- ["Rahil Momin", "https://github.com/iamrahil"]
|
- ["Rahil Momin", "https://github.com/iamrahil"]
|
||||||
translators:
|
translators:
|
||||||
- ["Zsolt Prontvai", "https://github.com/prozsolt"]
|
- ["Zsolt Prontvai", "https://github.com/prozsolt"]
|
||||||
---
|
---
|
||||||
|
|
||||||
|
848
id-id/php-id.html.markdown
Normal file
848
id-id/php-id.html.markdown
Normal file
@ -0,0 +1,848 @@
|
|||||||
|
---
|
||||||
|
language: PHP
|
||||||
|
contributors:
|
||||||
|
- ["Malcolm Fell", "http://emarref.net/"]
|
||||||
|
- ["Trismegiste", "https://github.com/Trismegiste"]
|
||||||
|
filename: learnphp-id.php
|
||||||
|
translators:
|
||||||
|
- ["Ahmad Zafrullah", "https://github.com/23Pstars"]
|
||||||
|
lang: id-id
|
||||||
|
---
|
||||||
|
|
||||||
|
Dokumen ini menjelaskan tentang PHP5 keatas.
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php // Skrip PHP harus diawali dengan tag <?php
|
||||||
|
|
||||||
|
// Jika dokumen PHP hanya mengandung kode PHP, sebaiknya tidak menggunakan
|
||||||
|
// tag penutup PHP untuk menghindari ketidaksengajaan tampilnya sesuatu.
|
||||||
|
|
||||||
|
// Dua garis miring diawal digunakan untuk komentar satu baris.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Membatasi teks dalam garis miring-bintang dan bintang-garis miring
|
||||||
|
membuat komentar untuk banyak-baris sekaligus.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Gunakan "echo" or "print" untuk menampilkan sesuatu
|
||||||
|
print('Halo '); // Menampilkan "Halo " tanpa baris baru
|
||||||
|
|
||||||
|
// () boleh tidak digunakan dalam menggunakan "print" dan "echo"
|
||||||
|
echo "Dunia\n"; // Menampilkan "Dunia" dengan baris baru
|
||||||
|
// (semua perintah harus diakhiri dengan titik koma)
|
||||||
|
|
||||||
|
// Apapun yang berada diluar tag <?php akan ditampilkan secara otomatis
|
||||||
|
?>
|
||||||
|
Halo Dunia, lagi!
|
||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
/************************************
|
||||||
|
* Tipe Data & Variabel
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Variabel diawali dengan simnbol $.
|
||||||
|
// Nama variabel yang benar diawali dengan huruf atau garis-bawah,
|
||||||
|
// diikuti dengan beberapa huruf, angka, dan garis-bawah lainnya.
|
||||||
|
|
||||||
|
// Nilai Boolean adalah case-insensitive
|
||||||
|
$boolean = true; // atau TRUE atau True
|
||||||
|
$boolean = false; // atau FALSE atau False
|
||||||
|
|
||||||
|
// Nilai Integer
|
||||||
|
$int1 = 12; // => 12
|
||||||
|
$int2 = -12; // => -12
|
||||||
|
$int3 = 012; // => 10 (awalan 0 menandakan bilangan Oktal)
|
||||||
|
$int4 = 0x0F; // => 15 (awalan 0x menandakan bilangan Heksadesimal)
|
||||||
|
// Bilangan Biner Integer tersedia mulai dari PHP 5.4.0.
|
||||||
|
$int5 = 0b11111111; // 255 (awalan 0b menandakan bilangan Biner)
|
||||||
|
|
||||||
|
// Nilai Floats (dikenal juga sebagai Doubles)
|
||||||
|
$float = 1.234;
|
||||||
|
$float = 1.2e3;
|
||||||
|
$float = 7E-10;
|
||||||
|
|
||||||
|
// Menghapus variable
|
||||||
|
unset($int1);
|
||||||
|
|
||||||
|
// Aritmatika
|
||||||
|
$jumlah = 1 + 1; // 2
|
||||||
|
$selisih = 2 - 1; // 1
|
||||||
|
$perkalian = 2 * 2; // 4
|
||||||
|
$pembagian = 2 / 1; // 2
|
||||||
|
|
||||||
|
// Aritmatika singkat
|
||||||
|
$angka = 0;
|
||||||
|
$angka += 1; // Menjumlahkan $angka dengan 1
|
||||||
|
echo $angka++; // Menampilkan 1 (dijumlahkan dengan 1 setelah ditampilkan)
|
||||||
|
echo ++$angka; // Menampilkan 3 (dijumlahkan dengan 1 sebelum ditampilkan)
|
||||||
|
$angka /= $float; // Membagi dan menyimpan hasil pembagian pada $angka;
|
||||||
|
|
||||||
|
// String biasanya diawali dan ditutup dengan petik satu.
|
||||||
|
$sgl_quotes = '$String'; // => '$String'
|
||||||
|
|
||||||
|
// Hindari menggunakan petik dua kecuali menyertakan variabel lain
|
||||||
|
$dbl_quotes = "Ini adalah $sgl_quotes."; // => 'Ini adalah $String.'
|
||||||
|
|
||||||
|
// Karakter khusus hanya berlaku pada petik dua
|
||||||
|
$berfungsi = "Ini mengandung \t karakter tab.";
|
||||||
|
$tidak_berfungsi = 'Ini hanya mengandung garis miring dan huruf t: \t';
|
||||||
|
|
||||||
|
// Batasi variabel dengan kurung kurawal jika diperlukan
|
||||||
|
$uang = "Saya memiliki $${angka} di Bank.";
|
||||||
|
|
||||||
|
// Sejak PHP 5.3, nowdocs dapat digunakan untuk tak-terinterpolasi banyak-baris
|
||||||
|
$nowdoc = <<<'END'
|
||||||
|
Banyak baris
|
||||||
|
string
|
||||||
|
END;
|
||||||
|
|
||||||
|
// Heredocs akan melakukan interpolasi
|
||||||
|
$heredoc = <<<END
|
||||||
|
Banyak baris
|
||||||
|
$sgl_quotes
|
||||||
|
END;
|
||||||
|
|
||||||
|
// Menyambung string dapat dilakukan menggunakan .
|
||||||
|
echo 'String ini ' . 'tersambung';
|
||||||
|
|
||||||
|
// String dapat dijadikan parameter pada "echo"
|
||||||
|
echo 'Banyak', 'Parameter', 'String'; // Menampilkan 'BanyakParameterString'
|
||||||
|
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* Konstan
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Sebuah konstan didifinisikan menggunakan fungsi define()
|
||||||
|
// dan tidak bisa diganti/rubah selama program berjalan!
|
||||||
|
|
||||||
|
// Nama konstan yang benar diawali dengan huruf dan garis-bawah,
|
||||||
|
// diikuti dengan beberapa huruf, angka, atau garis-bawah.
|
||||||
|
define("FOO", "sesuatu");
|
||||||
|
|
||||||
|
// Mengakses konstan memungkinkan untuk dapat dipanggil tanpa menggunakan simbol $
|
||||||
|
echo FOO; // Menampilkan 'sesuatu'
|
||||||
|
echo 'Keluaran ini adalah ' . FOO; // Menampilkan 'Keluaran ini adalah sesuatu'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* Larik (Array)
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Semua larik dalam PHP bersifat asosiatif (saling berhubungan).
|
||||||
|
|
||||||
|
// Berfungsi pada semua versi PHP
|
||||||
|
$asosiatif = array('Satu' => 1, 'Dua' => 2, 'Tiga' => 3);
|
||||||
|
|
||||||
|
// Pada PHP 5.4 diperkenalkan cara penulisan (sintaks) baru
|
||||||
|
$asosiatif = ['Satu' => 1, 'Dua' => 2, 'Tiga' => 3];
|
||||||
|
|
||||||
|
echo $asosiatif['Satu']; // menampilkan 1
|
||||||
|
|
||||||
|
// Daftar literal secara tidak langsung ditentukan oleh kunci integer
|
||||||
|
$larik = ['Satu', 'Dua', 'Tiga'];
|
||||||
|
echo $larik[0]; // => "Satu"
|
||||||
|
|
||||||
|
// Menambahkan sebuah elemen pada akhir larik
|
||||||
|
$larik[] = 'Empat';
|
||||||
|
// atau
|
||||||
|
array_push($larik, 'Lima');
|
||||||
|
|
||||||
|
// Menghapus elemen dari larik
|
||||||
|
unset($larik[3]);
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* Keluaran
|
||||||
|
*/
|
||||||
|
|
||||||
|
echo('Halo Dunia!');
|
||||||
|
// Menampilkan Halo Dunia! ke "stdout".
|
||||||
|
// "stdout" adalah sebuah halaman web ketika dijalankan dalam peramban (browser).
|
||||||
|
|
||||||
|
print('Halo Dunia!'); // Sama seperti "echo"
|
||||||
|
|
||||||
|
// "echo" dan "print" merupakan bahasa konstruksi, jadi tanda kurung dapat dihilangkan
|
||||||
|
echo 'Halo Dunia!';
|
||||||
|
print 'Halo Dunia!';
|
||||||
|
|
||||||
|
$paragraf = 'paragraf';
|
||||||
|
|
||||||
|
echo 100; // Menampilkan variabel skalar secara langsung
|
||||||
|
echo $paragraf; // atau sebuat variabel
|
||||||
|
|
||||||
|
// Jika PHP tag-singkat telah dikonfigurasi, atau versi PHP yang digunakan
|
||||||
|
// adalah 5.4.0 keatas, dapat digunakan sintaks "echo" singkat
|
||||||
|
|
||||||
|
?>
|
||||||
|
<p><?= $paragraf ?></p>
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$x = 1;
|
||||||
|
$y = 2;
|
||||||
|
$x = $y; // $x sekarang berisi nilai yang sama dengan $y
|
||||||
|
$z = &$y;
|
||||||
|
// $z sekarang berisi referensi ke $y. Mengubah nilai dari $z
|
||||||
|
// akan mengubah nilai dari $y juga, begitupun sebaliknya.
|
||||||
|
// $x tetap tidak berubah sebagaimana nilai asli dari $y
|
||||||
|
|
||||||
|
echo $x; // => 2
|
||||||
|
echo $z; // => 2
|
||||||
|
$y = 0;
|
||||||
|
echo $x; // => 2
|
||||||
|
echo $z; // => 0
|
||||||
|
|
||||||
|
// Menampilkan tipe dan nilai dari variabel ke "stdout"
|
||||||
|
var_dump($z); // prints int(0)
|
||||||
|
|
||||||
|
// Menampilkan variabel ke "stdout" dalam format yang mudah dibaca
|
||||||
|
print_r($larik); // menampilkan: Array ( [0] => Satu [1] => Dua [2] => Tiga )
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* Logika
|
||||||
|
*/
|
||||||
|
$a = 0;
|
||||||
|
$b = '0';
|
||||||
|
$c = '1';
|
||||||
|
$d = '1';
|
||||||
|
|
||||||
|
// menegaskan lemparan sebuah peringatan jika pernyataan tidak benar
|
||||||
|
|
||||||
|
// Perbandingan berikut akan selalu benar, meskipun memiliki tipe yang berbeda.
|
||||||
|
assert($a == $b); // kesamaan
|
||||||
|
assert($c != $a); // ketidak-samaan
|
||||||
|
assert($c <> $a); // versi lain dari ketidak-samaan
|
||||||
|
assert($a < $c);
|
||||||
|
assert($c > $b);
|
||||||
|
assert($a <= $b);
|
||||||
|
assert($c >= $d);
|
||||||
|
|
||||||
|
// Dibawah ini hanya akan bernilai benar jika nilainya memiliki tipe yang sama.
|
||||||
|
assert($c === $d);
|
||||||
|
assert($a !== $d);
|
||||||
|
assert(1 === '1');
|
||||||
|
assert(1 !== '1');
|
||||||
|
|
||||||
|
// Operator 'Spaceship' (sejak PHP 7)
|
||||||
|
// Mengembalikan 0 jika nilai pada kedua sisi adalah sama
|
||||||
|
// Mengembalikan 1 jika nilai pada sisi kiri lebih besar
|
||||||
|
// Mengembalikan -1 jika nilai pada sisi kanan lebih besar
|
||||||
|
|
||||||
|
$a = 100;
|
||||||
|
$b = 1000;
|
||||||
|
|
||||||
|
echo $a <=> $a; // 0 karena keduanya sama
|
||||||
|
echo $a <=> $b; // -1 karena $a < $b
|
||||||
|
echo $b <=> $a; // 1 karena $b > $a
|
||||||
|
|
||||||
|
// Variabel dapat dikonversi menjadi tipe lain, sesuai penggunaannya.
|
||||||
|
|
||||||
|
$integer = 1;
|
||||||
|
echo $integer + $integer; // => 2
|
||||||
|
|
||||||
|
$string = '1';
|
||||||
|
echo $string + $string; // => 2 (string dipaksa menjadi integer)
|
||||||
|
|
||||||
|
$string = 'satu';
|
||||||
|
echo $string + $string; // => 0
|
||||||
|
// Menghasilkan 0 karena operator (+) tidak dapat memaksa string 'satu' menjadi sebuah integer
|
||||||
|
|
||||||
|
// Perubahan tipe dapat dimanfaatkan untuk diperlakukan sebagai tipe lainnya
|
||||||
|
|
||||||
|
$boolean = (boolean) 1; // => true
|
||||||
|
|
||||||
|
$nol = 0;
|
||||||
|
$boolean = (boolean) $nol; // => false
|
||||||
|
|
||||||
|
// Terdapat juga fungsi khusus untuk melakukan perubahan terhadap beberapa tipe
|
||||||
|
$integer = 5;
|
||||||
|
$string = strval($integer);
|
||||||
|
|
||||||
|
$var = null; // Nilai Null
|
||||||
|
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* Struktur Kontrol
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
print 'Saya tampil';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (false) {
|
||||||
|
print 'Saya tidak tampil';
|
||||||
|
} else {
|
||||||
|
print 'Saya tampil';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (false) {
|
||||||
|
print 'Tidak tampil';
|
||||||
|
} elseif(true) {
|
||||||
|
print 'Tampil';
|
||||||
|
}
|
||||||
|
|
||||||
|
// operator ternary
|
||||||
|
print (false ? 'Tidak tampil' : 'Tampil');
|
||||||
|
|
||||||
|
// cara pintas operator ternary mulai dirilis sejak PHP 5.3
|
||||||
|
// persamaan dari "$x ? $x : 'Kerjakan'"
|
||||||
|
$x = false;
|
||||||
|
print($x ?: 'Kerjakan');
|
||||||
|
|
||||||
|
// operator null coalesce sejak PHP 7
|
||||||
|
$a = null;
|
||||||
|
$b = 'Ditampilkan';
|
||||||
|
echo $a ?? 'a belum di-set'; // menampilkan 'a belum di-set'
|
||||||
|
echo $b ?? 'b belum di-set'; // menampilkan 'Ditampilkan'
|
||||||
|
|
||||||
|
|
||||||
|
$x = 0;
|
||||||
|
if ($x === '0') {
|
||||||
|
print 'Tidak ditampilkan';
|
||||||
|
} elseif($x == '1') {
|
||||||
|
print 'Tidak ditampilkan';
|
||||||
|
} else {
|
||||||
|
print 'Tampil';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Alternatif sintaks untuk kebutuhan templat:
|
||||||
|
?>
|
||||||
|
|
||||||
|
<?php if ($x): ?>
|
||||||
|
Ini ditampilkan jika pengujian benar.
|
||||||
|
<?php else: ?>
|
||||||
|
Selain tersebut ini yang akan ditampilkan.
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Gunakan "switch" untuk menghemat logika.
|
||||||
|
switch ($x) {
|
||||||
|
case '0':
|
||||||
|
print 'Switch mendukung tipe paksaan';
|
||||||
|
break; // Kata kunci "break" harus disertakan, jika tidak
|
||||||
|
// maka logika tersebut akan berlanjut ke bagian "dua" dan "tiga"
|
||||||
|
case 'dua':
|
||||||
|
case 'tiga':
|
||||||
|
// Lakukan sesuatu jika $x bernilai "dua" atau "tiga"
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// Aksi cadangan
|
||||||
|
}
|
||||||
|
|
||||||
|
// "while", "do...while" dan perulangan "for"
|
||||||
|
$i = 0;
|
||||||
|
while ($i < 5) {
|
||||||
|
echo $i++;
|
||||||
|
}; // Menampilkan "01234"
|
||||||
|
|
||||||
|
echo "\n";
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
do {
|
||||||
|
echo $i++;
|
||||||
|
} while ($i < 5); // Menampilkan "01234"
|
||||||
|
|
||||||
|
echo "\n";
|
||||||
|
|
||||||
|
for ($x = 0; $x < 10; $x++) {
|
||||||
|
echo $x;
|
||||||
|
} // Menampilkan "0123456789"
|
||||||
|
|
||||||
|
echo "\n";
|
||||||
|
|
||||||
|
$roda = ['sepeda' => 2, 'mobil' => 4];
|
||||||
|
|
||||||
|
// Perulangan "foreach" dapat melakukan iterasi pada larik (array)
|
||||||
|
foreach ($roda as $jumlah_roda) {
|
||||||
|
echo $jumlah_roda;
|
||||||
|
} // Menampilkan "24"
|
||||||
|
|
||||||
|
echo "\n";
|
||||||
|
|
||||||
|
// Iterasi dapat dilakukan terhadap "key" (kunci) dan "value" (nilai)
|
||||||
|
foreach ($roda as $mesin => $jumlah_roda) {
|
||||||
|
echo "$mesin memiliki $jumlah_roda buah roda";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "\n";
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
while ($i < 5) {
|
||||||
|
if ($i === 3) {
|
||||||
|
break; // Menghentikan proses perulangan
|
||||||
|
}
|
||||||
|
echo $i++;
|
||||||
|
} // Menampilkan "012"
|
||||||
|
|
||||||
|
for ($i = 0; $i < 5; $i++) {
|
||||||
|
if ($i === 3) {
|
||||||
|
continue; // Melewati tahapan iterasi saat ini
|
||||||
|
}
|
||||||
|
echo $i;
|
||||||
|
} // Menampilkan "0124"
|
||||||
|
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* Fungsi
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Fungsi didefinisikan dengan "function":
|
||||||
|
function fungsi_saya () {
|
||||||
|
return 'Halo';
|
||||||
|
}
|
||||||
|
|
||||||
|
echo fungsi_saya(); // => "Halo"
|
||||||
|
|
||||||
|
// Nama fungsi yang baik dan benar diawali dengan sebuah huruf atau garis-bawah, diikuti oleh
|
||||||
|
// beberapa huruf, angka, atau garis-bawah.
|
||||||
|
|
||||||
|
function jumlah ($x, $y = 1) { // $y merupakan opsional, jika tidak ditentukan akan bernilai 1
|
||||||
|
$hasil = $x + $y;
|
||||||
|
return $hasil;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo jumlah(4); // => 5
|
||||||
|
echo jumlah(4, 2); // => 6
|
||||||
|
|
||||||
|
// $hasil tidak dapat diakses dari luar fungsi
|
||||||
|
// print $hasil; // Akan menghasilkan sebuah "warning".
|
||||||
|
|
||||||
|
// Sejak PHP 5.3 fungsi dapat dideklarasikan menjadi tanpa-nama (anonymous);
|
||||||
|
$inc = function ($x) {
|
||||||
|
return $x + 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
echo $inc(2); // => 3
|
||||||
|
|
||||||
|
function foo ($x, $y, $z) {
|
||||||
|
echo "$x - $y - $z";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fungsi dapat mengembalikan fungsi juga
|
||||||
|
function bar ($x, $y) {
|
||||||
|
// Gunakan "use" untuk mengakses variabel diluar fungsi
|
||||||
|
return function ($z) use ($x, $y) {
|
||||||
|
foo($x, $y, $z);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
$bar = bar('A', 'B');
|
||||||
|
$bar('C'); // Menampilkan "A - B - C"
|
||||||
|
|
||||||
|
// Fungsi uang memiliki nama dapat dipanggil berdasarkan string
|
||||||
|
$nama_fungsi = 'jumlah';
|
||||||
|
echo $nama_fungsi(1, 2); // => 3
|
||||||
|
// Bermanfaat untuk menentukan fungsi mana yang akan dipanggil secara dinamis.
|
||||||
|
// Atau, dapat juga menggunakan fungsi call_user_func(callable $callback [, $parameter [, ... ]]);
|
||||||
|
|
||||||
|
// Akses semua parameter yang dikirim ke sebuah fungsi
|
||||||
|
function parameter() {
|
||||||
|
$jumlah_param = func_num_args();
|
||||||
|
if( $jumlah_param > 0 ) {
|
||||||
|
echo func_get_arg(0) . ' | ';
|
||||||
|
}
|
||||||
|
$daftar_param = func_get_args();
|
||||||
|
foreach( $daftar_param as $kunci => $param ) {
|
||||||
|
echo $kunci . ' - ' . $param . ' | ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parameter('Halo', 'Dunia'); // Halo | 0 - Halo | 1 - Dunia |
|
||||||
|
|
||||||
|
// Sejak PHP 5.6, mendapatkan jumlah variabel yang ada pada parameter
|
||||||
|
function variabel($kata, ...$daftar) {
|
||||||
|
echo $kata . " || ";
|
||||||
|
foreach ($daftar as $item) {
|
||||||
|
echo $item . ' | ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
variable("Pemisah", "Halo", "Dunia") // Pemisah || Halo | Dunia |
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* Penyertaan ("include")
|
||||||
|
*/
|
||||||
|
|
||||||
|
<?php
|
||||||
|
// Skrip PHP yang berada dalam dokumen "include" juga harus dibuka dengan tag PHP.
|
||||||
|
|
||||||
|
include 'dokumen-saya.php';
|
||||||
|
// Kode yang ada dalam dokumen-saya.php sekarang dapat diakses dari cakupan saat ini.
|
||||||
|
// Jika dokumen tidak dapat disertakan (include, seperti dokumen tidak ditemukan), maka pesan peringatan akan muncul.
|
||||||
|
|
||||||
|
include_once 'dokumen-saya.php';
|
||||||
|
// Jika dokumen-saya telah disertakan (include) oleh perintah sebelumnya, maka
|
||||||
|
// dokumen tersebut tidak akan disertakan lagi. Ini bertujuan untuk menghindari kesalahan
|
||||||
|
// yang diakibatkan oleh deklarasi ganda.
|
||||||
|
|
||||||
|
require 'dokumen-saya.php';
|
||||||
|
require_once 'dokumen-saya.php';
|
||||||
|
// Memiliki fungsi yang sama dengan "include", namun jika dokumen tidak ditemukan
|
||||||
|
// atau tidak dapat disertakan maka akan menghasilkan pesan kesalahan fatal.
|
||||||
|
|
||||||
|
// Isi dari dokumen-saya.php:
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return 'Apapun yang kamu suka.';
|
||||||
|
// akhir dari dokumen
|
||||||
|
|
||||||
|
// "include" dan "require" dapat mengembalikan sebuah nilai.
|
||||||
|
$nilai = include 'dokumen-saya.php';
|
||||||
|
|
||||||
|
// Dokumen akan disertakan berdasarkan lokasi direktori dokumen (file path) yang diberikan, jika tidak didefinisikan
|
||||||
|
// maka akan digunakan konfigurasi dari "include_path". Jika dokumen tidak ditemukan dalam "include_path",
|
||||||
|
// fungsi include akan melakukan pengecekan pada direktori yang sama dengan dokumen yang menggunakan fungsi include tersebut,
|
||||||
|
// jika tidak ditemukan juga maka pesan gagal akan dimunculkan.
|
||||||
|
/* */
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* Kelas (class)
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Kelas didefinisikan dengan kata "class"
|
||||||
|
|
||||||
|
class KelasSaya
|
||||||
|
{
|
||||||
|
const NILAI_KONSTAN = 'nilai'; // Sebuah konstan
|
||||||
|
|
||||||
|
static $nilaiStatis = 'statis';
|
||||||
|
|
||||||
|
// Variabel statis dan hak jenis aksesnya
|
||||||
|
public static $variabelStatisPublik = 'nilaiStatisPublik';
|
||||||
|
// Hanya dapat diakses dalam kelas
|
||||||
|
private static $variabelStatisPrivat = 'nilaiStatisPrivat';
|
||||||
|
// Dapat diakses dalam kelas dan kelas turunan
|
||||||
|
protected static $variabelStatisTerlindungi = 'nilaiStatisTerlindungi';
|
||||||
|
|
||||||
|
// Properti harus mendeklarasikan hak aksesnya
|
||||||
|
public $properti = 'publik';
|
||||||
|
public $PropertiInstansi;
|
||||||
|
protected $variabel = 'terlindungi'; // Dapat diakses dari kelas itu sendiri dan kelas turunannya
|
||||||
|
private $variabel = 'tersembunyi'; // Hanya dapat diakses dari kelas itu sendiri
|
||||||
|
|
||||||
|
// Membuat konstruktor dengan perintah __construct
|
||||||
|
public function __construct($PropertiInstansi) {
|
||||||
|
// Akses variabel instansi menggunakan perintah $this
|
||||||
|
$this->PropertiInstansi = $PropertiInstansi;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method dideklarasikan sebagai fungsi didalam kelas
|
||||||
|
public function methodSaya()
|
||||||
|
{
|
||||||
|
print 'KelasSaya';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perintah "final" membuat sebuah fungsi tidak dapat di-override oleh kelas turunannya
|
||||||
|
final function tidakDapatDiOverride()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Deklarasi properti atau method pada kelas sebagai statis membuat properti atau method tersebut
|
||||||
|
* dapat diakses tanpa melakukan instansiasi kelas. Properti statis tidak dapat diakses melalui
|
||||||
|
* objek kelas yang hasil instansiasi, sedangkan method statis bisa.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static function methodStatisSaya()
|
||||||
|
{
|
||||||
|
print 'Saya adalah statis';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Konstan pada kelas dapat diakses secara statis
|
||||||
|
echo KelasSaya::NILAI_KONSTAN; // Menampilkan 'nilai'
|
||||||
|
|
||||||
|
echo KelasSaya::$nilaiStatis; // Menampilkan 'statis'
|
||||||
|
KelasSaya::methodStatisSaya(); // Menampilkan 'Saya adalah statis'
|
||||||
|
|
||||||
|
// Instansi kelas menggunakan perintah "new"
|
||||||
|
$kelas_saya = new KelasSaya('Sebuah properti instansiasi');
|
||||||
|
// Tanda kurung adalah opsional jika tidak ingin menggunakan argumen.
|
||||||
|
|
||||||
|
// Akses anggota kelas menggunakan ->
|
||||||
|
echo $kelas_saya->properti; // => "publik"
|
||||||
|
echo $kelas_saya->propertiInstansi; // => "Sebuah properti instansi"
|
||||||
|
$kelas_saya->methodSaya(); // => "KelasSaya"
|
||||||
|
|
||||||
|
// Menurunkan kelas menggunakan kata kunci "extends"
|
||||||
|
class KelasSayaLainnya extends KelasSaya
|
||||||
|
{
|
||||||
|
function tampilkanPropertiTerlindungi()
|
||||||
|
{
|
||||||
|
echo $this->properti;
|
||||||
|
}
|
||||||
|
|
||||||
|
// "override" terhadap sebuah method
|
||||||
|
function methodSaya()
|
||||||
|
{
|
||||||
|
parent::methodSaya();
|
||||||
|
print ' > KelasSayaLainnya';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$kelas_saya_lainnya = new KelasSayaLainnya('Instansiasi properti');
|
||||||
|
$kelas_saya_lainnya->tampilkanPropertiTerlindung(); // => Menampilkan "terlindungi"
|
||||||
|
$kelas_saya_lainnya->methodSaya(); // Menampilkan "KelasSaya > KelasSayaLainnya"
|
||||||
|
|
||||||
|
final class SayaTidakBisaDiturunkan
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gunakan method ajaib (magic method) untuk membuat fungsi "getters" dan "setters"
|
||||||
|
class PetaKelasSaya
|
||||||
|
{
|
||||||
|
private $properti;
|
||||||
|
|
||||||
|
public function __get($key)
|
||||||
|
{
|
||||||
|
return $this->$key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __set($key, $value)
|
||||||
|
{
|
||||||
|
$this->$key = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$x = new PetaKelasSaya();
|
||||||
|
echo $x->properti; // akan memanggil method __get()
|
||||||
|
$x->properti = 'Sesuatu'; // akan memanggil method __set();
|
||||||
|
|
||||||
|
// Kelas dapat dijadikan abstrak (menggunakan kata kunci "abstract"), atau
|
||||||
|
// meng-implementasikan interfaces (menggunakan kata kunci "implements").
|
||||||
|
// Sebuah interface dideklarasikan dengan perintah "interface".
|
||||||
|
|
||||||
|
interface InterfaceSatu
|
||||||
|
{
|
||||||
|
public function kerjakanSesuatu();
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InterfaceDua
|
||||||
|
{
|
||||||
|
public function kerjakanYangLain();
|
||||||
|
}
|
||||||
|
|
||||||
|
// interface dapat diturunkan
|
||||||
|
interface InterfaceTiga extends InterfaceDua
|
||||||
|
{
|
||||||
|
public function kerjakanYangBerbeda();
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class KelasAbstrakSaya implements InterfaceSatu
|
||||||
|
{
|
||||||
|
public $x = 'kerjakanSesuatu';
|
||||||
|
}
|
||||||
|
|
||||||
|
class KelasKongkritSaya extends KelasAbstrakSaya implements InterfaceTwo
|
||||||
|
{
|
||||||
|
public function kerjakanSesuatu()
|
||||||
|
{
|
||||||
|
echo $x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function kerjakanYangLain()
|
||||||
|
{
|
||||||
|
echo 'kerjakanYangLain';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kelas dapat diimplementasikan pada banyak interface
|
||||||
|
class KelasLainnya implements InterfaceSatu, InterfaceDua
|
||||||
|
{
|
||||||
|
public function kerjakanSesuatu()
|
||||||
|
{
|
||||||
|
echo 'kerjakanSesuatu';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function kerjakanYangLain()
|
||||||
|
{
|
||||||
|
echo 'kerjakanYangLain';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* Sifat (Traits)
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Traits mulai tersedia sejak PHP 5.4.0 dan dideklarasikan menggunakan kata kunci "trait"
|
||||||
|
|
||||||
|
trait TraitSaya
|
||||||
|
{
|
||||||
|
public function methodTraitSaya()
|
||||||
|
{
|
||||||
|
print 'Saya menggunakan Trait';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class KelasTraitSaya
|
||||||
|
{
|
||||||
|
use TraitSaya;
|
||||||
|
}
|
||||||
|
|
||||||
|
$kls = new KelasTraitSaya();
|
||||||
|
$kls->methodTraitSaya(); // menampilkan "Saya menggunakan Trait"
|
||||||
|
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
* Namespaces
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Bagian ini telah dibatasi, karena deklarasi "namespace"
|
||||||
|
// karena harus ditempatkan diawal dokumen.
|
||||||
|
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Secara default, kelas tersedia sebagai namespace umum, dan dapat
|
||||||
|
// secara khusus dipanggil dengan garis-miring terbalik (backslash).
|
||||||
|
|
||||||
|
$kls = new \KelasSaya();
|
||||||
|
|
||||||
|
|
||||||
|
// Menentukan namespace untuk sebuah dokumen
|
||||||
|
namespace Saya\Namespace;
|
||||||
|
|
||||||
|
class KelasSaya
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// (dari dokumen lainnya)
|
||||||
|
$kls = new Saya\Namespace\KelasSaya;
|
||||||
|
|
||||||
|
// Atau dari dalam namespace lainnya.
|
||||||
|
namespace Saya\Lainnya\Namespace;
|
||||||
|
|
||||||
|
use Saya\Namespace\KelasSaya;
|
||||||
|
|
||||||
|
$kls = new KelasSaya();
|
||||||
|
|
||||||
|
// Namespace dapat menggunakan alias
|
||||||
|
|
||||||
|
namespace Saya\Lainnya\Namespace;
|
||||||
|
|
||||||
|
use Saya\Namespace as SuatuKelasLainnya;
|
||||||
|
|
||||||
|
$kls = new SuatuKelasLainnya\KelasSaya();
|
||||||
|
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* Late Static Binding
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
class KelasInduk {
|
||||||
|
public static function siapa() {
|
||||||
|
echo "Ini adalah " . __CLASS__ . "\n";
|
||||||
|
}
|
||||||
|
public static function coba() {
|
||||||
|
// kata kunci "self" merujuk pada method yang berada dalam satu kelas
|
||||||
|
self::who();
|
||||||
|
// kata kunci "static" merujuk pada method yang berada di kelas dimana method itu dijalankan
|
||||||
|
static::who();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KelasInduk::coba();
|
||||||
|
/*
|
||||||
|
Ini adalah KelasInduk
|
||||||
|
Ini adalah KelasInduk
|
||||||
|
*/
|
||||||
|
|
||||||
|
class KelasAnak extends KelasInduk {
|
||||||
|
public static function siapa() {
|
||||||
|
echo "Tapi ini adalah " . __CLASS__ . "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KelasAnak::tes();
|
||||||
|
/*
|
||||||
|
Ini adalah KelasInduk
|
||||||
|
Tapi ini adalah KelasAnak
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* Magic constants
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Mendapatkan nama dari suatu kelas. Harus dideklarasikan didalam kelas tersebut.
|
||||||
|
echo "Nama kelas ini adalah " . __CLASS__;
|
||||||
|
|
||||||
|
// Mendapatkan alamat lengkap direktori
|
||||||
|
echo "Alamat direktori ini adalah " . __DIR__;
|
||||||
|
|
||||||
|
// Beberapa yang banyak digunakan
|
||||||
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
|
// Mendapatkan alamat lengkap dokumen
|
||||||
|
echo "Alamat dokumen ini adalah " . __FILE__;
|
||||||
|
|
||||||
|
// Mendapatkan nama fungsi
|
||||||
|
echo "Nama fungsi ini adalah " . __FUNCTION__;
|
||||||
|
|
||||||
|
// Mendapatkan nomor baris perintah
|
||||||
|
echo "Nomor baris perintah ini adalah " . __LINE__;
|
||||||
|
|
||||||
|
// Mendapatkan nama method. Hanya mengembalikan sebuah nilai jika berada didalam trait atau deklarasi objek.
|
||||||
|
echo "Nama method ini adalah " . __METHOD__;
|
||||||
|
|
||||||
|
// Mendapatkan nama namespace
|
||||||
|
echo "Namespace saat ini adalah " . __NAMESPACE__;
|
||||||
|
|
||||||
|
// Mendapatkan nama dari trait. Hanya mengembalikan sebuah nilai jika berada didalam trait atau deklarasi objek.
|
||||||
|
echo "Namespace saat ini adalah " . __TRAIT__;
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* Penanganan Kesalahan (Error)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Penanganan error sederhana menggunakan "try...catch"
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Kerjakan sesuatu
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// Penanganan exception
|
||||||
|
}
|
||||||
|
|
||||||
|
// Menggunakan "try...catch" blok pada namespace
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Kerjakan sesuatu
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// Penanganan exception
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exception khusus
|
||||||
|
|
||||||
|
class ExceptionSaya extends Exception {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$kondisi = true;
|
||||||
|
|
||||||
|
if ($kondisi) {
|
||||||
|
throw new ExceptionSaya('Terjadi sesuatu');
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (ExceptionSaya $e) {
|
||||||
|
// Penanganan untuk exception khusus
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Informasi lainnya
|
||||||
|
|
||||||
|
Kunjungi [Dokumentasi resmi PHP](http://www.php.net/manual/) untuk referensi dan masukan komunitas.
|
||||||
|
|
||||||
|
Jika anda tertarik untuk belajar lebih dalam, kunjungi
|
||||||
|
[PHP The Right Way](http://www.phptherightway.com/).
|
||||||
|
|
||||||
|
Jika anda terbiasa dengan manajemen paket, kunjungi
|
||||||
|
[Composer](http://getcomposer.org/).
|
||||||
|
|
||||||
|
Untuk standar umum, kunjungi PHP Framework Interoperability Group's
|
||||||
|
[PSR standards](https://github.com/php-fig/fig-standards).
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
||||||
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
@ -438,7 +438,7 @@ public class LearnJava {
|
|||||||
|
|
||||||
|
|
||||||
// You can include other, non-public outer-level classes in a .java file,
|
// You can include other, non-public outer-level classes in a .java file,
|
||||||
// but it is good practice. Instead split classes into separate files.
|
// but it is not good practice. Instead split classes into separate files.
|
||||||
|
|
||||||
|
|
||||||
// Class Declaration Syntax:
|
// Class Declaration Syntax:
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
||||||
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
@ -8,7 +8,7 @@ contributors:
|
|||||||
Less is a CSS pre-processor, that adds features such as variables, nesting, mixins and more.
|
Less is a CSS pre-processor, that adds features such as variables, nesting, mixins and more.
|
||||||
Less (and other preprocessors, such as [Sass](http://sass-lang.com/) help developers to write maintainable and DRY (Don't Repeat Yourself) code.
|
Less (and other preprocessors, such as [Sass](http://sass-lang.com/) help developers to write maintainable and DRY (Don't Repeat Yourself) code.
|
||||||
|
|
||||||
```less
|
```css
|
||||||
|
|
||||||
|
|
||||||
//Single line comments are removed when Less is compiled to CSS.
|
//Single line comments are removed when Less is compiled to CSS.
|
||||||
|
@ -11,7 +11,7 @@ target to the most recent version of the source. Famously written over a
|
|||||||
weekend by Stuart Feldman in 1976, it is still widely used (particularly
|
weekend by Stuart Feldman in 1976, it is still widely used (particularly
|
||||||
on Unix) despite many competitors and criticisms.
|
on Unix) despite many competitors and criticisms.
|
||||||
|
|
||||||
There are many varieties of make in existance, this article assumes that
|
There are many varieties of make in existence, this article assumes that
|
||||||
we are using GNU make which is the standard on Linux.
|
we are using GNU make which is the standard on Linux.
|
||||||
|
|
||||||
```make
|
```make
|
||||||
|
@ -11,7 +11,7 @@ that gives the programmer power without compromises on runtime efficiency.
|
|||||||
|
|
||||||
Nim is efficient, expressive, and elegant.
|
Nim is efficient, expressive, and elegant.
|
||||||
|
|
||||||
```nimrod
|
```javascript
|
||||||
var # Declare (and assign) variables,
|
var # Declare (and assign) variables,
|
||||||
letter: char = 'n' # with or without type annotations
|
letter: char = 'n' # with or without type annotations
|
||||||
lang = "N" & "im"
|
lang = "N" & "im"
|
||||||
|
@ -1,235 +0,0 @@
|
|||||||
---
|
|
||||||
category: tool
|
|
||||||
tool: amd
|
|
||||||
contributors:
|
|
||||||
- ["Frederik Ring", "https://github.com/m90"]
|
|
||||||
translators:
|
|
||||||
- ["Reinoud Kruithof", "https://github.com/reinoudk"]
|
|
||||||
filename: learnamd-nl.js
|
|
||||||
lang: nl-nl
|
|
||||||
---
|
|
||||||
|
|
||||||
## Aan de slag met AMD
|
|
||||||
|
|
||||||
De **Asynchronous Module Definition** API specificeert een mechanisme om JavaScript
|
|
||||||
modules the definiëren zodat de module en dependencies (afhankelijkheden) asynchroon
|
|
||||||
geladen kunnen worden. Dit is vooral erg geschikt voor de browseromgeving, waar het
|
|
||||||
synchroon laden van modules zorgt voor problemen qua prestatie, gebruiksvriendelijkheid,
|
|
||||||
debugging en cross-domain toegangsproblemen.
|
|
||||||
|
|
||||||
### Basis concept
|
|
||||||
```javascript
|
|
||||||
// De basis AMD API bestaat uit niks meer dan twee methodes: `define` en `require`
|
|
||||||
// and gaat vooral over de definitie en gebruik van modules:
|
|
||||||
// `define(id?, dependencies?, factory)` definieert een module
|
|
||||||
// `require(dependencies, callback)` importeert een set van dependencies en
|
|
||||||
// gebruikt ze in de gegeven callback
|
|
||||||
|
|
||||||
// Laten we starten met het gebruiken van define om een nieuwe module (met naam)
|
|
||||||
// te creëeren, welke geen dependencies heeft. Dit doen we door een naam
|
|
||||||
// en een zogeheten factory functie door te geven aan define:
|
|
||||||
define('awesomeAMD', function(){
|
|
||||||
var isAMDAwesome = function(){
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
// De return waarde van een module's factory functie is
|
|
||||||
// wat andere modules of require calls ontvangen wanneer
|
|
||||||
// ze onze `awesomeAMD` module requiren.
|
|
||||||
// De geëxporteerde waarde kan van alles zijn: (constructor) functies,
|
|
||||||
// objecten, primitives, zelfs undefined (hoewel dat niet veel nut heeft).
|
|
||||||
return isAMDAwesome;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// We gaan nu een andere module defineren die afhankelijk is van onze
|
|
||||||
// `awesomeAMD` module. Merk hierbij op dat er nu een extra functieargument
|
|
||||||
// is die de dependencies van onze module defineert:
|
|
||||||
define('schreewlelijk', ['awesomeAMD'], function(awesomeAMD){
|
|
||||||
// dependencies worden naar de factory's functieargumenten
|
|
||||||
// gestuurd in de volgorde waarin ze gespecificeert zijn
|
|
||||||
var vertelIedereen = function(){
|
|
||||||
if (awesomeAMD()){
|
|
||||||
alert('Dit is zOoOo cool!');
|
|
||||||
} else {
|
|
||||||
alert('Vrij saai, niet?');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return vertelIedereen;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Nu we weten hoe we define moeten gebruiken, kunnen we require gebruiken
|
|
||||||
// om ons programma mee te starten. De vorm van `require` is
|
|
||||||
// `(arrayVanDependencies, callback)`.
|
|
||||||
require(['schreeuwlelijk'], function(schreewlelijk){
|
|
||||||
schreeuwlelijk();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Om deze tutorial code uit te laten voeren, gaan we hier een vrij basic
|
|
||||||
// (niet-asynchrone) versie van AMD implementeren:
|
|
||||||
function define(naam, deps, factory){
|
|
||||||
// merk op hoe modules zonder dependencies worden afgehandeld
|
|
||||||
define[naam] = require(factory ? deps : [], factory || deps);
|
|
||||||
}
|
|
||||||
|
|
||||||
function require(deps, callback){
|
|
||||||
var args = [];
|
|
||||||
// we halen eerst alle dependecies op die nodig zijn
|
|
||||||
// om require aan te roepen
|
|
||||||
for (var i = 0; i < deps.length; i++){
|
|
||||||
args[i] = define[deps[i]];
|
|
||||||
}
|
|
||||||
// voldoe aan alle dependencies van de callback
|
|
||||||
return callback.apply(null, args);
|
|
||||||
}
|
|
||||||
// je kan deze code hier in actie zien (Engels): http://jsfiddle.net/qap949pd/
|
|
||||||
```
|
|
||||||
|
|
||||||
### require.js in de echte wereld
|
|
||||||
|
|
||||||
In contrast met het voorbeeld uit de introductie, implementeert `require.js`
|
|
||||||
(de meest populaire AMD library) de **A** in **AMD**. Dit maakt het mogelijk
|
|
||||||
om je modules en hun dependencies asynchroon in the laden via XHR:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
/* file: app/main.js */
|
|
||||||
require(['modules/someClass'], function(SomeClass){
|
|
||||||
// de callback word uitgesteld tot de dependency geladen is
|
|
||||||
var things = new SomeClass();
|
|
||||||
});
|
|
||||||
console.log('Dus, hier wachten we!'); // dit wordt als eerste uitgevoerd
|
|
||||||
```
|
|
||||||
|
|
||||||
De afspraak is dat je over het algemeen één module in één bestand opslaat.
|
|
||||||
`require.js` kan module-namen achterhalen gebaseerd op de bestandslocatie,
|
|
||||||
dus je hoeft je module geen naam te geven. Je kan simpelweg aan ze referen
|
|
||||||
door hun locatie te gebruiken.
|
|
||||||
In het voorbeeld nemen we aan dat `someClass` aanwezig is in de `modules` map,
|
|
||||||
relatief ten opzichte van de `baseUrl` uit je configuratie.
|
|
||||||
|
|
||||||
* app/
|
|
||||||
* main.js
|
|
||||||
* modules/
|
|
||||||
* someClass.js
|
|
||||||
* someHelpers.js
|
|
||||||
* ...
|
|
||||||
* daos/
|
|
||||||
* things.js
|
|
||||||
* ...
|
|
||||||
|
|
||||||
Dit betekent dat we `someClass` kunnen defineren zonder een module-id te specificeren:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
/* file: app/modules/someClass.js */
|
|
||||||
define(['daos/things', 'modules/someHelpers'], function(thingsDao, helpers){
|
|
||||||
// definitie van de module gebeurt, natuurlijk, ook asynchroon
|
|
||||||
function SomeClass(){
|
|
||||||
this.method = function(){/**/};
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
return SomeClass;
|
|
||||||
});
|
|
||||||
```
|
|
||||||
Gebruik `requirejs.config(configObj)` om het gedrag van de standaard mapping
|
|
||||||
aan te passen in je `main.js`:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
/* file: main.js */
|
|
||||||
requirejs.config({
|
|
||||||
baseUrl : 'app',
|
|
||||||
paths : {
|
|
||||||
// je kan ook modules uit andere locatie inladen
|
|
||||||
jquery : '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min',
|
|
||||||
coolLibUitBower : '../bower_components/cool-lib/coollib'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
require(['jquery', 'coolLibUitBower', 'modules/someHelpers'], function($, coolLib, helpers){
|
|
||||||
// een `main` bestand moet require minstens eenmaal aanroepen,
|
|
||||||
// anders zal er geen code uitgevoerd worden
|
|
||||||
coolLib.doFancyDingenMet(helpers.transform($('#foo')));
|
|
||||||
});
|
|
||||||
```
|
|
||||||
Op `require.js` gebaseerde apps hebben vaak een enkel beginpunt (`main.js`)
|
|
||||||
welke toegevoegd wordt aan de `require.js` script tag als een data-attribuut.
|
|
||||||
Deze zal automisch geladen en uitgevoerd worden als de pagina laadt:
|
|
||||||
|
|
||||||
```html
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Honder script tags? Nooi meer!</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<script src="require.js" data-main="app/main"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
```
|
|
||||||
|
|
||||||
### Een heel project optimaliseren met r.js
|
|
||||||
|
|
||||||
Veel mensen geven er de voorkeur aan om AMD te gebruiken tijdens de
|
|
||||||
ontwikkelfase om code op een gezonde manier te organiseren maar
|
|
||||||
willen nog steeds een enkel scriptbestand gebruiken in productie in
|
|
||||||
plaats van honderderen XHR verzoeken uit te voeren als de pagina laadt.
|
|
||||||
|
|
||||||
`require.js` wordt geleverd met een script genaamd `r.js` (die je waarschijnlijk
|
|
||||||
uitvoert in node.js, hoewel Rhino ook ondersteund wordt) welke de
|
|
||||||
dependency book van je project analyseert en een enkel bestand bouwt met daarin
|
|
||||||
al je module (juist genaamd), geminificeerd en klaar voor productie.
|
|
||||||
|
|
||||||
Instaleren met `npm`:
|
|
||||||
```shell
|
|
||||||
$ npm install requirejs -g
|
|
||||||
```
|
|
||||||
|
|
||||||
Nu kun je het een configuratiebestand voeden:
|
|
||||||
```shell
|
|
||||||
$ r.js -o app.build.js
|
|
||||||
```
|
|
||||||
|
|
||||||
Voor ons bovenstaande voorbeeld zou de configuratie er zo uit kunnen zien:
|
|
||||||
```javascript
|
|
||||||
/* file : app.build.js */
|
|
||||||
({
|
|
||||||
name : 'main', // naam van het beginpunt
|
|
||||||
out : 'main-built.js', // naam van het bestand waar de output naar geschreven wordt
|
|
||||||
baseUrl : 'app',
|
|
||||||
paths : {
|
|
||||||
// `empty:` verteld r.js dat dee nog steeds geladen moet worden van de CDN,
|
|
||||||
// gebruik makend van de locatie gespecificeert in `main.js`
|
|
||||||
jquery : 'empty:',
|
|
||||||
coolLibUitBower : '../bower_components/cool-lib/coollib'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
```
|
|
||||||
Verwissel simpelweg `data-main` om het gebouwde bestand te gebruiken in productie:
|
|
||||||
```html
|
|
||||||
<script src="require.js" data-main="app/main-built"></script>
|
|
||||||
```
|
|
||||||
|
|
||||||
Een erg gedetaileerd [overzicht van bouwopties](https://github.com/jrburke/r.js/blob/master/build/example.build.js) is
|
|
||||||
beschikbar in de GitHub repo (Engels).
|
|
||||||
|
|
||||||
Hieronder vind je nog meer informatie over AMD (Engels).
|
|
||||||
|
|
||||||
### Onderwerpen die niet aan bod zijn gekomen
|
|
||||||
* [Loader plugins / transforms](http://requirejs.org/docs/plugins.html)
|
|
||||||
* [CommonJS style loading and exporting](http://requirejs.org/docs/commonjs.html)
|
|
||||||
* [Advanced configuration](http://requirejs.org/docs/api.html#config)
|
|
||||||
* [Shim configuration (loading non-AMD modules)](http://requirejs.org/docs/api.html#config-shim)
|
|
||||||
* [CSS loading and optimizing with require.js](http://requirejs.org/docs/optimization.html#onecss)
|
|
||||||
* [Using almond.js for builds](https://github.com/jrburke/almond)
|
|
||||||
|
|
||||||
### Verder lezen:
|
|
||||||
|
|
||||||
* [Official Spec](https://github.com/amdjs/amdjs-api/wiki/AMD)
|
|
||||||
* [Why AMD?](http://requirejs.org/docs/whyamd.html)
|
|
||||||
* [Universal Module Definition](https://github.com/umdjs/umd)
|
|
||||||
|
|
||||||
### Implementaties:
|
|
||||||
|
|
||||||
* [require.js](http://requirejs.org)
|
|
||||||
* [dojo toolkit](http://dojotoolkit.org/documentation/tutorials/1.9/modules/)
|
|
||||||
* [cujo.js](http://cujojs.com/)
|
|
||||||
* [curl.js](https://github.com/cujojs/curl)
|
|
||||||
* [lsjs](https://github.com/zazl/lsjs)
|
|
||||||
* [mmd](https://github.com/alexlawrence/mmd)
|
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
||||||
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
||||||
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
||||||
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
@ -174,6 +174,10 @@ some_var # => 5
|
|||||||
# See Control Flow to learn more about exception handling.
|
# See Control Flow to learn more about exception handling.
|
||||||
some_unknown_var # Raises a NameError
|
some_unknown_var # Raises a NameError
|
||||||
|
|
||||||
|
# if can be used as an expression
|
||||||
|
# Equivalent of C's '?:' ternary operator
|
||||||
|
"yahoo!" if 3 > 2 else 2 # => "yahoo!"
|
||||||
|
|
||||||
# Lists store sequences
|
# Lists store sequences
|
||||||
li = []
|
li = []
|
||||||
# You can start with a prefilled list
|
# You can start with a prefilled list
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
||||||
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
- ["Mathias Bynens", "http://mathiasbynens.be/"]
|
@ -7,11 +7,12 @@ contributors:
|
|||||||
- ["Andre Polykanine", "http://oire.me/"]
|
- ["Andre Polykanine", "http://oire.me/"]
|
||||||
lang: ru-ru
|
lang: ru-ru
|
||||||
---
|
---
|
||||||
|
|
||||||
D - современный компилируемый язык общего назначения с Си-подобным синтаксисом,
|
D - современный компилируемый язык общего назначения с Си-подобным синтаксисом,
|
||||||
который сочетает удобство, продуманный дизайн и высокую производительность.
|
который сочетает удобство, продуманный дизайн и высокую производительность.
|
||||||
D - это С++, сделанный правильно.
|
D - это С++, сделанный правильно.
|
||||||
|
|
||||||
```d
|
```c
|
||||||
// Welcome to D! Это однострочный комментарий
|
// Welcome to D! Это однострочный комментарий
|
||||||
|
|
||||||
/* многострочный
|
/* многострочный
|
||||||
|
@ -48,7 +48,7 @@ println(10)
|
|||||||
// Printing, without forcing a new line on next print
|
// Printing, without forcing a new line on next print
|
||||||
print("Hello world")
|
print("Hello world")
|
||||||
print(10)
|
print(10)
|
||||||
// Hello world!10
|
// Hello world10
|
||||||
|
|
||||||
// Declaring values is done using either var or val.
|
// Declaring values is done using either var or val.
|
||||||
// val declarations are immutable, whereas vars are mutable. Immutability is
|
// val declarations are immutable, whereas vars are mutable. Immutability is
|
||||||
|
@ -7,9 +7,9 @@ contributors:
|
|||||||
- ["Connor Shea", "https://github.com/connorshea"]
|
- ["Connor Shea", "https://github.com/connorshea"]
|
||||||
- ["Deepanshu Utkarsh", "https://github.com/duci9y"]
|
- ["Deepanshu Utkarsh", "https://github.com/duci9y"]
|
||||||
translators:
|
translators:
|
||||||
- ["Rasendran Kirushan", "https://github.com/kirushanr"]
|
- ["Rasendran Kirushan", "https://github.com/kirushanr"]
|
||||||
filename: learncss.css
|
filename: learncss-ta.css
|
||||||
lang:in-ta
|
lang: in-ta
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,8 +5,8 @@ contributors:
|
|||||||
- ['Ariel Krakowski', 'http://www.learneroo.com']
|
- ['Ariel Krakowski', 'http://www.learneroo.com']
|
||||||
translators:
|
translators:
|
||||||
- ["Rasendran Kirushan", "https://github.com/kirushanr"]
|
- ["Rasendran Kirushan", "https://github.com/kirushanr"]
|
||||||
filename: javascript.js
|
filename: javascript-ta.js
|
||||||
lang:in-ta
|
lang: in-ta
|
||||||
---
|
---
|
||||||
|
|
||||||
javascript 1995 ஆம் ஆண்டு Netscape இல் பணிபுரிந்த Brendan Eich
|
javascript 1995 ஆம் ஆண்டு Netscape இல் பணிபுரிந்த Brendan Eich
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
---
|
---
|
||||||
language: xml
|
language: xml
|
||||||
filename: learnxml.xml
|
filename: learnxml-ta.xml
|
||||||
contributors:
|
contributors:
|
||||||
- ["João Farias", "https://github.com/JoaoGFarias"]
|
- ["João Farias", "https://github.com/JoaoGFarias"]
|
||||||
translators:
|
translators:
|
||||||
- ["Rasendran Kirushan", "https://github.com/kirushanr"]
|
- ["Rasendran Kirushan", "https://github.com/kirushanr"]
|
||||||
lang:in-ta
|
lang: in-ta
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
filename: brainfuck-tr
|
filename: brainfuck-tr
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io"]
|
@ -9,7 +9,7 @@ filename: LearnZfs.txt
|
|||||||
|
|
||||||
[ZFS](http://open-zfs.org/wiki/Main_Page)
|
[ZFS](http://open-zfs.org/wiki/Main_Page)
|
||||||
is a rethinking of the storage stack, combining traditional file systems as well as volume
|
is a rethinking of the storage stack, combining traditional file systems as well as volume
|
||||||
managers into one cohesive tool. ZFS has some specific teminology that sets it appart from
|
managers into one cohesive tool. ZFS has some specific terminology that sets it apart from
|
||||||
more traditional storage systems, however it has a great set of features with a focus on
|
more traditional storage systems, however it has a great set of features with a focus on
|
||||||
usability for systems administrators.
|
usability for systems administrators.
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ types of VDEV's that offer various advantages, including redundancy and speed.
|
|||||||
VDEV's offer better reliability and safety than a RAID card. It is discouraged to use a
|
VDEV's offer better reliability and safety than a RAID card. It is discouraged to use a
|
||||||
RAID setup with ZFS, as ZFS expects to directly manage the underlying disks.
|
RAID setup with ZFS, as ZFS expects to directly manage the underlying disks.
|
||||||
|
|
||||||
Types of VDEV's
|
Types of VDEV's
|
||||||
* stripe (a single disk, no redundancy)
|
* stripe (a single disk, no redundancy)
|
||||||
* mirror (n-way mirrors supported)
|
* mirror (n-way mirrors supported)
|
||||||
* raidz
|
* raidz
|
||||||
@ -39,13 +39,13 @@ increase your IOPS.
|
|||||||
### Storage Pools
|
### Storage Pools
|
||||||
|
|
||||||
ZFS uses Storage Pools as an abstraction over the lower level storage provider (VDEV), allow
|
ZFS uses Storage Pools as an abstraction over the lower level storage provider (VDEV), allow
|
||||||
you to separate the user visable file system from the physcal layout.
|
you to separate the user visible file system from the physical layout.
|
||||||
|
|
||||||
### ZFS Dataset
|
### ZFS Dataset
|
||||||
|
|
||||||
ZFS datasets are analagous to traditional filesystems but with many more features. They
|
ZFS datasets are analogous to traditional filesystems but with many more features. They
|
||||||
provide many of ZFS's advantages. Datasets support [Copy on Write](https://en.wikipedia.org/wiki/Copy-on-write)
|
provide many of ZFS's advantages. Datasets support [Copy on Write](https://en.wikipedia.org/wiki/Copy-on-write)
|
||||||
snapshots, quota's, compression and deduplication.
|
snapshots, quota's, compression and de-duplication.
|
||||||
|
|
||||||
|
|
||||||
### Limits
|
### Limits
|
||||||
@ -68,7 +68,7 @@ Actions:
|
|||||||
List zpools
|
List zpools
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Create a raidz zpool
|
# Create a raidz zpool
|
||||||
$ zpool create bucket raidz1 gpt/zfs0 gpt/zfs1 gpt/zfs2
|
$ zpool create bucket raidz1 gpt/zfs0 gpt/zfs1 gpt/zfs2
|
||||||
|
|
||||||
# List ZPools
|
# List ZPools
|
||||||
@ -347,7 +347,7 @@ $ zfs promote tank/home/sarlalian_new
|
|||||||
|
|
||||||
### Putting it all together
|
### Putting it all together
|
||||||
|
|
||||||
This following a script utilizing FreeBSD, jails and ZFS to automate
|
This following a script utilizing FreeBSD, jails and ZFS to automate
|
||||||
provisioning a clean copy of a mysql staging database from a live replication
|
provisioning a clean copy of a mysql staging database from a live replication
|
||||||
slave.
|
slave.
|
||||||
|
|
||||||
@ -384,7 +384,7 @@ mv /jails/staging/etc/rc.conf.staging /jails/staging/etc/rc.conf.local
|
|||||||
echo "==== Starting the staging db server ===="
|
echo "==== Starting the staging db server ===="
|
||||||
jail -c staging
|
jail -c staging
|
||||||
|
|
||||||
echo "==== Make sthe staging database not pull from the master ===="
|
echo "==== Makes the staging database not pull from the master ===="
|
||||||
echo "STOP SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging
|
echo "STOP SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging
|
||||||
echo "RESET SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging
|
echo "RESET SLAVE;" | /usr/local/bin/mysql -u root -pmyrootpassword -h staging
|
||||||
```
|
```
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: brainfuck
|
language: bf
|
||||||
lang: zh-cn
|
lang: zh-cn
|
||||||
contributors:
|
contributors:
|
||||||
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
- ["Prajit Ramachandran", "http://prajitr.github.io/"]
|
Loading…
Reference in New Issue
Block a user