-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby-101.txt
205 lines (165 loc) · 3.62 KB
/
ruby-101.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
Vortrag
==============================
------------------------------
Hello World
# java
System.out.println("Hello World");
# ruby
puts("Hello World");
puts("Hello World")
puts "Hello World"
------------------------------
Variablen
# java
int answer = 42; // ich bin ein kommentar
Integer answer2 = new Integer(42);
String foo = "Hello World";
List<String> users = new ArrayList<String>();
String[] strArray = new String[] {"foo", "bar"};
Map map = new HashMap();
# ruby
answer = 42 # ich bin auch ein kommentar
answer2 = 42;
foo = "Hello World"
str_array = ["foo", "bar"]
users = []
map = {}
------------------------------
Maps & Arrays
# java
users.add("foo");
users.add("bar");
map.put("key", "thevalue");
map.get("key");
# ruby
users.push("foo");
users << "bar"
map["key"] = "thevalue"
map["key"]
map = {"key" => "thevalue"}
------------------------------
Iteratoren
for (String s : objects) {
System.out.println(s);
}
# ruby
objects = ["foo", "bar"]
for object in objects do |object|
puts object
end
objects.each {|object| puts object }
-----------------------------
Kontrollstrukturen (if / else / case)
int i = 5;
if (i == 5) {
System.out.println("i = 7");
}
else if (i == 6) {
System.out.println("6");
}
else {
System.out.println("something else");
}
i = 5
if i == 7
puts "i = 7"
elsif i == 6
puts "6"
else
puts "something else"
end
puts 'foo' if i == 5 # => foo
puts 'bar' unless i == 5 # =>
------------------------------
Nil
foo = "bar" # => "bar"
foo.nil? # => false
foo = nil # => nil => NilClass.new
foo.nil? # => true
nil == false
if foo
puts "foo ist nicht false und nicht nil"
else
puts "foo ist false oder nil"
end
-------------------------------
Ruby Kurzformen
i = 5
if i == 7 then puts "i = 7"
elsif i == 6 then puts "6"
else puts "something else"
end
puts if i == 7 then "i = 7"
elsif i == 6 then "6"
else "something else"
end
puts i == 7 ? "i = 7" : "something else"
-------------------------------
Funktionen
class Foo {
public String function bar {
return "bar";
}
}
class Foo
def bar
"bar"
end
end
def bar(argument = "default")
puts argument
end
bar() # => default
bar("nodefault") # => nodefault
# In Ruby keine überladenen Methoden
def quoz(options={})
...
end
def foos(arg)
if arg.kind_of? String
puts "It is a String"
else
puts "something else"
end
end
-------------------------------
Proc & Lambda
block = Proc.new {
puts "bar"
}
block.call # => bar
def div_tag(class_name, &block)
puts "<div class=\"#{class_name}\">"
block.call
puts "</div>"
end
def div_tag(class_name)
puts "<div class=\"#{class_name}\">"
block.call # oder yield
puts "</div>"
end
div_tag('my_class') { puts 'between' }
# <div class="my_class">
# between
# </div>
-------------------------------
RegExp
/abc/ =~ "This is abc" # => Index des Matches
/abc/.match("This is abc") # => MatchData # MatchData.to_s => 'abc'
puts "found #{$1}" if /abc(\d\d)/ =~ "foo bar abc12 foo" # => found 12
-----------------------------
Stringformatierungen
i = 5
'bar #{i}' # => bar #{i}
"bar #{i}" # => bar 5
var = <<-EOS
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
#{i}
EOS
---------------------------------
Ruby Schönheit
3.down_to(1).each { |i| puts i } # => 321
["live", "ton", "crap", "si", "evil"].map { |s| s.reverse.capitalize }.reverse.reject { |item| item == "Parc" }.join(' ') # => "Live Is Not Evil"
[["Java", "is", "nice"], ["is", "Java", "nice"], ["evil", "is", "live"]].transpose.first.join(" ")