ruby 学习笔记 3 ruby简单的语法

在netbean里 参照 《programming ruby》学习

def say_goodnight(name)
   result="goog night ,#{name}"+name
   return result

end
#time for bed
puts say_goodnight("chenyu")
puts say_goodnight('boy')

#testing array
def test_array()
  a=[1,'cat',3.14]
  b=%w{dog cat pig}    #array all string
  puts a[1]
  puts a[0]
  puts a[2]
  puts a[3]  #nil

  puts b[1]
  puts b[0]
  puts b[2]
  puts b[3]  #nil

end

test_array()


def test_collections()
  inst_section={
    'key1'=>'hello',
    'key2'=>'boys',
    'key3'=>'and',
    'key4'=>'girls'

  }
  puts inst_section['key2']
end

test_collections()

# new Hash

def test_new_hash()
  hash=Hash.new(0)
  hash['key1']=1;
  puts hash['key1']    #1
  puts hash['key2']    #0

end
test_new_hash()

def test_if_condition()
  count=2
  if(count>10)
    puts "try again"
  elsif count==0
    puts "you lose"
  else
    puts "enter a number"
  end
end

test_if_condition()

def test_while_condition()
  while line=gets
    puts line.downcase     #"hELLO"=>"hello"
  end
end

#test_while_condition()

def test_statement_modifiers()
  a=10
  puts "hello" if a==10
  
   a =a+1 while a<20
   puts a
end

test_statement_modifiers()

 

你可能感兴趣的:(Ruby)