zoukankan      html  css  js  c++  java
  • Ruby安装和简介

    Ruby下载地址:https://www.ruby-lang.org/zh_cn/downloads/

    我安装的是RubyInstaller.it is a self-contained Windows-based installer that includes the Ruby language, an execution environment, important documentation, and more.

    安装界面如下:

    点击install就安装成功了。

    安装结束后,运行ruby -v 显示版本号。如果正常显示Ruby版本号,表示安装成功。

    如果没有正常显示ruby的版本号,则自行添加系统变量

    SET RUBY_HOME=D:/ruby
    SET PATH=%PATH%;%RUBY_HOME%/bin
    SET RUBYOPT=rubygems

    把以上代码复制到记事本,另存为ruby.bat,然后执行文件即可。

    Ide现在用notepad++。

    notepad++ NppExec 扩展可以很方便运行各类程序。

     按下快键 F6 ,将出现下面窗口(NppExec),在窗口中输入:


      ruby $(FULL_CURRENT_PATH)

      点击ok,  啊哦,ruby: No such file or directory -- new (LoadError) 
      呵呵  你忘了保存(注:不要保存在有空格的目录下),save and ok again, 结果出来了。

    ruby F: uby ubySource est1.rb
    Process started >>>
    hello
    <<< Process finished. (Exit code 0)

    更多参考:http://lemonzc.iteye.com/blog/150919

    http://www.rubytips.org/2011/12/22/using-notepad-for-writing-ruby-programs-in-windows/

    快速入门:

    https://www.ruby-lang.org/zh_cn/documentation/quickstart/2/

    irb(main):019:0> def h(name = "World")
    irb(main):020:1> puts "Hello #{name.capitalize}!"
    irb(main):021:1> end
    => nil
    irb(main):022:0> h "chris"
    Hello Chris!
    => nil
    irb(main):023:0> h
    Hello World!
    => nil
    

    这里还有几个小窍门。第一是我们又一次省略了函数的括号。如果我们的命令看起来意图很明显的话, 函数的括号是可以省略的。另一个是函数缺省的参数值是World。意思就是说 “如果 name 参数没有给出的话, name 的缺省值就设置为“World"

    Ruby是一门完全面向对象的语言。

    irb(main):023:0> 4.class
    => Fixnum
    irb(main):024:0> 4.methods
    => [:to_s, :inspect, :-@, :+, :-, :*, :/,
    , :abs, :magnitude, :==, :===, :<=>, :>, :。。。

    irb(main):025:0> false.class
    => FalseClass
    irb(main):026:0> true.class
    => TrueClass
    irb(main):027:0>

    ruby中的true和false是一等对象(first-clas object)。(即与整数,浮点数等基本类型同等处理的对象,注意,这里说的对象并非特指面对对象语言中的对象,而是泛指编程语言中的类型,一等对象应具有几个性质:可存储变量或数据结构中;可作为参数传递给函数;可作为返回值从函数函数,可在运行时创建。举例来说:
    c++对象就是一等对象。但其函数无法在运行时创建,所以不是一等对象;与之相反,函数式语言中的函数是一等对象,因为它既可以传递和返回,也可以在运行时动态创建)。

    if:

    x=4
    puts "this appear to be true" if x==4
    if x==4
    puts "this appear to be true"
    end

    puts "this appeart to be false unless x==4

    当使用if,unless时,可以使用block块形式(if condition,statements,end),也可以选用单行形式(statement if condition).

    irb(main):041:0> puts "yes" if x
    yes
    => nil
    irb(main):042:0>

    除了nil和false,其他值都代表true.即使是0.

    Duck Typing
    Let’s get into Ruby’s typing model a little. The first thing you need to
    know is how much protection Ruby will give you when you make a
    mistake with types. We’ re talking about type safety. Strongly typed lan-

    guages check types for certain operations and check the types before
    you can do any damage. This check can happen when you present the
    code to an interpreter or a compiler or when you execute it. Check out
    this code:
    >> 4 + 'four'
    TypeError: String can't be coerced into Fixnum
    from (irb):51:in `+'
    from (irb):51
    >> 4.class
    => Fixnum
    >> (4.0).class
    => Float
    >> 4 + 4.0
    => 8.0
    So, Ruby is strongly typed,3 meaning you’ll get an error when types
    collide. Ruby makes these type checks at run time, not compile time.
    I’m going to show you how to define a function a little before I normally
    would to prove the point. The keyword def defines a function but doesn’t
    execute it. Enter this code:

    >> def add_them_up
    >> 4 + 'four'
    >> end
    => nil
    >> add_them_up
    TypeError: String can't be coerced into Fixnum
    from (irb):56:in `+'
    from (irb):56:in `add_them_up'
    from (irb):58
    So, Ruby does not do type checking until you actually try to execute
    code. This concept is called dynamic typing. 只有真正运行时,才进行类型检查,这个叫做动态类型。

    There are disadvantages:
    you can’t catch as many errors as you can with static typing because
    compilers and tools can catch more errors with a statically typed system.
    But Ruby’s type system also has several potential advantages.
    Your classes don’t have to inherit from the same parent to be used
    in the same way:

    => 0
    irb(main):044:0> a=["100",100.0]
    => ["100", 100.0]
    irb(main):045:0> while i<2
    irb(main):046:1> puts a[i].to_i
    irb(main):047:1> i=i+1
    irb(main):048:1> end
    100
    100
    => nil

    You just saw duck typing in action. The first element of the array is
    a String, and the second is a Float. The same code converts each to an
    integer via to_i. Duck typing doesn’t care what the underlying type might
    be. If it walks like a duck and quacks like a duck, it’s a duck. In this
    case, the quack method is to_i.
    Duck typing is extremely important when it comes to clean objectoriented
    design. An important tenet of design philosophy is to code
    to interfaces rather than implementations. If you’re using duck typing,
    this philosophy is easy to support with very little added ceremony. If
    an object has push and pop methods, you can treat it like a stack. If it
    doesn’t, you can’t.

    散列表hashes

    >> numbers = {1 => 'one', 2 => 'two'}
    => {1=>"one", 2=>"two"}
    >> numbers[1]
    => "one"
    >> numbers[2]
    => "two"
    >> stuff = {:array => [1, 2, 3], :string => 'Hi, mom!'}
    => {:array=>[1, 2, 3], :string=>"Hi, mom!"}
    >> stuff[:string]
    => "Hi, mom!

    最后那个散列表很有趣,因为我在其中首次引入了符号(symbol)。符号是前面带有冒号的标识符,类似于:symbol的形式,他在给事物和概念命名时非常好友,尽管两个同值字符串在物理上不同,但相同的符号却是同一物理对象。我们可以通过多次获取相同的符号对象标识符来证实这一点,类似下面:

    irb(main):055:0> "string".object_id
    => 14967024
    irb(main):056:0> "string".object_id
    => 18150060
    irb(main):057:0> :string.object_id
    => 94184
    irb(main):058:0> :string.object_id
    => 94184

    hash有一些别处新裁的应用,比如:ruby虽然不支持named argument,但可以利用hash来模拟它,只要加进一颗小小的语法糖,他就能获得一些有趣的特性:

    >> def tell_the_truth(options={})
    >> if options[:profession] == :lawyer
    >> 'it could be believed that this is almost certainly not false.'
    >> else
    >> true
    >> end
    >> end
    => nil
    >> tell_the_truth
    => true
    Report erratum
    this copy is (P1.0 printing, October 2010)
    Download from Wow! eBook <www.wowebook.com>
    DAY 2: FLOATING DOWN FROM THE SKY 39
    >> tell_the_truth :profession => :lawyer
    => "it could be believed that this is almost certainly not false."

    hash用作函数最后一个参数时,{}可有可无。

    Code Blocks and Yield 代码块和yield
    A code block is a function without a name. You can pass it as a parameter
    to a function or a method. For example:

    代码块是没有命名的函数,可以传给fucntion或method作为参数。

    irb(main):059:0> 3.times{puts "helo"}
    helo
    helo
    helo
    => 3

    大括号之间的代码就叫做代码块,times是Fixnum类的方法,可以采用
    {/}和do/end 两种界定代码块的形式,ruby一般惯例是:代码块只占一行时用大括号,代码块占多行时用do/end,代码块可带有一个或多个参数:

    >> animals = ['lions and ', 'tigers and', 'bears', 'oh my']
    => ["lions and ", "tigers and", "bears", "oh my"]
    >> animals.each {|a| puts a}
    lions and
    tigers and
    bears
    oh my

    上面这段代码能让你见识到代码块的威力,它指示Ruby在集合里的每个元素上执行某些行为,仅仅用上少许代码块语句,Ruby就遍历了每一个元素,还把他们全都打印出来。To

    really get a feel for what’s going on, here’s a custom implementation of
    the times method:

    class Fixnum
      def my_times
         i=self
         while i>0
         i=i-1
         yield
         end
      end
     end
     3.my_times{puts 'mangy moose'}

    This code opens up an existing class and adds a method. In this case,
    the method called my_times loops a set number of times, invoking the
    code block with yield. Blocks can also be first-class parameters. Check
    out this example:

    >> def call_block(&block)
    >> block.call
    >> end
    => nil
    >> def pass_block(&block)
    >> call_block(&block)
    >> end
    => nil
    >> pass_block {puts 'Hello, block'}
    Hello, block

    This technique will let you pass around executable code. Blocks aren’t
    just for iteration. In Ruby, you’ll use blocks to delay execution...

    这技术能让你把可执行代码派发给其他方法。在Rbuy中,代码块不仅用于循环,还可用于延迟执行。


    execute_at_noon { puts 'Beep beep... time to get up'}

    (在Ruby中,参数名之前加一个“&”,表示将代码块作为闭包传递给函数。)

    执行某些条件行为:

    ...some code...
    in_case_of_emergency do
       use_credit_card
      panic
    end
    def in_case_of_emergency
       yield if emergency?
    end
    ...more code...
    enforce policy...
    within_a_transaction do
    things_that
    must_happen_together
    end
    def within_a_transaction
    begin_transaction
    yield
    end_transaction
    end

    以及诸多其他用途,你会见到各种使用代码块的Ruby库,包括处理文件的每一行,执行HTTp事务中的任务,在集合上进行各种复杂操作,Ruby简直就是一个代码块的大联欢。

  • 相关阅读:
    13.困难重重
    02.Django的第一个网页
    03.Django模板
    01.Web开发之简介
    14.效率利器之多线程和线程池
    Silverlight实用窍门系列:37.Silverlight和ASP.NET相互传参的两种常用方式(QueryString,Cookie)
    Web编码规范中文乱码解决方案
    H*ber*ate Lazy属性
    Bat如何判断txt文本中第8行是否只有"符号,有则删除整行
    #每日一练 获取字典值
  • 原文地址:https://www.cnblogs.com/youxin/p/3751526.html
Copyright © 2011-2022 走看看