
Ruby Language #ruby 1 1: Ruby 2 2 2 Examples 2 IRBHello World 2 tk 2 2 3 Hello World 3 Hello World - ShebangUnix 4 4 4 4 2: C 5 Examples 5 5 C 5 C - RubyInLine 7 3: ERB 9 9 9 9 Examples 9 ERB 9 4: Hanami 11 11 Examples 11 Hanami 11 Hanami 11 12 5: instance_eval 14 14 14 Examples 14 14 14 6: IRB 16 16 16 Examples 17 17 RubyIRB 17 7: method_missing 18 18 18 Examples 18 18 18 19 19 8: OptionParser 20 20 Examples 20 20 20 21 9: rbenv 22 Examples 22 1.rbenvRuby 22 Ruby 23 10: Ruby 24 24 Examples 24 24 25 11: Ruby 27 Examples 27 ____ 27 __DIR__ 27 $ PROGRAM_NAME$ 0 27 $$ 27 12 27 ARGV$ * 27 STDIN 27 STDOUT 27 STDERR 28 $ 28 $ 28 $ 28 ENV 28 12: Ruby 29 Examples 29 29 30 31 32 13: Ruby 35 Examples 35 35 35 14: Ruby 37 Examples 37 gemset 37 RVMRuby 37 15: Ruby 38 38 Examples 38 38 40 16: Splat* 43 Examples 43 43 43 17: RubyJSON 44 Examples 44 RubyJSON 44 44 18: 45 45 Examples 45 45 45 46 47 48 19: 49 49 Examples 49 “with” 49 49 50 send 50 50 20: 52 Examples 52 52 52 53 53 54 54 21: 56 Examples 56 56 56 56 56 57 22: 58 58 58 Examples 58 58 58 58 59 23: Ruby 61 61 Examples 61 61 24: 64 64 64 64 Examples 64 64 25: 65 65 65 Examples 65 65 67 68 68 26: 71 Examples 71 71 27: 72 72 72 72 Examples 72 72 73 74 75 76 76 77 77 77 78 78 28: 79 79 79 Examples 79 79 Singleton 79 Singleton/ 80 Singleton 81 Singleton 81 Singleton 81 Singleton 81 Singleton 82 83 29: Ruby 85 85 85 Examples 85 85 ruby 85 85 86 86 30: Ruby 87 Examples 87 87 87 31: Ruby 88 88 Examples 88 88 32: Lambdas 89 89 89 Examples 89 PROC 89 Lambda 89 90 91 91 92 Proc 93 Currying 93 Currying 94 currying 94 33: 96 96 Examples 96 2D 96 3D 96 96 96 34: 98 98 Examples 98 98 98 99 99 99 100 100 101 102 102 102 102 103 103 103 35: 105 Examples 105 Linux - 105 Linux 105 Windows - 105 105 Linux - gem 106 Ruby macOS 107 36: / 108 Examples 108 Gemspec 108 109 109 37: 110 Examples 110 110 110 github / filesystemgem 110 gem 111 GemfileBundler 111 Bundler / inlinebundler v1.10 112 38: 114 114 114 Examples 114 114 114 114 114 39: 115 115 Examples 115 Ruby 115 if-else 115 40: / 116 Examples 116 116 116 117 118 118 119 41: 121 Examples 121 ifelsifelseend 121 TruthyFalsy 122 122 if / 122 123 123 breaknextredo 125 break 125 next 125 redo 125 Enumerable 126 126 126 127 127 returnnext 128 Or-Equals /|| = 128 129 129 42: Shell 131 131 131 Examples 131 Rubyshell 131 RubyshellClasic 133 43: 134 134 Examples 134 134 134 135 44: 136 136 136 Examples 136 136 136 137 137 137 138 138 138 45: 139 139 Examples 139 139 [] 139 139 140 Array :: new 140 141 142 142 142 142 142 143 143 splat* 144 145 146 146 / 146 147 #compactnil 148 148 149 46: I / O 150 150 Examples 150 150 150 char 151 STDIN 151 ARGV 151 47: 153 153 153 153 Examples 154 154 h11 154 154 h12 154 154 h13 154 splat 155 h14 155 mix 155 155 splat 156 157 158 158 159 48: 160 160 Examples 160 strftime 160 160 49: 161 161 161 Examples 161 161 161 161 50: 163 163 163 Examples 163 163 163 mixin 164 164 51: 166 Examples 166 166 = 166 166 167 168 168 168 - 168 169 52: 170 Examples 170 170 170 171 172 53: 174 174 174 Examples 174 174 54: 175 175 175 Examples 175 6 175 175 55: 176 176 176 176 Examples 177 177 178 178 56: 179 Examples 179 179 179 179 57: 180 180 180 Examples 180 DateTime 180 180 / 180 58: RSpec JSON API 182 Examples 182 SerializerController 182 59: 185 185 Examples 185 185 185 185 60: 186 Examples 186 186 186 187 187 61: 188 Examples 188 188 188 62: 189 Examples 189 189 63: 190 Examples 190 PryByebug 190 64: 191 Examples 191 191 1 191 2 191 192 192 192 193 193 65: 195 195 195 && vs. and ||or 195 Examples 195 195 === 197 198 66: 200 Examples 200 200 200 200 200 200 67: 202 202 Examples 202 202 203 203 203 204 68: 206 Examples 206 206 206 206 206 69: 208 208 Examples 208 208 209 splat 210 70: 212 212 Examples 212 212 212 - - 212 - #push 213 - #pop 213 - 213 214 214 71: 215 Examples 215 215 215 215 72: 218 218 218 Examples 218 218 218 218 gettersetter 219 220 220 221 222 222 222 223 223 224 225 226 You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: ruby-language It is an unofficial and free Ruby Language ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Ruby Language. The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners. Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected] https://riptutorial.com/zh-TW/home 1 1: Ruby Ruby。Yukihiro MatsumotoMatz1995。 RubyPerl Smalltalk Eiffel AdaLisp 。。。 2.4 20161225 2.3 20151225 2.2 2.1 20131225 2.0 2013224 1.9 2007-12-25 1.8 2003-08-04 1.6.8 2002-12-24 Examples IRBHello World Interactive Ruby Shell IRBRubyRuby。 IRB $ irb puts "Hello World" Hello World print print "Hello World" tk TkRubyGUI。RubyGUI。 https://riptutorial.com/zh-TW/home 2 require "tk" TkRoot.new{ title "Hello World!" } Tk.mainloop require "tk" tk。 TkRoot.new{ title "Hello World!" } Hello World Tk.mainloop 。 Ruby。 hello.rb puts 'Hello World' Ruby $ ruby hello.rb Hello World 。 Ruby。 RubyRuby。 Hello World Rubyshell。RubyRuby ruby -e 'puts "Hello World"' https://riptutorial.com/zh-TW/home 3 Ruby。shellhere ruby <<END puts "Hello World" END Hello World - ShebangUnix shebang。hello_world.rb #!/usr/bin/env ruby puts 'Hello World!' 。Unix $ chmod u+x hello_world.rb Ruby。 $ ./hello_world.rb my_first_method.rb def hello_world puts "Hello world!" end hello_world() # or just 'hello_world' (without parenthesis) ruby my_first_method.rb Hello world! • defdef -ining- hello_world。 • puts "Hello world!" puts putsHello world! • endhello_world • hello_world Ruby https://riptutorial.com/zh-TW/ruby/topic/195/ruby https://riptutorial.com/zh-TW/home 4 2: C Examples C 1. C。 2. 。 extconf.rb require 'mkmf' create_makefile('hello_c') hello_c。require。 extconf.rbgemruby extconf.rbMakefile。Makefile.c。 hello.cruby extconf.rb && make #include <stdio.h> #include "ruby.h" VALUE world(VALUE self) { printf("Hello World!\n"); return Qnil; } // The initialization method for this module void Init_hello_c() { VALUE HelloC = rb_define_module("HelloC"); rb_define_singleton_method(HelloC, "world", world, 0); } Init_hello_cextconf.rbRuby。 rb_define_moduleHelloCRubyC。 rb_define_singleton_methodHelloCHelloC.worldrubyHelloC.world 。 makeC。 irb(main):001:0> require './hello_c' => true irb(main):002:0> HelloC.world Hello World! => nil C https://riptutorial.com/zh-TW/home 5 CRubyData_Wrap_StructData_Get_Struct。 Data_Wrap_StructRubyData_Wrap_StructC。VALUE。 Data_Get_StructVALUEC。 #include <stdio.h> #include <ruby.h> typedef struct example_struct { char *name; } example_struct; void example_struct_free(example_struct * self) { if (self->name != NULL) { free(self->name); } ruby_xfree(self); } static VALUE rb_example_struct_alloc(VALUE klass) { return Data_Wrap_Struct(klass, NULL, example_struct_free, ruby_xmalloc(sizeof(example_struct))); } static VALUE rb_example_struct_init(VALUE self, VALUE name) { example_struct* p; Check_Type(name, T_STRING); Data_Get_Struct(self, example_struct, p); p->name = (char *)malloc(RSTRING_LEN(name) + 1); memcpy(p->name, StringValuePtr(name), RSTRING_LEN(name) + 1); return self; } static VALUE rb_example_struct_name(VALUE self) { example_struct* p; Data_Get_Struct(self, example_struct, p); printf("%s\n", p->name); return Qnil; } void Init_example() { VALUE mExample = rb_define_module("Example"); VALUE cStruct = rb_define_class_under(mExample, "Struct", rb_cObject); rb_define_alloc_func(cStruct, rb_example_struct_alloc); rb_define_method(cStruct, "initialize", rb_example_struct_init, 1); rb_define_method(cStruct, "name", rb_example_struct_name, 0); } extconf.rb require 'mkmf' create_makefile('example') https://riptutorial.com/zh-TW/home 6 irb(main):001:0> require './example' => true irb(main):002:0> test_struct = Example::Struct.new("Test Struct") => #<Example::Struct:0x007fc741965068> irb(main):003:0> test_struct.name Test Struct => nil C - RubyInLine RubyInlineRuby。Moduleinline。RubyRuby。 CC ++RubyInline。.ruby_inline。 RubyC • RubyInlinerubyinline gem RubyInlineirb #!/usr/bin/ruby -w # copy.rb require 'rubygems' require 'inline' class Copier inline do |builder| builder.c <<END void copy_file(const char *source, const char *dest) { FILE *source_f = fopen(source, "r"); if (!source_f) { rb_raise(rb_eIOError, "Could not open source : '%s'", source); } FILE *dest_f = fopen(dest, "w+"); if (!dest_f) { rb_raise(rb_eIOError, "Could not open destination : '%s'", dest); } char buffer[1024]; int nread = fread(buffer, 1, 1024, source_f); while (nread > 0) { fwrite(buffer, 1, nread, dest_f); nread = fread(buffer, 1, 1024, source_f); } } END end end Ccopy_fileCopier https://riptutorial.com/zh-TW/home 7 open('source.txt', 'w') { |f| f << 'Some text.' } Copier.new.copy_file('source.txt', 'dest.txt') puts open('dest.txt') { |f| f.read } C https://riptutorial.com/zh-TW/ruby/topic/5009/c https://riptutorial.com/zh-TW/home 8 3: ERB ERBRubyRubyHTMLYAML。 ERBRubyERBRuby。 • <number = rand10> • <= number> • <comment text> • ERBERBRuby。 • .erb.js.erb .html.erb .css.erb。 Examples ERB IRB。 => require 'erb' => input = <<-HEREDOC <ul> <% (0..10).each do |i| %> <%# This is a comment %>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages249 Page
-
File Size-