Ruby初学笔记之Hello World
程序员文章站
2024-01-07 13:41:46
注:ruby windows安装包请前往下载安装。
安装了个ruby193,写了第一个ruby程序,来记录下自己学习点滴。
首先在ruby的新建个文件夹sample,再...
注:ruby windows安装包请前往下载安装。
安装了个ruby193,写了第一个ruby程序,来记录下自己学习点滴。
首先在ruby的新建个文件夹sample,再新添加个文件,名字helloworld.rb
内容就是:
复制代码 代码如下:
puts "hello,world!"
当然也可以先声明变量,如后再用puts语句
复制代码 代码如下:
a = "hello,world!"
puts a
当然也可以先写个函数,再调用,让其输出。
复制代码 代码如下:
def say(name)
"hello,#{name}"
end
puts say("world!")
考虑到ruby是完全面向对象,也可以先写个类,再调类里面的对象方式来完成。
复制代码 代码如下:
class hello
attr_reader :msg
def initialize
@msg = "hello, world"
end
end
h = hello.new
puts h.msg
print "press return"
$stdin.gets
运行方式,如下:
复制代码 代码如下:
c:\ruby193\sample>ruby helloworld.rb
hello, world!
推荐阅读