Ruby(初学)
Su_CRF
没有.
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
[Ruby笔记]12.Ruby 方法参数变量优先级method(*arg)
def m(a,b=2,*c,d) puts "a = #{a} ,b = #{b} ,c = #{c} ,d = #{d} "endm(1,3)# a = 1 ,b = 2 ,c = [] ,d = 3m(1,3,5)# a = 1 ,b = 3 ,c = [] ,d = 5m(1,3,5,7)# a = 1 ,b = 3 ,c = [5] ,d = 7m(1,3,5,7,9)# a = 1 ,原创 2016-05-23 20:13:22 · 1256 阅读 · 0 评论 -
[Ruby笔记]19.Ruby 2.0+ prepend 与 include
# code创建一个`win.rb` 文件:```PS C:\Users\Administrator\RubyCode> ls-a--- 2016/6/3 16:52 142 win.rb```使用`prepend M`语句 , `module win` :```module M def hello puts "Module win!" endend原创 2016-06-03 16:58:13 · 517 阅读 · 0 评论 -
[Ruby笔记]21.Ruby public_method_defined? 以及 method_missing
public_method_defined?在A类中定义了一个hello方法,使用public_method_defined?就可以判断是不是存在 :PS C:\Users\Administrator\Rubycode> irb --simple-prompt>> class A>> def hello>> puts "hello hi">> end>> end>> A.pub原创 2016-06-03 20:23:38 · 833 阅读 · 0 评论 -
[Ruby笔记]22.Ruby :: namespace 以及 instance method 与class method
:: namespace在module里面放了一个class,要实例化这个class,需要用M::C.new , module M 起到了namespace的作用,从视觉上大大加强代码的可读性:PS C:\Users\Administrator> irb --simple-prompt>> module M>> class C>> end>> end=> nil>> t = M::C.原创 2016-06-03 21:16:35 · 1893 阅读 · 0 评论 -
[Ruby笔记]10. Ruby object return Boolean nil false #{}
>> if puts "Hello world">> puts "You can't see this">> endHello world=> nil原创 2016-05-23 14:20:02 · 661 阅读 · 0 评论 -
[Ruby笔记]11.Ruby == .equal? object .object_id .respond_to? .send()
obj = Object.newdef obj.hello "hello world"enddef obj.year "2016/05/23"endputs "obj's id is #{obj.object_id}"puts "Information : "request = gets.chompif obj.respond_to?(request)原创 2016-05-23 15:32:20 · 660 阅读 · 0 评论 -
[Ruby笔记]23.Ruby self “main class module instance”
# self !- **test_self.rb** ```puts "Top Level"puts "self is #{self}" # self is mainclass C puts "Class definition block : " puts "self is #{self}" #self is C # class method def self.x puts "Cl原创 2016-06-04 23:25:56 · 548 阅读 · 0 评论 -
[Ruby笔记]25.local scope 本地作用域
code# file : nest2.rbclass A x = 'A' module M x = 'M' class B x = 'B' def show_x x = 'X' puts x end原创 2016-06-28 20:29:21 · 560 阅读 · 0 评论 -
[Ruby笔记]24.Ruby全局变量 $global_variable
PS C:\Users\Administrator> irb --simple-prompt>> class Time>> def hour_minute_seconds>> time = $hour + ":">> time << "#{$minute} : " if $minute>> time << "#{$second}">> end>> end=> :hour_minute_原创 2016-06-14 01:29:40 · 945 阅读 · 0 评论 -
[Ruby笔记]26. self 不变,每一次调用函数都会产生新的local scope
class C def hello(a, recurse = false) print "Now , self is : " p self print "self object id is : " p self.object_id print "And here's a : " puts a print "a has object id : " p a.object_id p原创 2016-07-07 18:11:01 · 805 阅读 · 0 评论 -
[Ruby笔记]27. ::String 前加双冒号确保使用built-in Ruby class
::String# File : ex.rbclass V class String attr_reader :picth def initialize(picth) @picth = picth + " From My String" puts @picth end end d原创 2016-07-07 18:48:06 · 1407 阅读 · 0 评论 -
[Ruby笔记]28.Ruby @@class_variables 类变量 vs @instance_variable 实例变量
@@class_variablesTestCase想象这样一个场景,一次小班授课,参加的学生有A B C 三人 ,这时候老师开始提问了,我们使用类Student 记录 : 到场的学生名单;每人答题次数;老师总的提问次数CodeClass Student 类@@names存着学生名单的数组 ; @@answers存着每个学生答题次数的哈希表 ;@@total_answers存着老师总原创 2016-07-19 23:42:52 · 752 阅读 · 0 评论 -
[Ruby笔记]29. Ruby yield from method to block 从方法到块
yieldyield关键字可以实现从方法method到块block的往返.exampleoutputPS E:\ruby> ruby test_yield.rb1.method2.block3.back to the methodsource codedef test puts "1.method" yield puts "3.back to the meth原创 2017-01-25 09:41:33 · 745 阅读 · 3 评论 -
[Ruby笔记]30.Ruby hash symbol 在哈希表中用符号作为键
reference 《The Well-Grounded Rubyist, Second Edition》 (https://www.manning.com/books/the-well-grounded-rubyist-second-edition)あついおっ~~~~~ ∧ ∧ γ⌒ヽ (* 'ω')i ミ(二i ( ∪ ∪ ヽ、,_|| と_)_) r-!!原创 2017-01-25 17:14:55 · 1657 阅读 · 0 评论 -
[Ruby笔记]20.Ruby super initialize class
# code in file `abc.rb````class SuperABC def initialize @a = 1 @b = 2 @c = 3 endendclass ABC < SuperABC def initialize super # keyword @a = 0 end def a puts @a end def b puts @b原创 2016-06-03 17:38:06 · 1095 阅读 · 0 评论 -
[Ruby笔记]9.Ruby文档工具 ri ruby-doc Windows CMD.exe Powershell
在Windows上安装ruby-doc ri 工具安装步骤Ruby的命令行文档工具ri ,一般安装步骤[1]1.在终端运行 gem install rdoc-data 安装2.然后需要生成 ri 数据,在终端运行 rdoc-data –install gem rdoc –all –overwrite gem rdoc --all --ri --no-rdoc可能会遭原创 2016-05-21 02:09:45 · 1100 阅读 · 0 评论 -
[Ruby笔记]8. Ruby Rakefile rake 删除文件 确认
PS C:\Users\Administrator\RubyCode> rake admin:clean_tmpDelete ./tmp/新建文本文档 - 副本 (2).txt? yDelete ./tmp/新建文本文档 - 副本 (3).txt? yDelete ./tmp/新建文本文档 - 副本 (4).txt? yDelete ./tmp/新建文本文档 - 副本 (5).txt? yDelete ./原创 2016-05-21 01:04:59 · 714 阅读 · 0 评论 -
[Ruby笔记]13.Ruby object .replace("") .dup .freeze
.replace()打开irb :PS C:\Users\Administrator> irb --simple-prompt创建字符串变量str ,值为“hello”,赋值给变量abc,本质是使abc指向同一个字符串对象,所以均输出为同一个“hello” :>> str = "Hello"=> "Hello">> abc = str=> "Hello"对str 变量使用.replac原创 2016-05-25 01:51:38 · 668 阅读 · 0 评论 -
[Ruby笔记]14.Ruby local_variable
Ruby local_variable 可用命名格式a_anamefirst_namehello48user_ID_Ruby如何区分local_variable /keyword/ method call Identifier difference keyword 诸如def 、if之类keyword,Ruby内置一张关键词的表,可以直接判断出来 local_varia原创 2016-05-25 02:03:30 · 712 阅读 · 0 评论 -
[Ruby笔记]15.@instance_variable 以及 糖“在方法名中可使用=”
class Sugar def price=(amount) @price = amount end def price @price endend原创 2016-05-27 12:21:32 · 945 阅读 · 0 评论 -
[Ruby笔记]16.Ruby 判断数字 .is_a?(Numeric) .to_i
PS C:\Users\Administrator\RubyCode> irb --simple-prompt>> 100.is_a?(Numeric)=> true原创 2016-05-29 08:39:58 · 4401 阅读 · 0 评论 -
[Ruby笔记]17.Ruby attribute attr_reader attr_writer attr_accessor attr
class Test attr_reader :name, :sex attr_writer :age attr_accessor :weight def initialize(name, sex, weight) @name = name @sex = sex @weight = weight endend原创 2016-05-29 09:05:24 · 962 阅读 · 0 评论 -
[Ruby笔记]1. ruby ruby oh~ ruby 安装
Ruby安装下载https://www.ruby-lang.org/en/downloads/测试是否安装成功Windows >> PowerShell >>> ruby -vruby 2.3.0p0 (2015-12-25 revision 53290) [x64-mingw32]Ruby资源Learn Ruby the Hard Way, 3rd Edition. (Zed.Shaw)英文原原创 2016-05-15 19:43:01 · 570 阅读 · 0 评论 -
[Ruby笔记]2. ruby基础的基础: irb --simple-prompt / puts print p/ false nil
irb –simple-promptPS C:\Users\Administrator> irbirb(main):001:0> exit()PS C:\Users\Administrator> irb --simple-prompt>> exit()PS C:\Users\Administrator>irb –simple-prompt 模式更“清爽”puts print p创建两个变量x原创 2016-05-16 02:09:49 · 824 阅读 · 0 评论 -
[Ruby笔记]3. 语法检查ruby -cw xx.rb /代码运行 ruby xx.rb/变量约定 @ @@ $
ruby -cw xx.rbruby检查代码语法 ruby 后面加个 -cwPS C:\Users\Administrator\RubyCode> more ex11.rb# missing "puts "Hello ,worldPS C:\Users\Administrator\RubyCode> ruby -cw ex11.rbex11.rb:2: unterminated strin原创 2016-05-16 21:36:12 · 1137 阅读 · 0 评论 -
[Ruby笔记]4. ruby 读写文件 File open read write new
Run新建了一个文件temp.dat 存了一个数据,整数 100PS C:\Users\Administrator\RubyCode> more temp.dat100PS C:\Users\Administrator\RubyCode> ruby c2fin2out.rbReading Celsius temperature value from data file...The Celsius原创 2016-05-16 21:56:45 · 1923 阅读 · 0 评论 -
[Ruby笔记]18.Ruby 继承 Inheritance 与 .superclass
- 打开`irb` ,定义一个`Super`类 ```PS C:\Users\Administrator> irb --simple-prompt>> class Super>> def say_hello>> "hello">> end>> end=> :say_hello>> class My < Super>> end```原创 2016-05-30 08:08:46 · 930 阅读 · 0 评论 -
[Ruby笔记]5. Ruby RbConfig::CONFIG[""]
irb --simple-prompt -rrbconfig原创 2016-05-19 01:16:02 · 1139 阅读 · 0 评论 -
[Ruby笔记]6. Ruby load require 使用对比
# load "loadee.rb"# require "./loadee.rb"# require "./loadee"require_relative "loadee"原创 2016-05-19 02:07:40 · 607 阅读 · 0 评论 -
[Ruby笔记]7.ruby -e ' " 单引号、双引号对比
使用`ruby -e "..."`可以在命令行直接运行脚本```PS C:\Users\Administrator\RubyCode> ruby -e "print 'Enter a name: '; print gets.reverse"Enter a name: TommoTPS```原创 2016-05-20 03:16:03 · 1891 阅读 · 0 评论 -
[Ruby笔记]31.ruby set 类型 交、并、补、异或 & + - ^ |
setset是Ruby的标准库的类;打开irb使用,需要先写语句 require 'set';codeirb$ irb --simple-prompt>> require 'set'=> true创建两个集合 ()里面放一个数组即可>> first = Set.new(["A","B","C","D"])=> #<Set: {"A", "B", "C", "D"}>>> sec原创 2017-01-26 00:03:59 · 3171 阅读 · 0 评论
分享