| 50 | | |
| 51 | | |
| 52 | | |
| 53 | | |
| 54 | | |
| | 50 | =begin |
| | 51 | p 'name' |
| | 52 | p :name |
| | 53 | =end |
| | 54 | |
| | 55 | =begin |
| | 56 | p "name".object_id == "name".object_id |
| | 57 | p :name.object_id == :name.object_id |
| | 58 | =end |
| | 59 | |
| | 60 | =begin |
| | 61 | p "name".class |
| | 62 | p "name".intern.class |
| | 63 | p :name.object_id == "name".object_id |
| | 64 | p :name.object_id == "name".intern.object_id |
| | 65 | =end |
| | 66 | |
| | 67 | =begin |
| | 68 | p [1, 2, 3] |
| | 69 | p [:num, 10] |
| | 70 | =end |
| | 71 | |
| | 72 | =begin |
| | 73 | p %w(a b c) |
| | 74 | =end |
| | 75 | |
| | 76 | =begin |
| | 77 | array = [1, 2, 3, 4] |
| | 78 | p array.size # 4 |
| | 79 | p array[0] # 1 |
| | 80 | p array[1,3] # [2, 3, 4] |
| | 81 | p array[4] # nil |
| | 82 | p array[-1] # 4 |
| | 83 | p array[0..2] # [1, 2, 3] |
| | 84 | |
| | 85 | p [3,1,2].sort # [1, 2, 3] |
| | 86 | p %w( a b c ).join(',') # "a,b,c" |
| | 87 | |
| | 88 | array = [1,2,3,2] |
| | 89 | array.uniq! |
| | 90 | p array # [1, 2, 3] |
| | 91 | |
| | 92 | [1,2].each{ |i| puts i } |
| | 93 | p [1,2].map{ |i| i*10 } # [10, 20] |
| | 94 | p [1,2].push(:a) # [1, 2, :a] |
| | 95 | =end |
| | 96 | |
| | 97 | =begin |
| | 98 | valids = { 0 => '無効', 1 => '有効' } |
| | 99 | hosts = { |
| | 100 | "localhost" => "127.0.0.1", |
| | 101 | "router" => "192.168.0.1", |
| | 102 | } |
| | 103 | |
| | 104 | p hosts["router"] # "192.168.0.1" |
| | 105 | hosts["router"] = nil |
| | 106 | p hosts["router"] # nil |
| | 107 | =end |
| | 108 | |
| | 109 | =begin |
| | 110 | options = { |
| | 111 | :root => 1, |
| | 112 | :elements => ['a', 'b', 'c'], |
| | 113 | "status" => "OK", |
| | 114 | 200 => :ok, |
| | 115 | } |
| | 116 | =end |
| | 117 | |
| | 118 | =begin |
| | 119 | hash = { :a=>1, :b=>2} |
| | 120 | p hash.size # 2 |
| | 121 | p hash[0] # nil |
| | 122 | p hash['a'] # nil |
| | 123 | p hash[:a] # 1 |
| | 124 | p hash.keys # [:a, :b] |
| | 125 | p hash.values # [1, 2] |
| | 126 | |
| | 127 | hash.each_pair do |key, val| |
| | 128 | puts "key=#{key}, val=#{val}" |
| | 129 | end |
| | 130 | # key=a, val=1 |
| | 131 | # key=b, val=2 |
| | 132 | =end |
| | 133 | |
| | 134 | =begin |
| | 135 | flag = 0 |
| | 136 | if flag |
| | 137 | puts "execute" |
| | 138 | end |
| | 139 | =end |
| | 140 | |
| | 141 | =begin |
| | 142 | print "処理を開始します" if verbose? |
| | 143 | |
| | 144 | if verbose? |
| | 145 | print "処理を開始します" |
| | 146 | end |
| | 147 | =end |
| | 148 | |
| | 149 | =begin |
| | 150 | def hello1 |
| | 151 | "hello!" |
| | 152 | end |
| | 153 | |
| | 154 | def hello2 |
| | 155 | return "hello!" |
| | 156 | end |
| | 157 | |
| | 158 | p hello1() # "hello!" |
| | 159 | p hello2() # "hello!" |
| | 160 | =end |
| | 161 | |
| | 162 | =begin |
| | 163 | def hello(name) |
| | 164 | puts "こんにちは、#{name}さん" |
| | 165 | end |
| | 166 | |
| | 167 | hello("ruby") # こんにちは、rubyさん |
| | 168 | hello "ruby" # こんにちは、rubyさん |
| | 169 | =end |
| | 170 | |
| | 171 | =begin |
| | 172 | def hello |
| | 173 | "メソッドからこんにちは" |
| | 174 | end |
| | 175 | |
| | 176 | hello = "変数でハロー" |
| | 177 | p hello |
| | 178 | =end |
| | 179 | |
| | 180 | =begin |
| | 181 | def hello |
| | 182 | "メソッドからこんにちは" |
| | 183 | end |
| | 184 | |
| | 185 | hello = "変数でハロー" |
| | 186 | puts hello |
| | 187 | puts hello() |
| | 188 | =end |
| | 189 | |
| | 190 | =begin |
| | 191 | link_to "詳細", :action=>"show", :id=>8 |
| | 192 | link_to("詳細", :action=>"show", :id=>8) |
| | 193 | link_to("詳細", {:action=>"show", :id=>8}) |
| | 194 | |
| | 195 | options = { |
| | 196 | :action => "show", |
| | 197 | :id => 8 |
| | 198 | } |
| | 199 | link_to("詳細", options) |
| | 200 | |
| | 201 | foo("test", :name=>"ruby", 1) # SyntaxError |
| | 202 | |
| | 203 | link_to "詳細", {:action=>"show", :id=>8}, :title=>"click" |
| | 204 | =end |
| | 205 | |
| | 206 | =begin |
| | 207 | p 1 + 1 # 2 |
| | 208 | block = Proc.new{ 1 + 1 } |
| | 209 | p block.call # 2 |
| | 210 | =end |
| | 211 | |
| | 212 | =begin |
| | 213 | def foo |
| | 214 | yield |
| | 215 | end |
| | 216 | |
| | 217 | #p foo{ 1+1} 2 |
| | 218 | #foo # LocalJumpError |
| | 219 | =end |
| | 220 | |
| | 221 | =begin |
| | 222 | def foo (&block) |
| | 223 | block.call |
| | 224 | end |
| | 225 | |
| | 226 | foo { p :ok } |
| | 227 | =end |
| | 228 | |
| | 229 | =begin |
| | 230 | sum = Proc.new{ |a,b| a+b } |
| | 231 | p sum.call(1,2) # 3 |
| | 232 | =end |
| | 233 | |
| | 234 | =begin |
| | 235 | [1,2,3].each{ |i| p i } |
| | 236 | p [1,2,3].map{ |i| i.to_s } # ["1", "2", "3"] |
| | 237 | p (1..10).select{ |i| i%2 == 0 } # [2, 4, 6, 8, 10] |
| | 238 | =end |
| | 239 | |
| | 240 | =begin |
| | 241 | class Array |
| | 242 | def each |
| | 243 | size.times {|i| yield(self[i])} |
| | 244 | end |
| | 245 | |
| | 246 | def collect |
| | 247 | converted = [] |
| | 248 | each { |e| converted << yield(e) } |
| | 249 | return converted |
| | 250 | end |
| | 251 | |
| | 252 | def select |
| | 253 | selected = [] |
| | 254 | each { |e| selected << e if yield(e)} |
| | 255 | return selected |
| | 256 | end |
| | 257 | end |
| | 258 | =end |
| | 272 | =begin |
| | 273 | module Foo |
| | 274 | class Bar |
| | 275 | end |
| | 276 | end |
| | 277 | =end |
| | 278 | |
| | 279 | =begin |
| | 280 | p $LOAD_PATH |
| | 281 | $LOAD_PATH << "lib" |
| | 282 | p $LOAD_PATH |
| | 283 | $LOAD_PATH.unshift "lib" |
| | 284 | p $LOAD_PATH |
| | 285 | =end |
| | 286 | |
| | 287 | =begin |
| | 288 | require 'rubygems' |
| | 289 | require 'active_support' |
| | 290 | $: << 'lib' |
| | 291 | #bar = Foo::Bar.new |
| | 292 | =end |
| | 293 | |
| | 294 | =begin |
| | 295 | require 'rubygems' |
| | 296 | require 'active_support' |
| | 297 | p "Rails".underscore # "rails" |
| | 298 | p "ActiveRecord".underscore # "active_record" |
| | 299 | p "Active1Record".underscore # "active1_record" |
| | 300 | p "Active1record".underscore # "active1record" |
| | 301 | p "ToDoList".underscore # "to_do_list" |
| | 302 | p "XML".underscore # "xml" |
| | 303 | p "XML2".underscore # "xml2" |
| | 304 | p "XMLdata".underscore # "xm_ldata" |
| | 305 | p "XMLData".underscore # "xml_data" |
| | 306 | p "XML2Data".underscore # "xml2_data" |
| | 307 | p "Iso2022jpMailer".underscore # "iso2022jp_mailer" |
| | 308 | =end |
| | 309 | |
| | 310 | =begin |
| | 311 | require 'rubygems' |
| | 312 | require 'active_support' |
| | 313 | p "Admin::SuperUser".underscore # "admin/super_user" |
| | 314 | p "admin/super_user".classify # "Admin::SuperUser" |
| | 315 | =end |