ruby Ruby片段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Ruby片段相关的知识,希望对你有一定的参考价值。

#method

def method(par)
  par = "hello world"
end

def method
  puts = "hello world"
end

#string

fname = "endy"
lname = "haniff"

puts = fname + lname

#string interpolation #{variable} not work in single quote

name = "endy haniff"

puts "My name is #{endy}"

#accessing method in common class
fname = "endy"
fname.length #outputting 4

#evaluate string
fname = "" 
fname.nil? #output false
fname.empty? #output true

#ask input
puts "Enter your first Name"

firstName = gets.chomp

puts "Welcome #{firstName}"

#array
fname = ["endy","haniff"]

#hash or associative arrays
fname = { "name" => "Endy", "color" => "red" }

#hash access with symbol
fname = { a => 1, b =>2 , c => 3}
fname[:c] #output 3

#oop class
class User
    
end

user = User.new #instantiate

puts user

以上是关于ruby Ruby片段的主要内容,如果未能解决你的问题,请参考以下文章

ruby 方便的片段

ruby serverspec片段

ruby 片段

ruby Codiing片段

ruby RubySteps 012 - Rails - 迷你框架片段

ruby 我感兴趣的库中的代码片段